@@ -140,26 +140,43 @@ def tid(self) -> str:
140140 else :
141141 return name
142142
143- def expand (self , template_dir : str , target_dir : str , namespace : str ) -> None :
144- """Expand test case This will create the target folder, copy files and render render templates."""
143+ def expand (self , template_dir : str , target_dir : str , namespace : str , common_dir : Optional [str ] = None ) -> None :
144+ """Expand test case. This will create the target folder, copy files and render templates.
145+
146+ The test's own steps (from ``template_dir/<name>``) are rendered first. If ``common_dir`` is
147+ given and exists, its files are then rendered into the SAME test folder, so shared steps (e.g.
148+ a teardown) can live in a single place instead of being copied into every test. Common files
149+ are rendered after the test's own files, so a common file wins on a name collision; use
150+ non-colliding names (e.g. a high step number like ``99-teardown.yaml``).
151+ """
145152 logging .info ("Expanding test case id [%s]" , self .tid )
146- td_root = path .join (template_dir , self .name )
147153 tc_root = path .join (target_dir , self .name , self .tid )
148154 _mkdir_ignore_exists (tc_root )
149- test_env = Environment (loader = FileSystemLoader (path .join (template_dir , self .name )), trim_blocks = True )
150- test_env .globals ["lookup" ] = ansible_lookup
151- test_env .globals ["NAMESPACE" ] = determine_namespace (self .tid , namespace )
155+ namespace = determine_namespace (self .tid , namespace )
156+ self ._render_dir (path .join (template_dir , self .name ), tc_root , namespace )
157+ if common_dir :
158+ if path .isdir (common_dir ):
159+ logging .debug ("Rendering common steps from [%s] into [%s]" , common_dir , tc_root )
160+ self ._render_dir (common_dir , tc_root , namespace )
161+ else :
162+ logging .warning ("Common steps directory [%s] does not exist, skipping" , common_dir )
163+
164+ def _render_dir (self , source_root : str , tc_root : str , namespace : str ) -> None :
165+ """Render/copy every file under ``source_root`` into ``tc_root``, preserving sub-directories."""
166+ env = Environment (loader = FileSystemLoader (source_root ), trim_blocks = True )
167+ env .globals ["lookup" ] = ansible_lookup
168+ env .globals ["NAMESPACE" ] = namespace
152169 sub_level : int = 0
153- for root , dirs , files in walk (td_root ):
170+ for root , dirs , files in walk (source_root ):
154171 sub_level += 1
155172 if sub_level == 8 :
156173 # Sanity check
157174 raise ValueError ("Maximum recursive level (8) reached." )
158175 for dir_name in dirs :
159- _mkdir_ignore_exists (path .join (tc_root , root [len (td_root ) + 1 :], dir_name ))
176+ _mkdir_ignore_exists (path .join (tc_root , root [len (source_root ) + 1 :], dir_name ))
160177 for file_name in files :
161178 test_source = make_test_source_with_context (
162- file_name , root , path .join (tc_root , root [len (td_root ) + 1 :]), test_env , self .values
179+ file_name , root , path .join (tc_root , root [len (source_root ) + 1 :]), env , self .values
163180 )
164181 test_source .build_destination ()
165182
@@ -336,6 +353,7 @@ def expand(
336353 output_dir : str ,
337354 kuttl_tests : str ,
338355 namespace : str ,
356+ common_dir : Optional [str ] = None ,
339357) -> int :
340358 """Expand test suite."""
341359 try :
@@ -344,7 +362,7 @@ def expand(
344362 _mkdir_ignore_exists (output_dir )
345363 _expand_kuttl_tests (ets .test_cases , output_dir , kuttl_tests )
346364 for test_case in ets .test_cases :
347- test_case .expand (template_dir , output_dir , namespace )
365+ test_case .expand (template_dir , output_dir , namespace , common_dir )
348366 except StopIteration as exc :
349367 raise ValueError (f"Cannot expand test suite [{ suite } ] because cannot find it in [{ kuttl_tests } ]" ) from exc
350368 return 0
0 commit comments