Skip to content

Commit 68a68ba

Browse files
committed
Adapt unit tests for GardenLinux feature changes
Signed-off-by: Tobias Wolf <[email protected]> On-behalf-of: SAP <[email protected]>
1 parent f930e54 commit 68a68ba

File tree

13 files changed

+302
-291
lines changed

13 files changed

+302
-291
lines changed

src/gardenlinux/features/__main__.py

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import logging
1010
import os
1111
import re
12-
from functools import reduce
1312
from os import path
1413
from typing import Any, List, Set
1514

@@ -167,21 +166,6 @@ def main() -> None:
167166
print(f"{version}-{commit_id_or_hash[:8]}")
168167

169168

170-
def get_flavor(sorted_features: List[str]):
171-
"""
172-
Get the base cname for the feature set given.
173-
174-
:param sorted_features: Sorted feature set
175-
176-
:return: (str) Base cname
177-
:since: 0.7.0
178-
"""
179-
180-
return reduce(
181-
lambda a, b: a + ("-" if not b.startswith("_") else "") + b, sorted_features
182-
)
183-
184-
185169
def get_version_and_commit_id_from_files(gardenlinux_root: str) -> tuple[str, str]:
186170
"""
187171
Returns the version and commit ID based on files in the GardenLinux root directory.
@@ -287,9 +271,9 @@ def print_output_from_features_parser(
287271
sorted_features = Parser.sort_graph_nodes(graph)
288272
minimal_feature_set = get_minimal_feature_set(graph)
289273

290-
sorted_minimal_features = sort_subset(minimal_feature_set, sorted_features)
274+
sorted_minimal_features = Parser.subset(minimal_feature_set, sorted_features)
291275

292-
cname_base = get_flavor(sorted_minimal_features)
276+
cname_base = Parser.get_flavor_from_feature_set(sorted_minimal_features)
293277

294278
if output_type == "cname_base":
295279
print(cname_base)
@@ -325,10 +309,21 @@ def print_output_from_cname(output_type: str, cname_instance: CName) -> None:
325309
:since: 0.11.0
326310
"""
327311

328-
if output_type == "cname_base":
329-
print(cname_instance.flavor)
330-
elif output_type == "cname":
331-
print(cname_instance.cname)
312+
if output_type in ("cname_base", "cname", "flavor"):
313+
sorted_features = Parser.get_flavor_as_feature_set(cname_instance.flavor)
314+
flavor = Parser.get_flavor_from_feature_set(sorted_features)
315+
316+
if output_type in ("cname_base", "flavor"):
317+
print(flavor)
318+
else:
319+
if cname_instance.version_and_commit_id is None:
320+
raise RuntimeError(
321+
"Version and commit ID can't be provided without appropriate input."
322+
)
323+
324+
print(
325+
f"{flavor}-{cname_instance.arch}-{cname_instance.version_and_commit_id}"
326+
)
332327
elif output_type == "container_name":
333328
print(RE_CAMEL_CASE_SPLITTER.sub("\\1-\\2", cname_instance.flavor).lower())
334329
elif output_type == "platform":
@@ -341,19 +336,5 @@ def print_output_from_cname(output_type: str, cname_instance: CName) -> None:
341336
print(cname_instance.feature_set_flag)
342337

343338

344-
def sort_subset(input_set: Set[str], order_list: List[str]) -> List[str]:
345-
"""
346-
Returns items from `order_list` if given in `input_set`.
347-
348-
:param input_set: Set of values
349-
:param order_list: networkx.Digraph
350-
351-
:return: (str) mermaid.js representation
352-
:since: 0.7.0
353-
"""
354-
355-
return [item for item in order_list if item in input_set]
356-
357-
358339
if __name__ == "__main__":
359340
main()

src/gardenlinux/features/cname.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ 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._flag_multiple_platforms = bool(environ.get("GL_ALLOW_FRANKENSTEIN", False))
56+
57+
self._flag_multiple_platforms = bool(
58+
environ.get("GL_ALLOW_FRANKENSTEIN", False)
59+
)
60+
5761
self._flavor = None
5862
self._version = None
5963

@@ -242,7 +246,8 @@ def feature_set_platform(self) -> str:
242246
if self._flag_multiple_platforms:
243247
return ",".join(platforms)
244248

245-
assert len(platforms) < 2; "Only one platform is supported"
249+
assert len(platforms) < 2
250+
"Only one platform is supported"
246251
return platforms[0]
247252

248253
@property
@@ -257,7 +262,8 @@ def release_metadata_string(self) -> str:
257262
features = Parser().filter_as_dict(self.flavor)
258263

259264
if not self._flag_multiple_platforms:
260-
assert len(features["platform"]) < 2; "Only one platform is supported"
265+
assert len(features["platform"]) < 2
266+
"Only one platform is supported"
261267

262268
elements = ",".join(features["element"])
263269
flags = ",".join(features["flag"])

src/gardenlinux/features/cname_main.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@
1111
from os.path import basename, dirname
1212

1313
from .__main__ import (
14-
get_flavor,
1514
get_minimal_feature_set,
1615
get_version_and_commit_id_from_files,
17-
sort_subset,
1816
)
1917
from .cname import CName
2018
from .parser import Parser
@@ -78,9 +76,9 @@ def main():
7876
sorted_features = Parser.sort_graph_nodes(graph)
7977
minimal_feature_set = get_minimal_feature_set(graph)
8078

81-
sorted_minimal_features = sort_subset(minimal_feature_set, sorted_features)
79+
sorted_minimal_features = Parser.subset(minimal_feature_set, sorted_features)
8280

83-
generated_cname = get_flavor(sorted_minimal_features)
81+
generated_cname = Parser.get_flavor_from_feature_set(sorted_minimal_features)
8482

8583
generated_cname += f"-{cname.arch}"
8684

src/gardenlinux/features/parser.py

Lines changed: 57 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66

77
import logging
88
import os
9+
from functools import reduce
910
from glob import glob
1011
from pathlib import Path
11-
from typing import Callable, Optional, cast
12+
from typing import Callable, List, Optional, Set
1213

1314
import networkx
1415
import yaml
@@ -131,7 +132,7 @@ def filter(
131132
:since: 0.7.0
132133
"""
133134

134-
feature_set = Parser.get_cname_as_feature_set(cname)
135+
feature_set = Parser.get_flavor_as_feature_set(cname)
135136

136137
return self.filter_based_on_feature_set(
137138
feature_set, ignore_excludes, additional_filter_func
@@ -282,11 +283,11 @@ def filter_based_on_feature_set(
282283
self.graph.add_node("libc", content=BARE_FLAVOR_LIBC_FEATURE_CONTENT)
283284

284285
for feature in feature_set:
285-
filter_set.update(
286-
networkx.descendants(
287-
Parser._get_graph_view_for_attr(self.graph, "include"), feature
288-
)
289-
)
286+
for node in networkx.descendants(
287+
Parser._get_graph_view_for_attr(self.graph, "include"), feature
288+
):
289+
if node not in filter_set:
290+
filter_set.append(node)
290291

291292
graph = networkx.subgraph_view(
292293
self.graph,
@@ -310,9 +311,7 @@ def _exclude_from_filter_set(graph, feature_set, filter_set):
310311
:since: 0.7.0
311312
"""
312313

313-
exclude_graph_view = cast(
314-
networkx.DiGraph, Parser._get_graph_view_for_attr(graph, "exclude")
315-
)
314+
exclude_graph_view = Parser._get_graph_view_for_attr(graph, "exclude")
316315
exclude_list = []
317316

318317
for node in networkx.lexicographical_topological_sort(graph):
@@ -362,18 +361,48 @@ def _read_feature_yaml(self, feature_yaml_file: str):
362361
return {"name": name, "content": content}
363362

364363
@staticmethod
365-
def get_cname_as_feature_set(cname):
364+
def get_flavor_from_feature_set(sorted_features: List[str]):
365+
"""
366+
Get the base cname for the feature set given.
367+
368+
:param sorted_features: Sorted feature set
369+
370+
:return: (str) Base cname
371+
:since: 0.7.0
372+
"""
373+
374+
return reduce(
375+
lambda a, b: a + ("-" if not b.startswith("_") else "") + b, sorted_features
376+
)
377+
378+
@staticmethod
379+
def get_flavor_as_feature_set(cname):
366380
"""
367381
Returns the features of a given canonical name.
368382
369383
:param cname: Canonical name
370384
371-
:return: (set) Features of the cname
372-
:since: 0.7.0
385+
:return: (list) Features of the cname
386+
:since: 0.11.0
373387
"""
374388

375389
cname = cname.replace("_", "-_")
376-
return set(cname.split("-"))
390+
391+
platform = None
392+
features = []
393+
flags = []
394+
395+
for feature in cname.split("-"):
396+
if platform is None:
397+
platform = feature
398+
continue
399+
400+
if feature[:1] == "_":
401+
flags.append(feature)
402+
else:
403+
features.append(feature)
404+
405+
return [platform] + sorted(features) + sorted(flags)
377406

378407
@staticmethod
379408
def _get_filter_set_callable(filter_set, additional_filter_func):
@@ -473,6 +502,20 @@ def key_function(node):
473502

474503
return list(networkx.lexicographical_topological_sort(graph, key=key_function))
475504

505+
@staticmethod
506+
def subset(input_set: Set[str], order_list: List[str]) -> List[str]:
507+
"""
508+
Returns items from `order_list` if given in `input_set`.
509+
510+
:param input_set: Set of values for filtering
511+
:param order_list: Set of values to be filtered
512+
513+
:return: (list) Subset
514+
:since: 0.11.0
515+
"""
516+
517+
return [item for item in order_list if item in input_set]
518+
476519
@staticmethod
477520
def sort_reversed_graph_nodes(graph):
478521
"""

test-data/gardenlinux

Submodule gardenlinux updated 757 files

tests/features/test_cname_main.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import pytest
66

77
import gardenlinux.features.cname_main as cname_main
8+
from gardenlinux.features import Parser
89

910

1011
def test_main_happy(monkeypatch, capsys):
@@ -19,7 +20,7 @@ class FakeGraph:
1920
in_degree = lambda self: [("f1", 0)]
2021
edges = [("f1", "f2")]
2122

22-
class FakeParser:
23+
class FakeParser(Parser):
2324
def __init__(self, *a, **k):
2425
pass
2526

@@ -55,7 +56,7 @@ def test_main_version_from_file(monkeypatch, capsys):
5556
lambda root: ("2.0", "abcdef12"),
5657
)
5758

58-
class FakeParser:
59+
class FakeParser(Parser):
5960
def __init__(self, *a, **k):
6061
pass
6162

@@ -96,7 +97,7 @@ def raise_runtime(_):
9697
)
9798

9899
# Patch Parser for minimal valid graph
99-
class FakeParser:
100+
class FakeParser(Parser):
100101
def __init__(self, *a, **k):
101102
pass
102103

tests/features/test_features_parser.py

Lines changed: 0 additions & 71 deletions
This file was deleted.

0 commit comments

Comments
 (0)