/
templates.py
334 lines (271 loc) · 12.2 KB
/
templates.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
#
# Copyright 2021 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.
"""Renku common fixtures."""
import shutil
import textwrap
from pathlib import Path
from typing import Generator, List, Optional
import pytest
from renku.core.template.template import EmbeddedTemplates, FileAction, RepositoryTemplates, copy_template_to_project
from renku.domain_model.project_context import project_context
from renku.domain_model.template import (
RenderedTemplate,
Template,
TemplateMetadata,
TemplateParameter,
TemplatesManifest,
)
from renku.infrastructure.repository import Repository
from renku.version import __version__ as renku_version
from tests.fixtures.repository import RenkuProject
@pytest.fixture
def template_metadata():
"""Default template metadata."""
yield {
"__template_source__": "renku",
"__template_ref__": renku_version,
"__template_id__": "python-minimal",
"__namespace__": "",
"__repository__": "",
"__project_description__": "no description",
"__project_slug__": "",
"__renku_version__": renku_version,
}
@pytest.fixture
def template(template_metadata):
"""Yield template data."""
template = {
"url": "https://github.com/SwissDataScienceCenter/renku-project-template",
"id": "python-minimal",
"index": 1,
"branch": "master",
# TODO: Add template parameters here once parameters are added to the template.
"metadata": {},
"default_metadata": template_metadata,
}
yield template
@pytest.fixture()
def project_init(template):
"""Yield template data."""
data = {
"test_project": "test-new-project",
"test_project_alt": "test-new-project-2",
"main_branch": "main",
}
commands = {
"init": ["init", "."],
"init_test": ["init", data["test_project"]],
"init_alt": ["init", data["test_project_alt"]],
"init_custom": [
"init",
"--template-ref",
template["branch"],
"--template-id",
"python-minimal",
data["test_project"],
],
"init_custom_template": (
"https://gitlab.dev.renku.ch/renku-python-integration-tests/core-it-template-variable-test-project"
),
"remote": ["--template-source", template["url"], "--template-ref", template["branch"]],
"id": ["--template-id", template["id"]],
"force": ["--force"],
"parameters": ["--parameter", "p1=v1", "--parameter", "p2=v2"],
"parameters_equal_missing": ["--parameter", "p3:v3"],
"parameters_equal_early": ["--parameter", "=p4v3"],
"initial_branch_main": ["--initial-branch", "main"],
"confirm": len(set(template["metadata"].keys())) * "\n",
}
yield data, commands
@pytest.fixture
def templates_source_root(tmp_path) -> Path:
"""Root of Dummy TemplatesSource."""
return tmp_path / "templates_source"
@pytest.fixture
def source_template(templates_source_root) -> Template:
"""A dummy Template."""
dummy_template_root = templates_source_root / "dummy"
(dummy_template_root / ".renku").mkdir(parents=True, exist_ok=True)
(dummy_template_root / "{{ __name__ }}.dummy").touch()
(dummy_template_root / "README.md").write_text("""A Renku project: {{ __project_description__ }}\n""")
(dummy_template_root / "immutable.file").write_text("immutable content")
(dummy_template_root / ".gitignore").write_text(".swp")
(dummy_template_root / ".renku" / "renku.ini").touch()
(dummy_template_root / "requirements.txt").touch()
(dummy_template_root / "Dockerfile").write_text(
textwrap.dedent(
"""
ARG RENKU_BASE_IMAGE=renku/renkulab-r:4.1.2-0.11.0
FROM ${RENKU_BASE_IMAGE}
# Docker content
ARG RENKU_VERSION={{ __renku_version__ | default("0.42.0") }}
# More content
########################################################
# Renku-specific section - DO NOT MODIFY #
########################################################
FROM ${RENKU_BASE_IMAGE} as builder
RUN specific commands
########################################################
# End Renku-specific section #
########################################################
# More content
"""
)
)
return Template(
id="dummy",
aliases=[],
name="Dummy Template",
description="A dummy template",
source="dummy",
reference="1.0.0",
version="1.0.0",
allow_update=True,
parameters={},
immutable_files=["immutable.file"],
path=dummy_template_root,
icon="",
templates_source=None,
ssh_supported=False,
)
@pytest.fixture(params=["renku", "repository"])
def templates_source(request, monkeypatch, templates_source_root, source_template):
"""A dummy TemplatesSource."""
manifest = templates_source_root / "manifest.yaml"
manifest.write_text(
textwrap.dedent(
"""
- id: dummy
name: Dummy Template
description: A dummy template
immutable_template_files:
- immutable.file
"""
)
)
def update_template_files(templates_source, id, content, parameters):
template = templates_source.get_template(id=id, reference=None)
if template is None or template.path is None:
return
for relative_path in template.get_files():
path = template.path / relative_path
if relative_path == "Dockerfile":
path.write_text(path.read_text().replace("RUN specific commands", "RUN updated specific commands"))
else:
path.write_text(f"{path.read_text()}\n{content}")
template.parameters = parameters or []
def rename_template(templates_source, id: str, new_name: str):
shutil.copytree(templates_source.path / "dummy", templates_source.path / new_name)
manifest.write_text(
textwrap.dedent(
f"""
- id: {new_name}
name: Dummy Template
description: A dummy template
immutable_template_files:
- immutable.file
aliases: [{id}]
"""
)
)
# NOTE: Reload manifest file
templates_source.manifest = TemplatesManifest.from_path(manifest, skip_validation=True)
class DummyRenkuTemplatesSource(EmbeddedTemplates):
"""Base class for Renku template sources."""
@classmethod
def fetch(cls, source: Optional[str], reference: Optional[str]) -> "DummyRenkuTemplatesSource":
return cls(path=templates_source_root, source="renku", reference=reference, version=reference)
def update(self, id, version, content="# modification", parameters: Optional[List[TemplateParameter]] = None):
"""Update all files of a template."""
update_template_files(self, id, content, parameters)
self.version = self.reference = version
def rename(self, id: str, new_name: str, version: str):
"""Rename a template."""
rename_template(templates_source=self, id=id, new_name=new_name)
self.version = self.reference = version
class DummyRepositoryTemplatesSource(RepositoryTemplates):
"""Base class for Renku template sources."""
@classmethod
def fetch(cls, source: Optional[str], reference: Optional[str]) -> "DummyRepositoryTemplatesSource":
repository = Repository.initialize(templates_source_root)
repository.add(all=True)
repository.commit("dummy template", no_verify=True)
repository.tags.add(reference)
return cls(
path=templates_source_root,
source=f"file://{templates_source_root}",
reference=reference,
version=repository.head.commit.hexsha,
repository=repository,
)
def update(self, id, version, content="# modification", parameters: Optional[List[TemplateParameter]] = None):
"""Update all files of a template."""
update_template_files(self, id, content, parameters)
self.update_templates(f"Update {id} template", version)
def rename(self, id: str, new_name: str, version: str):
"""Rename a template."""
rename_template(templates_source=self, id=id, new_name=new_name)
self.update_templates(f"Rename {id} template", version)
def update_templates(self, message, version):
self.repository.add(all=True)
self.repository.commit(message, no_verify=True)
self.repository.tags.add(version)
self.version = self.repository.head.commit.hexsha
self.reference = version
if not request.param or request.param == "renku":
dummy_templates_source = DummyRenkuTemplatesSource.fetch("renku", reference="1.0.0")
elif request.param == "repository":
dummy_templates_source = DummyRepositoryTemplatesSource.fetch("renku", reference="1.0.0")
else:
raise ValueError(f"Invalid TemplatesSource value: {request.param}")
with monkeypatch.context() as monkey:
import renku.core.template.usecase
def mocked_fetch_templates_source(*_, **__):
return dummy_templates_source
monkey.setattr(renku.core.template.usecase, "fetch_templates_source", mocked_fetch_templates_source)
yield dummy_templates_source
@pytest.fixture
def rendered_template(source_template, template_metadata):
"""A dummy RenderedTemplate."""
rendered_template = source_template.render(metadata=TemplateMetadata.from_dict(template_metadata))
yield rendered_template
@pytest.fixture
def project_with_template(project, rendered_template, with_injection) -> Generator[RenkuProject, None, None]:
"""A project with a dummy template."""
with with_injection():
actions = {f: FileAction.OVERWRITE for f in rendered_template.get_files()}
project_object = project_context.project
copy_template_to_project(rendered_template=rendered_template, project=project_object, actions=actions)
project_object.template_files = [str(project_context.path / f) for f in rendered_template.get_files()]
project.repository.add(all=True)
project.repository.commit("Set a dummy template", no_verify=True)
yield project
@pytest.fixture
def rendered_template_with_update(tmp_path, rendered_template):
"""An updated RenderedTemplate that modifies some template files."""
updated_template_root = tmp_path / "rendered_template_with_update"
shutil.copytree(str(rendered_template.path), str(updated_template_root))
(updated_template_root / "immutable.file").write_text("updated immutable content")
(updated_template_root / ".gitignore").write_text(".swp\n.idea")
(updated_template_root / "requirements.txt").write_text("changed\n")
dockerfile = updated_template_root / "Dockerfile"
dockerfile.write_text(dockerfile.read_text().replace("RUN specific commands", "RUN updated specific commands"))
(updated_template_root / "README.md").write_text("""Updated README: {{ __project_description__ }}\n""")
updated_rendered_template = RenderedTemplate(
path=updated_template_root, template=rendered_template.template, metadata=rendered_template.metadata
)
yield updated_rendered_template