Skip to content

Commit 89ddc98

Browse files
committed
feat: add --namespace argument for preferred namespace names
1 parent 02cd155 commit 89ddc98

File tree

3 files changed

+33
-10
lines changed

3 files changed

+33
-10
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]

src/beku/kuttl.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,15 @@ def tid(self) -> str:
125125
),
126126
)
127127

128-
def expand(self, template_dir: str, target_dir: str) -> None:
128+
def expand(self, template_dir: str, target_dir: str, namespace: str) -> None:
129129
"""Expand test case This will create the target folder, copy files and render render templates."""
130130
logging.info("Expanding test case id [%s]", self.tid)
131131
td_root = path.join(template_dir, self.name)
132132
tc_root = path.join(target_dir, self.name, self.tid)
133133
_mkdir_ignore_exists(tc_root)
134134
test_env = Environment(loader=FileSystemLoader(path.join(template_dir, self.name)), trim_blocks=True)
135135
test_env.globals["lookup"] = ansible_lookup
136-
test_env.globals["NAMESPACE"] = determine_namespace(self.tid)
136+
test_env.globals["NAMESPACE"] = determine_namespace(self.tid, namespace)
137137
sub_level: int = 0
138138
for root, dirs, files in walk(td_root):
139139
sub_level += 1
@@ -315,7 +315,12 @@ class EffectiveTestSuite:
315315

316316

317317
def expand(
318-
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,
319324
) -> int:
320325
"""Expand test suite."""
321326
try:
@@ -324,14 +329,14 @@ def expand(
324329
_mkdir_ignore_exists(output_dir)
325330
_expand_kuttl_tests(ets.test_cases, output_dir, kuttl_tests)
326331
for test_case in ets.test_cases:
327-
test_case.expand(template_dir, output_dir)
332+
test_case.expand(template_dir, output_dir, namespace)
328333
except StopIteration as exc:
329334
raise ValueError(f"Cannot expand test suite [{suite}] because cannot find it in [{kuttl_tests}]") from exc
330335
return 0
331336

332337

333-
def determine_namespace(testcase_name: str) -> str:
334-
"""Generate a namespace name for the given test case.
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.
335340
336341
The format of the namespace name is "kuttl-<hash>" where hash is the first 10 chars of the test
337342
case's sha256 value.
@@ -343,8 +348,11 @@ def determine_namespace(testcase_name: str) -> str:
343348
different syntactic restrictions.
344349
Therefore, to be on the safe side, the namespace name is kept as simple as possible.
345350
"""
346-
hash = sha256(testcase_name.encode("utf-8")).hexdigest()
347-
return f"kuttl-{hash[:10]}"
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]}"
348356

349357

350358
def _expand_kuttl_tests(test_cases, output_dir: str, kuttl_tests: str) -> None:

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+
)

0 commit comments

Comments
 (0)