77import re
88from dataclasses import dataclass , field
99from functools import cached_property
10+ from hashlib import sha256
1011from itertools import product , chain
1112from os import walk , path , makedirs
1213from 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
315317def 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+
331358def _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 )))
0 commit comments