diff --git a/src/rpdk/core/project.py b/src/rpdk/core/project.py index 38c7ffd9..bbf0922e 100644 --- a/src/rpdk/core/project.py +++ b/src/rpdk/core/project.py @@ -77,11 +77,9 @@ TYPE_NAME = "typeName" CONTRACT_TEST_FILE_NAMES = "contract_test_file_names" INPUT1_FILE_NAME = "inputs_1.json" -FILE_GENERATION_ENABLED = "file_generation_enabled" CONTRACT_TEST_FOLDER = "contract-tests-artifacts" CONTRACT_TEST_INPUT_PREFIX = "inputs_*" CONTRACT_TEST_DEPENDENCY_FILE_NAME = "dependencies.yml" -FILE_GENERATION_ENABLED = "file_generation_enabled" TYPE_NAME = "typeName" CONTRACT_TEST_FILE_NAMES = "contract_test_file_names" FN_SUB = "Fn::Sub" @@ -181,6 +179,7 @@ def __init__(self, overwrite_enabled=False, root=None): self.executable_entrypoint = None self.fragment_dir = None self.canary_settings = {} + self.has_canary_settings = None self.target_info = {} self.env = Environment( @@ -257,7 +256,9 @@ def rpdk_config(self): @property def file_generation_enabled(self): - return self.canary_settings.get(FILE_GENERATION_ENABLED, False) + if self.has_canary_settings is False: + return False + return True @property def contract_test_file_names(self): @@ -338,6 +339,10 @@ def validate_and_load_resource_settings(self, raw_settings): self._plugin = load_plugin(raw_settings["language"]) self.settings = raw_settings.get("settings", {}) self.canary_settings = raw_settings.get("canarySettings", {}) + if raw_settings.get("canarySettings", False) is False: + self.has_canary_settings = False + else: + self.has_canary_settings = True def _write_example_schema(self): self.schema = resource_json( @@ -454,7 +459,6 @@ def init(self, type_name, language, settings=None): self._plugin = load_plugin(language) self.settings = settings or {} self.canary_settings = { - FILE_GENERATION_ENABLED: True, CONTRACT_TEST_FILE_NAMES: [INPUT1_FILE_NAME], } self._write_example_schema() @@ -1323,9 +1327,16 @@ def generate_canary_files(self) -> None: not self.file_generation_enabled or not Path(self.target_contract_test_folder_path).exists() ): + LOG.info("Skipping Canary Auto-Generation") return + LOG.info("Starting Canary Auto-Generation...") + if self.file_generation_enabled and self.canary_settings == {}: + LOG.warning( + "canarySettings are provided but empty. Generation is enabled with default settings." + ) self._setup_stack_template_environment() self._generate_stack_template_files() + LOG.info("Finished Canary Auto-Generation") def _setup_stack_template_environment(self) -> None: stack_template_root = Path(self.target_canary_root_path) @@ -1337,7 +1348,12 @@ def _setup_stack_template_environment(self) -> None: ) bootstrap_file = stack_template_root / CANARY_DEPENDENCY_FILE_NAME if dependencies_file.exists(): + LOG.debug("Writing: %s", bootstrap_file) shutil.copy(str(dependencies_file), str(bootstrap_file)) + else: + LOG.debug( + "Not found: %s. Not writing to: %s", dependencies_file, bootstrap_file + ) def _generate_stack_template_files(self) -> None: stack_template_folder = Path(self.target_canary_folder_path) @@ -1349,6 +1365,7 @@ def _generate_stack_template_files(self) -> None: ] contract_test_files = sorted(contract_test_files) for count, ct_file in enumerate(contract_test_files, start=1): + LOG.debug("Loading contract test input file: %s", ct_file) with ct_file.open("r") as f: json_data = json.load(f) resource_name = self.type_info[2] @@ -1398,6 +1415,7 @@ def _save_stack_template_data( f"{CANARY_FILE_PREFIX}{contract_test_input_count}_{suffix}.yaml" ) stack_template_file_path = stack_template_folder / stack_template_file_name + LOG.debug("Writing Canary Stack Template File: %s", stack_template_file_path) with stack_template_file_path.open("w") as stack_template_file: yaml.dump(stack_template_data, stack_template_file, indent=2) diff --git a/tests/test_project.py b/tests/test_project.py index 3789b51f..06502834 100644 --- a/tests/test_project.py +++ b/tests/test_project.py @@ -39,7 +39,6 @@ CONTRACT_TEST_DEPENDENCY_FILE_NAME, CONTRACT_TEST_FILE_NAMES, CONTRACT_TEST_FOLDER, - FILE_GENERATION_ENABLED, OVERRIDES_FILENAME, SCHEMA_UPLOAD_FILENAME, SETTINGS_FILENAME, @@ -2796,7 +2795,6 @@ def test_generate_canary_files(project): "futureProperty": "value", "typeName": "AWS::Example::Resource", "canarySettings": { - FILE_GENERATION_ENABLED: True, CONTRACT_TEST_FILE_NAMES: ["inputs_1.json", "inputs_2.json"], }, } @@ -2850,7 +2848,6 @@ def test_create_template_file(mock_yaml_dump, project): "futureProperty": "value", "typeName": "AWS::Example::Resource", "canarySettings": { - FILE_GENERATION_ENABLED: True, CONTRACT_TEST_FILE_NAMES: ["inputs_1.json", "inputs_2.json"], }, } @@ -2932,30 +2929,6 @@ def setup_rpdk_config(project, rpdk_config): (contract_test_folder / CONTRACT_TEST_DEPENDENCY_FILE_NAME).touch() -def test_generate_canary_files_when_not_enabled(project): - rpdk_config = { - ARTIFACT_TYPE_RESOURCE: "RESOURCE", - "language": LANGUAGE, - "runtime": RUNTIME, - "entrypoint": None, - "testEntrypoint": None, - "futureProperty": "value", - "typeName": "AWS::Example::Resource", - "canarySettings": { - FILE_GENERATION_ENABLED: False, - "contract_test_file_names": ["inputs_1.json", "inputs_2.json"], - }, - } - tmp_path = project.root - setup_rpdk_config(project, rpdk_config) - project.generate_canary_files() - - canary_root_path = tmp_path / TARGET_CANARY_ROOT_FOLDER - canary_folder_path = tmp_path / TARGET_CANARY_FOLDER - assert not canary_root_path.exists() - assert not canary_folder_path.exists() - - def test_generate_canary_files_no_canary_settings(project): rpdk_config = { ARTIFACT_TYPE_RESOURCE: "RESOURCE", @@ -2986,7 +2959,6 @@ def test_generate_canary_files_empty_input_files(project): "futureProperty": "value", "typeName": "AWS::Example::Resource", "canarySettings": { - FILE_GENERATION_ENABLED: True, "contract_test_file_names": [], }, } @@ -3018,8 +2990,8 @@ def test_generate_canary_files_empty_canary_settings(project): project.generate_canary_files() canary_root_path = tmp_path / TARGET_CANARY_ROOT_FOLDER canary_folder_path = tmp_path / TARGET_CANARY_FOLDER - assert not canary_root_path.exists() - assert not canary_folder_path.exists() + assert canary_root_path.exists() + assert canary_folder_path.exists() def _get_mock_yaml_dump_call_arg( @@ -3063,7 +3035,6 @@ def test_generate_canary_files_with_patch_inputs(mock_yaml_dump, project): "futureProperty": "value", "typeName": "AWS::Example::Resource", "canarySettings": { - FILE_GENERATION_ENABLED: True, CONTRACT_TEST_FILE_NAMES: ["inputs_1.json", "inputs_2.json"], }, } @@ -3144,7 +3115,6 @@ def test_create_template_file_with_patch_inputs(mock_yaml_dump, project): "futureProperty": "value", "typeName": "AWS::Example::Resource", "canarySettings": { - FILE_GENERATION_ENABLED: True, CONTRACT_TEST_FILE_NAMES: ["inputs_1.json", "inputs_2.json"], }, } @@ -3246,7 +3216,6 @@ def test_create_template_file_by_list_index(mock_yaml_dump, project): "futureProperty": "value", "typeName": "AWS::Example::Resource", "canarySettings": { - FILE_GENERATION_ENABLED: True, CONTRACT_TEST_FILE_NAMES: ["inputs_1.json", "inputs_2.json"], }, } @@ -3324,7 +3293,6 @@ def test_create_template_file_with_skipped_patch_operation(mock_yaml_dump, proje "futureProperty": "value", "typeName": "AWS::Example::Resource", "canarySettings": { - FILE_GENERATION_ENABLED: True, CONTRACT_TEST_FILE_NAMES: ["inputs_1.json", "inputs_2.json"], }, } @@ -3403,7 +3371,6 @@ def test_create_template_file_with_patch_inputs_missing_from_create( "futureProperty": "value", "typeName": "AWS::Example::Resource", "canarySettings": { - FILE_GENERATION_ENABLED: True, CONTRACT_TEST_FILE_NAMES: ["inputs_1.json", "inputs_2.json"], }, } @@ -3499,7 +3466,6 @@ def test_create_template_file_throws_error_with_invalid_path(mock_yaml_dump, pro "futureProperty": "value", "typeName": "AWS::Example::Resource", "canarySettings": { - FILE_GENERATION_ENABLED: True, CONTRACT_TEST_FILE_NAMES: ["inputs_1.json", "inputs_2.json"], }, } @@ -3555,7 +3521,6 @@ def test_create_template_file_with_nested_replace_patch_inputs(mock_yaml_dump, p "futureProperty": "value", "typeName": "AWS::Example::Resource", "canarySettings": { - FILE_GENERATION_ENABLED: True, CONTRACT_TEST_FILE_NAMES: ["inputs_1.json", "inputs_2.json"], }, } @@ -3661,7 +3626,6 @@ def test_create_template_file_with_nested_remove_patch_inputs(mock_yaml_dump, pr "futureProperty": "value", "typeName": "AWS::Example::Resource", "canarySettings": { - FILE_GENERATION_ENABLED: True, CONTRACT_TEST_FILE_NAMES: ["inputs_1.json", "inputs_2.json"], }, } @@ -3760,7 +3724,6 @@ def test_create_template_file_with_nested_add_patch_inputs(mock_yaml_dump, proje "futureProperty": "value", "typeName": "AWS::Example::Resource", "canarySettings": { - FILE_GENERATION_ENABLED: True, CONTRACT_TEST_FILE_NAMES: ["inputs_1.json", "inputs_2.json"], }, }