Skip to content

Commit

Permalink
Add logging for canary auto generation and refactor canarySettings (#…
Browse files Browse the repository at this point in the history
…1078)

* Add logging for canary auto generation

* Refactor canarySettings
  • Loading branch information
marc-1010 authored Jul 8, 2024
1 parent d17ac7d commit 8ea4916
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 43 deletions.
26 changes: 22 additions & 4 deletions src/rpdk/core/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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]
Expand Down Expand Up @@ -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)

Expand Down
41 changes: 2 additions & 39 deletions tests/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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"],
},
}
Expand Down Expand Up @@ -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"],
},
}
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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": [],
},
}
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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"],
},
}
Expand Down Expand Up @@ -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"],
},
}
Expand Down Expand Up @@ -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"],
},
}
Expand Down Expand Up @@ -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"],
},
}
Expand Down Expand Up @@ -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"],
},
}
Expand Down Expand Up @@ -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"],
},
}
Expand Down Expand Up @@ -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"],
},
}
Expand Down Expand Up @@ -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"],
},
}
Expand Down Expand Up @@ -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"],
},
}
Expand Down

0 comments on commit 8ea4916

Please sign in to comment.