6
6
import shutil
7
7
import subprocess
8
8
import sys
9
- import tempfile
10
9
from pathlib import Path
11
10
from typing import Any , Dict
12
11
12
+ import boto3
13
+
14
+ from samtranslator .translator .arn_generator import ArnGenerator
15
+ from samtranslator .translator .managed_policy_translator import ManagedPolicyLoader
16
+ from samtranslator .translator .transform import transform
17
+ from samtranslator .yaml_helper import yaml_parse
18
+
13
19
SCRIPT_DIR = os .path .dirname (os .path .realpath (__file__ ))
14
20
TRANSFORM_TEST_DIR = os .path .join (SCRIPT_DIR , ".." , "tests" , "translator" )
15
21
22
+ iam_client = boto3 .client ("iam" )
23
+
16
24
parser = argparse .ArgumentParser (description = __doc__ )
17
25
parser .add_argument (
18
26
"--template-file" ,
@@ -40,7 +48,7 @@ def read_json_file(file_path: str) -> Dict[str, Any]:
40
48
41
49
def write_json_file (obj : Dict [str , Any ], file_path : str ) -> None :
42
50
with open (file_path , "w" , encoding = "utf-8" ) as f :
43
- json .dump (obj , f , indent = 2 )
51
+ json .dump (obj , f , indent = 2 , sort_keys = True )
44
52
45
53
46
54
def add_regional_endpoint_configuration_if_needed (template : Dict [str , Any ]) -> Dict [str , Any ]:
@@ -66,38 +74,28 @@ def replace_aws_partition(partition: str, file_path: str) -> None:
66
74
def generate_transform_test_output_files (input_file_path : str , file_basename : str ) -> None :
67
75
output_file_option = file_basename + ".json"
68
76
69
- # run sam-translate.py and get the temporary output file
70
- with tempfile .NamedTemporaryFile () as temp_output_file :
71
- subprocess .run (
72
- [
73
- sys .executable ,
74
- os .path .join (SCRIPT_DIR , "sam-translate.py" ),
75
- "--template-file" ,
76
- input_file_path ,
77
- "--output-template" ,
78
- temp_output_file .name ,
79
- ],
80
- check = True ,
81
- )
77
+ with open (os .path .join (input_file_path ), "r" ) as f :
78
+ manifest = yaml_parse (f ) # type: ignore[no-untyped-call]
79
+
80
+ transform_test_output_paths = {
81
+ "aws" : ("us-west-2" , os .path .join (TRANSFORM_TEST_DIR , "output" , output_file_option )),
82
+ "aws-cn" : ("cn-north-1 " , os .path .join (TRANSFORM_TEST_DIR , "output/aws-cn/" , output_file_option )),
83
+ "aws-us-gov" : ("us-gov-west-1" , os .path .join (TRANSFORM_TEST_DIR , "output/aws-us-gov/" , output_file_option )),
84
+ }
82
85
83
- # copy the output files into correct directories
84
- transform_test_output_path = os .path .join (TRANSFORM_TEST_DIR , "output" , output_file_option )
85
- shutil .copyfile (temp_output_file .name , transform_test_output_path )
86
+ for partition , (region , output_path ) in transform_test_output_paths .items ():
87
+ # Set Boto Session Region to guarantee the same hash input as transform tests for API deployment id
88
+ ArnGenerator .BOTO_SESSION_REGION_NAME = region
89
+ output_fragment = transform (manifest , {}, ManagedPolicyLoader (iam_client ))
86
90
87
- regional_transform_test_output_paths = {
88
- "aws-cn" : os .path .join (TRANSFORM_TEST_DIR , "output/aws-cn/" , output_file_option ),
89
- "aws-us-gov" : os .path .join (TRANSFORM_TEST_DIR , "output/aws-us-gov/" , output_file_option ),
90
- }
91
+ if not CLI_OPTIONS .disable_api_configuration and partition != "aws" :
92
+ output_fragment = add_regional_endpoint_configuration_if_needed (output_fragment )
91
93
92
- if not CLI_OPTIONS .disable_api_configuration :
93
- template = read_json_file (temp_output_file .name )
94
- template = add_regional_endpoint_configuration_if_needed (template )
95
- write_json_file (template , temp_output_file .name )
94
+ write_json_file (output_fragment , output_path )
96
95
97
- for partition , output_path in regional_transform_test_output_paths .items ():
98
- shutil .copyfile (temp_output_file .name , output_path )
99
- if not CLI_OPTIONS .disable_update_partition :
100
- replace_aws_partition (partition , output_path )
96
+ # Update arn partition if necessary
97
+ if not CLI_OPTIONS .disable_update_partition :
98
+ replace_aws_partition (partition , output_path )
101
99
102
100
103
101
def get_input_file_path () -> str :
@@ -118,6 +116,18 @@ def verify_input_template(input_file_path: str): # type: ignore[no-untyped-def]
118
116
)
119
117
120
118
119
+ def format_test_files () -> None :
120
+ subprocess .run (
121
+ [sys .executable , os .path .join (SCRIPT_DIR , "json-format.py" ), "--write" , "tests" ],
122
+ check = True ,
123
+ )
124
+
125
+ subprocess .run (
126
+ [sys .executable , os .path .join (SCRIPT_DIR , "yaml-format.py" ), "--write" , "tests" ],
127
+ check = True ,
128
+ )
129
+
130
+
121
131
def main () -> None :
122
132
input_file_path = get_input_file_path ()
123
133
file_basename = Path (input_file_path ).stem
@@ -133,6 +143,8 @@ def main() -> None:
133
143
"Generating transform test input and output files complete. \n \n Please check the generated output is as expected. This tool does not guarantee correct output."
134
144
)
135
145
146
+ format_test_files ()
147
+
136
148
137
149
if __name__ == "__main__" :
138
150
main ()
0 commit comments