11
11
# See the License for the specific language governing permissions and limitations under the License.
12
12
13
13
import logging
14
+ import os
14
15
15
16
import boto3
16
17
import pytest
18
+ from jinja2 import FileSystemLoader
19
+ from jinja2 .sandbox import SandboxedEnvironment
20
+
21
+ from conftest import _get_default_template_values , inject_additional_config_settings , \
22
+ inject_additional_image_configs_settings
17
23
18
24
OSS_REQUIRING_EXTRA_DEPS = ["alinux2023" , "rhel8" , "rocky8" ]
19
25
NUMBER_OF_NODES = [8 , 16 , 32 ]
@@ -39,3 +45,55 @@ def _shared_performance_test_cluster(instance, os, region, scheduler):
39
45
return cluster
40
46
41
47
return _shared_performance_test_cluster
48
+
49
+
50
+ @pytest .fixture (scope = "class" )
51
+ def shared_test_datadir (request , datadir ):
52
+ """
53
+ Inject the datadir with resources for the specific test function.
54
+
55
+ If the test function is declared in a class then datadir is ClassName/FunctionName
56
+ otherwise it is only FunctionName.
57
+ """
58
+ function_name = request .function .__name__
59
+ if not request .cls :
60
+ return datadir / function_name
61
+
62
+ class_name = request .cls .__name__
63
+ return datadir / "{0}/{1}" .format (class_name , function_name )
64
+
65
+
66
+ @pytest .fixture (scope = "class" )
67
+ def shared_pcluster_config_reader (test_datadir , vpc_stack , request , region ):
68
+ """
69
+ Define a fixture to render pcluster config templates associated to the running test.
70
+
71
+ The config for a given test is a pcluster.config.yaml file stored in the configs_datadir folder.
72
+ The config can be written by using Jinja2 template engine.
73
+ The current renderer already replaces placeholders for current keys:
74
+ {{ region }}, {{ os }}, {{ instance }}, {{ scheduler}}, {{ key_name }},
75
+ {{ vpc_id }}, {{ public_subnet_id }}, {{ private_subnet_id }}, {{ default_vpc_security_group_id }}
76
+ The current renderer injects options for custom templates and packages in case these
77
+ are passed to the cli and not present already in the cluster config.
78
+ Also sanity_check is set to true by default unless explicitly set in config.
79
+
80
+ :return: a _config_renderer(**kwargs) function which gets as input a dictionary of values to replace in the template
81
+ """
82
+
83
+ def _config_renderer (config_file = "pcluster.config.yaml" , benchmarks = None , output_file = None , ** kwargs ):
84
+ config_file_path = test_datadir / config_file
85
+ if not os .path .isfile (config_file_path ):
86
+ raise FileNotFoundError (f"Cluster config file not found in the expected dir { config_file_path } " )
87
+ output_file_path = test_datadir / output_file if output_file else config_file_path
88
+ default_values = _get_default_template_values (vpc_stack , request )
89
+ file_loader = FileSystemLoader (str (test_datadir ))
90
+ env = SandboxedEnvironment (loader = file_loader )
91
+ rendered_template = env .get_template (config_file ).render (** {** default_values , ** kwargs })
92
+ output_file_path .write_text (rendered_template )
93
+ if not config_file .endswith ("image.config.yaml" ):
94
+ inject_additional_config_settings (output_file_path , request , region , benchmarks )
95
+ else :
96
+ inject_additional_image_configs_settings (output_file_path , request )
97
+ return output_file_path
98
+
99
+ return _config_renderer
0 commit comments