Skip to content

Commit c8f09e0

Browse files
dervoetirazvan
andauthored
feat: determine namespace from testcase name and provide it as variable (#30)
* feat: determine namespace from testcase name and provide it as variable * fix: only use the hash for the namespace to prevent errors caused by special characters in test case names * docs: for determine_namespace * feat: add --namespace argument for preferred namespace names * changelog --------- Co-authored-by: Razvan-Daniel Mihai <[email protected]>
1 parent 37cfcab commit c8f09e0

File tree

5 files changed

+50
-6
lines changed

5 files changed

+50
-6
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,4 @@ repos:
6363
language: python
6464
'types': [python]
6565
pass_filenames: false
66-
stages: [commit]
66+
stages: [pre-commit]

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ All notable changes to this project will be documented in this file.
1010
- Raise build versions ([#18])
1111
- Use setuptools to build the project ([#18])
1212
- Drop support for 3.10 and add support for 3.12 ([#18])
13+
- Added NAMESPACE to template globals and --namespace cli argument for preferred names ([#30])
1314

1415
[#18]: https://github.com/stackabletech/beku.py/pull/18
16+
[#30]: https://github.com/stackabletech/beku.py/pull/30
1517

1618
## [0.0.9] - 2023-08-23
1719

src/beku/kuttl.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import re
88
from dataclasses import dataclass, field
99
from functools import cached_property
10+
from hashlib import sha256
1011
from itertools import product, chain
1112
from os import walk, path, makedirs
1213
from shutil import copy2
@@ -124,14 +125,15 @@ def tid(self) -> str:
124125
),
125126
)
126127

127-
def expand(self, template_dir: str, target_dir: str) -> None:
128+
def expand(self, template_dir: str, target_dir: str, namespace: str) -> None:
128129
"""Expand test case This will create the target folder, copy files and render render templates."""
129130
logging.info("Expanding test case id [%s]", self.tid)
130131
td_root = path.join(template_dir, self.name)
131132
tc_root = path.join(target_dir, self.name, self.tid)
132133
_mkdir_ignore_exists(tc_root)
133134
test_env = Environment(loader=FileSystemLoader(path.join(template_dir, self.name)), trim_blocks=True)
134135
test_env.globals["lookup"] = ansible_lookup
136+
test_env.globals["NAMESPACE"] = determine_namespace(self.tid, namespace)
135137
sub_level: int = 0
136138
for root, dirs, files in walk(td_root):
137139
sub_level += 1
@@ -313,7 +315,12 @@ class EffectiveTestSuite:
313315

314316

315317
def expand(
316-
suite: str, effective_test_suites: List[EffectiveTestSuite], template_dir: str, output_dir: str, kuttl_tests: str
318+
suite: str,
319+
effective_test_suites: List[EffectiveTestSuite],
320+
template_dir: str,
321+
output_dir: str,
322+
kuttl_tests: str,
323+
namespace: str,
317324
) -> int:
318325
"""Expand test suite."""
319326
try:
@@ -322,12 +329,32 @@ def expand(
322329
_mkdir_ignore_exists(output_dir)
323330
_expand_kuttl_tests(ets.test_cases, output_dir, kuttl_tests)
324331
for test_case in ets.test_cases:
325-
test_case.expand(template_dir, output_dir)
332+
test_case.expand(template_dir, output_dir, namespace)
326333
except StopIteration as exc:
327334
raise ValueError(f"Cannot expand test suite [{suite}] because cannot find it in [{kuttl_tests}]") from exc
328335
return 0
329336

330337

338+
def determine_namespace(testcase_name: str, prefered_namespace: str) -> str:
339+
"""Generate a namespace name for the given test case unless a prefered namespace name is given.
340+
341+
The format of the namespace name is "kuttl-<hash>" where hash is the first 10 chars of the test
342+
case's sha256 value.
343+
344+
There is an analogous function in "kubectl-kuttl" that generates the exact same namespace name.
345+
These two have to be kept in sync!
346+
347+
The tests use the namespace name also for other kubernetes objects like "metadata.name" which have
348+
different syntactic restrictions.
349+
Therefore, to be on the safe side, the namespace name is kept as simple as possible.
350+
"""
351+
if prefered_namespace:
352+
return prefered_namespace
353+
else:
354+
hash = sha256(testcase_name.encode("utf-8")).hexdigest()
355+
return f"kuttl-{hash[:10]}"
356+
357+
331358
def _expand_kuttl_tests(test_cases, output_dir: str, kuttl_tests: str) -> None:
332359
"""Generate the kuttl-tests.yaml file and fill in paths to tests."""
333360
env = Environment(loader=FileSystemLoader(path.dirname(kuttl_tests)))

src/beku/main.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ def parse_cli_args() -> Namespace:
6969
default="default",
7070
)
7171

72+
parser.add_argument(
73+
"-n",
74+
"--namespace",
75+
help="Name of the namespace to use for tests. Default: kuttl-<test name sha256>",
76+
type=str,
77+
required=False,
78+
)
79+
7280
return parser.parse_args()
7381

7482

@@ -86,4 +94,11 @@ def main() -> int:
8694
rmtree(path=cli_args.output_dir, ignore_errors=True)
8795
# Compatibility warning: add 'tests' to output_dir
8896
output_dir = path.join(cli_args.output_dir, "tests")
89-
return expand(cli_args.suite, effective_test_suites, cli_args.template_dir, output_dir, cli_args.kuttl_test)
97+
return expand(
98+
cli_args.suite,
99+
effective_test_suites,
100+
cli_args.template_dir,
101+
output_dir,
102+
cli_args.kuttl_test,
103+
cli_args.namespace,
104+
)

src/beku/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
"""Package version."""
22

3-
__version__ = "0.0.9"
3+
__version__ = "0.0.10"

0 commit comments

Comments
 (0)