Skip to content

Commit ee346a4

Browse files
committed
Add support for the GardenLinux platform variant if defined
Signed-off-by: Tobias Wolf <[email protected]> On-behalf-of: SAP <[email protected]>
1 parent 023a3b9 commit ee346a4

File tree

4 files changed

+140
-57
lines changed

4 files changed

+140
-57
lines changed

src/gardenlinux/features/cname.py

Lines changed: 72 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from configparser import UNNAMED_SECTION, ConfigParser
99
from os import PathLike, environ
1010
from pathlib import Path
11-
from typing import List, Optional
11+
from typing import Optional
1212

1313
from ..constants import (
1414
ARCHS,
@@ -53,6 +53,7 @@ def __init__(self, cname, arch=None, commit_hash=None, version=None):
5353
self._feature_flags_cached = None
5454
self._feature_platform_cached = None
5555
self._feature_set_cached = None
56+
self._platform_variant_cached = None
5657

5758
self._flag_multiple_platforms = bool(
5859
environ.get("GL_ALLOW_FRANKENSTEIN", False)
@@ -250,6 +251,44 @@ def feature_set_platform(self) -> str:
250251
"Only one platform is supported"
251252
return platforms[0]
252253

254+
@property
255+
def platform(self) -> str:
256+
"""
257+
Returns the feature set of type "platform" for the cname parsed.
258+
259+
:return: (str) Feature set platforms
260+
:since: 0.7.0
261+
"""
262+
263+
return self.feature_set_platform
264+
265+
@property
266+
def platform_variant(self) -> Optional[str]:
267+
"""
268+
Returns the platform variant for the cname parsed.
269+
270+
:return: (str) Platform variant
271+
:since: 1.0.0
272+
"""
273+
274+
if self._platform_variant_cached is not None:
275+
return self._platform_variant_cached
276+
277+
# @TODO: Platform variant is set by GardenLinux features to the release file. If not read or cached it is currently invisible for this library.
278+
return None
279+
280+
@platform_variant.setter
281+
def platform_variant(self, variant: str) -> None:
282+
"""
283+
Sets the the platform variant
284+
285+
:param variant: Platform variant
286+
287+
:since: 1.0.0
288+
"""
289+
290+
self._platform_variant_cached = variant
291+
253292
@property
254293
def release_metadata_string(self) -> str:
255294
"""
@@ -268,6 +307,10 @@ def release_metadata_string(self) -> str:
268307
elements = ",".join(features["element"])
269308
flags = ",".join(features["flag"])
270309
platform = ",".join(features["platform"])
310+
platform_variant = self.platform_variant
311+
312+
if platform_variant is None:
313+
platform_variant = ""
271314

272315
metadata = f"""
273316
ID={GL_RELEASE_ID}
@@ -283,24 +326,14 @@ def release_metadata_string(self) -> str:
283326
GARDENLINUX_FEATURES_PLATFORM="{platform}"
284327
GARDENLINUX_FEATURES_ELEMENTS="{elements}"
285328
GARDENLINUX_FEATURES_FLAGS="{flags}"
329+
GARDENLINUX_PLATFORM_VARIANT="{platform_variant}"
286330
GARDENLINUX_VERSION="{self.version}"
287331
GARDENLINUX_COMMIT_ID="{self.commit_id}"
288332
GARDENLINUX_COMMIT_ID_LONG="{self.commit_hash}"
289333
""".strip()
290334

291335
return metadata
292336

293-
@property
294-
def platform(self) -> str:
295-
"""
296-
Returns the feature set of type "platform" for the cname parsed.
297-
298-
:return: (str) Feature set platforms
299-
:since: 0.7.0
300-
"""
301-
302-
return self.feature_set_platform
303-
304337
@property
305338
def version(self) -> Optional[str]:
306339
"""
@@ -360,12 +393,18 @@ def load_from_release_file(self, release_file: PathLike | str) -> None:
360393
)
361394

362395
loaded_cname_instance = CName(
363-
release_config.get(UNNAMED_SECTION, "GARDENLINUX_CNAME")
396+
release_config.get(UNNAMED_SECTION, "GARDENLINUX_CNAME").strip("\"'")
364397
)
365398

366-
commit_id = release_config.get(UNNAMED_SECTION, "GARDENLINUX_COMMIT_ID")
367-
commit_hash = release_config.get(UNNAMED_SECTION, "GARDENLINUX_COMMIT_ID_LONG")
368-
version = release_config.get(UNNAMED_SECTION, "GARDENLINUX_VERSION")
399+
commit_id = release_config.get(UNNAMED_SECTION, "GARDENLINUX_COMMIT_ID").strip(
400+
"\"'"
401+
)
402+
commit_hash = release_config.get(
403+
UNNAMED_SECTION, "GARDENLINUX_COMMIT_ID_LONG"
404+
).strip("\"'")
405+
version = release_config.get(UNNAMED_SECTION, "GARDENLINUX_VERSION").strip(
406+
"\"'"
407+
)
369408

370409
if (
371410
loaded_cname_instance.flavor != self.flavor
@@ -386,19 +425,28 @@ def load_from_release_file(self, release_file: PathLike | str) -> None:
386425

387426
self._feature_set_cached = release_config.get(
388427
UNNAMED_SECTION, "GARDENLINUX_FEATURES"
389-
)
428+
).strip("\"'")
390429

391-
self._feature_elements_cached = release_config.get(
392-
UNNAMED_SECTION, "GARDENLINUX_FEATURES_ELEMENTS"
393-
).split(",")
430+
self._feature_elements_cached = (
431+
release_config.get(UNNAMED_SECTION, "GARDENLINUX_FEATURES_ELEMENTS")
432+
.strip("\"'")
433+
.split(",")
434+
)
394435

395-
self._feature_flags_cached = release_config.get(
396-
UNNAMED_SECTION, "GARDENLINUX_FEATURES_FLAGS"
397-
).split(",")
436+
self._feature_flags_cached = (
437+
release_config.get(UNNAMED_SECTION, "GARDENLINUX_FEATURES_FLAGS")
438+
.strip("\"'")
439+
.split(",")
440+
)
398441

399442
self._feature_platform_cached = release_config.get(
400443
UNNAMED_SECTION, "GARDENLINUX_FEATURES_PLATFORM"
401-
)
444+
).strip("\"'")
445+
446+
if release_config.has_option(UNNAMED_SECTION, "GARDENLINUX_PLATFORM_VARIANT"):
447+
self._platform_variant_cached = release_config.get(
448+
UNNAMED_SECTION, "GARDENLINUX_PLATFORM_VARIANT"
449+
).strip("\"'")
402450

403451
def save_to_release_file(
404452
self, release_file: PathLike | str, overwrite: Optional[bool] = False

tests/features/constants.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from gardenlinux.constants import (
2+
GL_BUG_REPORT_URL,
3+
GL_DISTRIBUTION_NAME,
4+
GL_HOME_URL,
5+
GL_RELEASE_ID,
6+
GL_SUPPORT_URL,
7+
)
8+
9+
10+
def generate_container_amd64_release_metadata(version, commit_hash):
11+
return f"""
12+
ID={GL_RELEASE_ID}
13+
NAME="{GL_DISTRIBUTION_NAME}"
14+
PRETTY_NAME="{GL_DISTRIBUTION_NAME} {version}"
15+
IMAGE_VERSION={version}
16+
VARIANT_ID="container-amd64"
17+
HOME_URL="{GL_HOME_URL}"
18+
SUPPORT_URL="{GL_SUPPORT_URL}"
19+
BUG_REPORT_URL="{GL_BUG_REPORT_URL}"
20+
GARDENLINUX_CNAME="container-amd64-{version}-{commit_hash}"
21+
GARDENLINUX_FEATURES="_slim,base,container"
22+
GARDENLINUX_FEATURES_PLATFORM="container"
23+
GARDENLINUX_FEATURES_ELEMENTS="base"
24+
GARDENLINUX_FEATURES_FLAGS="_slim"
25+
GARDENLINUX_PLATFORM_VARIANT=""
26+
GARDENLINUX_VERSION="{version}"
27+
GARDENLINUX_COMMIT_ID="{commit_hash}"
28+
GARDENLINUX_COMMIT_ID_LONG="{commit_hash}"
29+
""".strip()

tests/features/test_main.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import sys
22
import types
3+
from pathlib import Path
4+
from tempfile import TemporaryDirectory
35

46
import pytest
57

68
import gardenlinux.features.__main__ as fema
7-
from gardenlinux.features import CName, Parser
9+
from gardenlinux.features import CName
810

911
from ..constants import GL_ROOT_DIR
12+
from .constants import generate_container_amd64_release_metadata
1013

1114
# -------------------------------
1215
# Helper function tests
@@ -363,3 +366,32 @@ def test_main_with_exclude_cname_print_features(monkeypatch, capsys):
363366
"sap,ssh,_fwcfg,_ignite,_legacy,_nopkg,_prod,_slim,base,server,cloud,kvm,multipath,iscsi,nvme,gardener"
364367
== captured
365368
)
369+
370+
371+
def test_cname_release_file(monkeypatch, capsys):
372+
"""
373+
Test validation between release metadata and arguments given
374+
"""
375+
# Arrange
376+
with TemporaryDirectory() as tmpdir:
377+
os_release_file = Path(tmpdir, "os_release")
378+
379+
with os_release_file.open("w") as fp:
380+
fp.write(generate_container_amd64_release_metadata("today", "local"))
381+
382+
argv = [
383+
"prog",
384+
"--cname",
385+
"container-amd64-today-local",
386+
"--release-file",
387+
str(os_release_file),
388+
"cname",
389+
]
390+
monkeypatch.setattr(sys, "argv", argv)
391+
392+
# Act / Assert
393+
fema.main()
394+
395+
# Assert
396+
out = capsys.readouterr().out
397+
assert "container-amd64-today-local" in out

tests/features/test_metadata_main.py

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,8 @@
55
import pytest
66

77
import gardenlinux.features.metadata_main as metadata_main
8-
from gardenlinux.constants import (
9-
GL_BUG_REPORT_URL,
10-
GL_DISTRIBUTION_NAME,
11-
GL_HOME_URL,
12-
GL_RELEASE_ID,
13-
GL_SUPPORT_URL,
14-
)
15-
16-
17-
def get_container_amd64_release_metadata(version, commit_hash):
18-
return f"""
19-
ID={GL_RELEASE_ID}
20-
NAME="{GL_DISTRIBUTION_NAME}"
21-
PRETTY_NAME="{GL_DISTRIBUTION_NAME} today"
22-
IMAGE_VERSION=today
23-
VARIANT_ID="container-amd64"
24-
HOME_URL="{GL_HOME_URL}"
25-
SUPPORT_URL="{GL_SUPPORT_URL}"
26-
BUG_REPORT_URL="{GL_BUG_REPORT_URL}"
27-
GARDENLINUX_CNAME="container-amd64-today-local"
28-
GARDENLINUX_FEATURES="_slim,base,container"
29-
GARDENLINUX_FEATURES_PLATFORM="container"
30-
GARDENLINUX_FEATURES_ELEMENTS="base"
31-
GARDENLINUX_FEATURES_FLAGS="_slim"
32-
GARDENLINUX_VERSION="today"
33-
GARDENLINUX_COMMIT_ID="local"
34-
GARDENLINUX_COMMIT_ID_LONG="local"
35-
""".strip()
8+
9+
from .constants import generate_container_amd64_release_metadata
3610

3711

3812
def test_main_output(monkeypatch, capsys):
@@ -56,7 +30,7 @@ def test_main_output(monkeypatch, capsys):
5630
metadata_main.main()
5731

5832
# Assert
59-
expected = get_container_amd64_release_metadata("today", "local")
33+
expected = generate_container_amd64_release_metadata("today", "local")
6034
assert expected == capsys.readouterr().out.strip()
6135

6236

@@ -85,7 +59,7 @@ def test_main_write(monkeypatch, capsys):
8559
metadata_main.main()
8660

8761
# Assert
88-
expected = get_container_amd64_release_metadata("today", "local")
62+
expected = generate_container_amd64_release_metadata("today", "local")
8963
assert expected == os_release_file.open("r").read()
9064

9165

@@ -98,7 +72,7 @@ def test_main_validation(monkeypatch):
9872
os_release_file = Path(tmpdir, "os_release")
9973

10074
with os_release_file.open("w") as fp:
101-
fp.write(get_container_amd64_release_metadata("today", "local"))
75+
fp.write(generate_container_amd64_release_metadata("today", "local"))
10276

10377
argv = [
10478
"prog",
@@ -115,5 +89,5 @@ def test_main_validation(monkeypatch):
11589
monkeypatch.setattr(sys, "argv", argv)
11690

11791
# Act / Assert
118-
with pytest.raises(AssertionError):
92+
with pytest.raises(RuntimeError):
11993
metadata_main.main()

0 commit comments

Comments
 (0)