7
7
import re
8
8
from dataclasses import dataclass , field
9
9
from functools import cached_property
10
+ from hashlib import sha256
10
11
from itertools import product , chain
11
12
from os import walk , path , makedirs
12
13
from shutil import copy2
@@ -124,14 +125,15 @@ def tid(self) -> str:
124
125
),
125
126
)
126
127
127
- def expand (self , template_dir : str , target_dir : str ) -> None :
128
+ def expand (self , template_dir : str , target_dir : str , namespace : str ) -> None :
128
129
"""Expand test case This will create the target folder, copy files and render render templates."""
129
130
logging .info ("Expanding test case id [%s]" , self .tid )
130
131
td_root = path .join (template_dir , self .name )
131
132
tc_root = path .join (target_dir , self .name , self .tid )
132
133
_mkdir_ignore_exists (tc_root )
133
134
test_env = Environment (loader = FileSystemLoader (path .join (template_dir , self .name )), trim_blocks = True )
134
135
test_env .globals ["lookup" ] = ansible_lookup
136
+ test_env .globals ["NAMESPACE" ] = determine_namespace (self .tid , namespace )
135
137
sub_level : int = 0
136
138
for root , dirs , files in walk (td_root ):
137
139
sub_level += 1
@@ -313,7 +315,12 @@ class EffectiveTestSuite:
313
315
314
316
315
317
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 ,
317
324
) -> int :
318
325
"""Expand test suite."""
319
326
try :
@@ -322,12 +329,32 @@ def expand(
322
329
_mkdir_ignore_exists (output_dir )
323
330
_expand_kuttl_tests (ets .test_cases , output_dir , kuttl_tests )
324
331
for test_case in ets .test_cases :
325
- test_case .expand (template_dir , output_dir )
332
+ test_case .expand (template_dir , output_dir , namespace )
326
333
except StopIteration as exc :
327
334
raise ValueError (f"Cannot expand test suite [{ suite } ] because cannot find it in [{ kuttl_tests } ]" ) from exc
328
335
return 0
329
336
330
337
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
+
331
358
def _expand_kuttl_tests (test_cases , output_dir : str , kuttl_tests : str ) -> None :
332
359
"""Generate the kuttl-tests.yaml file and fill in paths to tests."""
333
360
env = Environment (loader = FileSystemLoader (path .dirname (kuttl_tests )))
0 commit comments