Skip to content

Commit 8ea4916

Browse files
authored
Add logging for canary auto generation and refactor canarySettings (#1078)
* Add logging for canary auto generation * Refactor canarySettings
1 parent d17ac7d commit 8ea4916

File tree

2 files changed

+24
-43
lines changed

2 files changed

+24
-43
lines changed

src/rpdk/core/project.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,9 @@
7777
TYPE_NAME = "typeName"
7878
CONTRACT_TEST_FILE_NAMES = "contract_test_file_names"
7979
INPUT1_FILE_NAME = "inputs_1.json"
80-
FILE_GENERATION_ENABLED = "file_generation_enabled"
8180
CONTRACT_TEST_FOLDER = "contract-tests-artifacts"
8281
CONTRACT_TEST_INPUT_PREFIX = "inputs_*"
8382
CONTRACT_TEST_DEPENDENCY_FILE_NAME = "dependencies.yml"
84-
FILE_GENERATION_ENABLED = "file_generation_enabled"
8583
TYPE_NAME = "typeName"
8684
CONTRACT_TEST_FILE_NAMES = "contract_test_file_names"
8785
FN_SUB = "Fn::Sub"
@@ -181,6 +179,7 @@ def __init__(self, overwrite_enabled=False, root=None):
181179
self.executable_entrypoint = None
182180
self.fragment_dir = None
183181
self.canary_settings = {}
182+
self.has_canary_settings = None
184183
self.target_info = {}
185184

186185
self.env = Environment(
@@ -257,7 +256,9 @@ def rpdk_config(self):
257256

258257
@property
259258
def file_generation_enabled(self):
260-
return self.canary_settings.get(FILE_GENERATION_ENABLED, False)
259+
if self.has_canary_settings is False:
260+
return False
261+
return True
261262

262263
@property
263264
def contract_test_file_names(self):
@@ -338,6 +339,10 @@ def validate_and_load_resource_settings(self, raw_settings):
338339
self._plugin = load_plugin(raw_settings["language"])
339340
self.settings = raw_settings.get("settings", {})
340341
self.canary_settings = raw_settings.get("canarySettings", {})
342+
if raw_settings.get("canarySettings", False) is False:
343+
self.has_canary_settings = False
344+
else:
345+
self.has_canary_settings = True
341346

342347
def _write_example_schema(self):
343348
self.schema = resource_json(
@@ -454,7 +459,6 @@ def init(self, type_name, language, settings=None):
454459
self._plugin = load_plugin(language)
455460
self.settings = settings or {}
456461
self.canary_settings = {
457-
FILE_GENERATION_ENABLED: True,
458462
CONTRACT_TEST_FILE_NAMES: [INPUT1_FILE_NAME],
459463
}
460464
self._write_example_schema()
@@ -1323,9 +1327,16 @@ def generate_canary_files(self) -> None:
13231327
not self.file_generation_enabled
13241328
or not Path(self.target_contract_test_folder_path).exists()
13251329
):
1330+
LOG.info("Skipping Canary Auto-Generation")
13261331
return
1332+
LOG.info("Starting Canary Auto-Generation...")
1333+
if self.file_generation_enabled and self.canary_settings == {}:
1334+
LOG.warning(
1335+
"canarySettings are provided but empty. Generation is enabled with default settings."
1336+
)
13271337
self._setup_stack_template_environment()
13281338
self._generate_stack_template_files()
1339+
LOG.info("Finished Canary Auto-Generation")
13291340

13301341
def _setup_stack_template_environment(self) -> None:
13311342
stack_template_root = Path(self.target_canary_root_path)
@@ -1337,7 +1348,12 @@ def _setup_stack_template_environment(self) -> None:
13371348
)
13381349
bootstrap_file = stack_template_root / CANARY_DEPENDENCY_FILE_NAME
13391350
if dependencies_file.exists():
1351+
LOG.debug("Writing: %s", bootstrap_file)
13401352
shutil.copy(str(dependencies_file), str(bootstrap_file))
1353+
else:
1354+
LOG.debug(
1355+
"Not found: %s. Not writing to: %s", dependencies_file, bootstrap_file
1356+
)
13411357

13421358
def _generate_stack_template_files(self) -> None:
13431359
stack_template_folder = Path(self.target_canary_folder_path)
@@ -1349,6 +1365,7 @@ def _generate_stack_template_files(self) -> None:
13491365
]
13501366
contract_test_files = sorted(contract_test_files)
13511367
for count, ct_file in enumerate(contract_test_files, start=1):
1368+
LOG.debug("Loading contract test input file: %s", ct_file)
13521369
with ct_file.open("r") as f:
13531370
json_data = json.load(f)
13541371
resource_name = self.type_info[2]
@@ -1398,6 +1415,7 @@ def _save_stack_template_data(
13981415
f"{CANARY_FILE_PREFIX}{contract_test_input_count}_{suffix}.yaml"
13991416
)
14001417
stack_template_file_path = stack_template_folder / stack_template_file_name
1418+
LOG.debug("Writing Canary Stack Template File: %s", stack_template_file_path)
14011419
with stack_template_file_path.open("w") as stack_template_file:
14021420
yaml.dump(stack_template_data, stack_template_file, indent=2)
14031421

tests/test_project.py

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
CONTRACT_TEST_DEPENDENCY_FILE_NAME,
4040
CONTRACT_TEST_FILE_NAMES,
4141
CONTRACT_TEST_FOLDER,
42-
FILE_GENERATION_ENABLED,
4342
OVERRIDES_FILENAME,
4443
SCHEMA_UPLOAD_FILENAME,
4544
SETTINGS_FILENAME,
@@ -2796,7 +2795,6 @@ def test_generate_canary_files(project):
27962795
"futureProperty": "value",
27972796
"typeName": "AWS::Example::Resource",
27982797
"canarySettings": {
2799-
FILE_GENERATION_ENABLED: True,
28002798
CONTRACT_TEST_FILE_NAMES: ["inputs_1.json", "inputs_2.json"],
28012799
},
28022800
}
@@ -2850,7 +2848,6 @@ def test_create_template_file(mock_yaml_dump, project):
28502848
"futureProperty": "value",
28512849
"typeName": "AWS::Example::Resource",
28522850
"canarySettings": {
2853-
FILE_GENERATION_ENABLED: True,
28542851
CONTRACT_TEST_FILE_NAMES: ["inputs_1.json", "inputs_2.json"],
28552852
},
28562853
}
@@ -2932,30 +2929,6 @@ def setup_rpdk_config(project, rpdk_config):
29322929
(contract_test_folder / CONTRACT_TEST_DEPENDENCY_FILE_NAME).touch()
29332930

29342931

2935-
def test_generate_canary_files_when_not_enabled(project):
2936-
rpdk_config = {
2937-
ARTIFACT_TYPE_RESOURCE: "RESOURCE",
2938-
"language": LANGUAGE,
2939-
"runtime": RUNTIME,
2940-
"entrypoint": None,
2941-
"testEntrypoint": None,
2942-
"futureProperty": "value",
2943-
"typeName": "AWS::Example::Resource",
2944-
"canarySettings": {
2945-
FILE_GENERATION_ENABLED: False,
2946-
"contract_test_file_names": ["inputs_1.json", "inputs_2.json"],
2947-
},
2948-
}
2949-
tmp_path = project.root
2950-
setup_rpdk_config(project, rpdk_config)
2951-
project.generate_canary_files()
2952-
2953-
canary_root_path = tmp_path / TARGET_CANARY_ROOT_FOLDER
2954-
canary_folder_path = tmp_path / TARGET_CANARY_FOLDER
2955-
assert not canary_root_path.exists()
2956-
assert not canary_folder_path.exists()
2957-
2958-
29592932
def test_generate_canary_files_no_canary_settings(project):
29602933
rpdk_config = {
29612934
ARTIFACT_TYPE_RESOURCE: "RESOURCE",
@@ -2986,7 +2959,6 @@ def test_generate_canary_files_empty_input_files(project):
29862959
"futureProperty": "value",
29872960
"typeName": "AWS::Example::Resource",
29882961
"canarySettings": {
2989-
FILE_GENERATION_ENABLED: True,
29902962
"contract_test_file_names": [],
29912963
},
29922964
}
@@ -3018,8 +2990,8 @@ def test_generate_canary_files_empty_canary_settings(project):
30182990
project.generate_canary_files()
30192991
canary_root_path = tmp_path / TARGET_CANARY_ROOT_FOLDER
30202992
canary_folder_path = tmp_path / TARGET_CANARY_FOLDER
3021-
assert not canary_root_path.exists()
3022-
assert not canary_folder_path.exists()
2993+
assert canary_root_path.exists()
2994+
assert canary_folder_path.exists()
30232995

30242996

30252997
def _get_mock_yaml_dump_call_arg(
@@ -3063,7 +3035,6 @@ def test_generate_canary_files_with_patch_inputs(mock_yaml_dump, project):
30633035
"futureProperty": "value",
30643036
"typeName": "AWS::Example::Resource",
30653037
"canarySettings": {
3066-
FILE_GENERATION_ENABLED: True,
30673038
CONTRACT_TEST_FILE_NAMES: ["inputs_1.json", "inputs_2.json"],
30683039
},
30693040
}
@@ -3144,7 +3115,6 @@ def test_create_template_file_with_patch_inputs(mock_yaml_dump, project):
31443115
"futureProperty": "value",
31453116
"typeName": "AWS::Example::Resource",
31463117
"canarySettings": {
3147-
FILE_GENERATION_ENABLED: True,
31483118
CONTRACT_TEST_FILE_NAMES: ["inputs_1.json", "inputs_2.json"],
31493119
},
31503120
}
@@ -3246,7 +3216,6 @@ def test_create_template_file_by_list_index(mock_yaml_dump, project):
32463216
"futureProperty": "value",
32473217
"typeName": "AWS::Example::Resource",
32483218
"canarySettings": {
3249-
FILE_GENERATION_ENABLED: True,
32503219
CONTRACT_TEST_FILE_NAMES: ["inputs_1.json", "inputs_2.json"],
32513220
},
32523221
}
@@ -3324,7 +3293,6 @@ def test_create_template_file_with_skipped_patch_operation(mock_yaml_dump, proje
33243293
"futureProperty": "value",
33253294
"typeName": "AWS::Example::Resource",
33263295
"canarySettings": {
3327-
FILE_GENERATION_ENABLED: True,
33283296
CONTRACT_TEST_FILE_NAMES: ["inputs_1.json", "inputs_2.json"],
33293297
},
33303298
}
@@ -3403,7 +3371,6 @@ def test_create_template_file_with_patch_inputs_missing_from_create(
34033371
"futureProperty": "value",
34043372
"typeName": "AWS::Example::Resource",
34053373
"canarySettings": {
3406-
FILE_GENERATION_ENABLED: True,
34073374
CONTRACT_TEST_FILE_NAMES: ["inputs_1.json", "inputs_2.json"],
34083375
},
34093376
}
@@ -3499,7 +3466,6 @@ def test_create_template_file_throws_error_with_invalid_path(mock_yaml_dump, pro
34993466
"futureProperty": "value",
35003467
"typeName": "AWS::Example::Resource",
35013468
"canarySettings": {
3502-
FILE_GENERATION_ENABLED: True,
35033469
CONTRACT_TEST_FILE_NAMES: ["inputs_1.json", "inputs_2.json"],
35043470
},
35053471
}
@@ -3555,7 +3521,6 @@ def test_create_template_file_with_nested_replace_patch_inputs(mock_yaml_dump, p
35553521
"futureProperty": "value",
35563522
"typeName": "AWS::Example::Resource",
35573523
"canarySettings": {
3558-
FILE_GENERATION_ENABLED: True,
35593524
CONTRACT_TEST_FILE_NAMES: ["inputs_1.json", "inputs_2.json"],
35603525
},
35613526
}
@@ -3661,7 +3626,6 @@ def test_create_template_file_with_nested_remove_patch_inputs(mock_yaml_dump, pr
36613626
"futureProperty": "value",
36623627
"typeName": "AWS::Example::Resource",
36633628
"canarySettings": {
3664-
FILE_GENERATION_ENABLED: True,
36653629
CONTRACT_TEST_FILE_NAMES: ["inputs_1.json", "inputs_2.json"],
36663630
},
36673631
}
@@ -3760,7 +3724,6 @@ def test_create_template_file_with_nested_add_patch_inputs(mock_yaml_dump, proje
37603724
"futureProperty": "value",
37613725
"typeName": "AWS::Example::Resource",
37623726
"canarySettings": {
3763-
FILE_GENERATION_ENABLED: True,
37643727
CONTRACT_TEST_FILE_NAMES: ["inputs_1.json", "inputs_2.json"],
37653728
},
37663729
}

0 commit comments

Comments
 (0)