Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement canary file generation functionality from contract test PatchInputs #1074

Merged
merged 3 commits into from
Jun 24, 2024

Conversation

marc-1010
Copy link
Contributor

@marc-1010 marc-1010 commented Jun 5, 2024

Issue #, if available:

Description of changes:
Add canary file generation functionality for Patch Operation canaries from contract test inputs.
canarySettings will be shared with generating Create Operation canaries.

Testing Scenarios:

  1. Validate that the correct number of output files are generated. Validated that file naming and structure are correct.
  2. Validate that an existing field in the CreateInputs will be updated by a replace, add or remove operation in PatchInputs.
  3. Validate that only replace, add and remove operations result in an update. Other operations such as move, copy and test are ignored.
  4. Validate that a field can be updated if it was not included in CreateInputs but included in PatchInputs for add and replace operations.
  5. Validate that nested inputs are correctly updated.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@marc-1010 marc-1010 marked this pull request as ready for review June 6, 2024 23:12
src/rpdk/core/project.py Outdated Show resolved Hide resolved
src/rpdk/core/project.py Outdated Show resolved Hide resolved
Copy link
Contributor

@ammokhov ammokhov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR needs some work. I think we should have a method that will translate desired state (dict) into a stack template (which we already have). Then you should prepare a list of such structures. You should iterate over each CreateInputs and apply PatchInputs then we can loop through all of them and generate stack template with names added incrementally.

Example:

{
    "CreateInputs": {
        "property1": "value1",
        "property2": "value1",
        "property3": "value1",
    },
    "PatchInputs": [
        {
            "op": "add",
            "path": "/property4",
            "value": "value1"
        },
        {
            "op": "replace",
            "path": "/property1",
            "value": "value2"
        }
    ]
}

those inputs will be translated into this

[
    {
        "property1": "value1",
        "property2": "value1",
        "property3": "value1"
    },
    {
        "property1": "value2",
        "property2": "value1",
        "property3": "value1",
        "property4": "value1",
    }
]

then you can iterate and run it through stack generation method

def apply_patch(document:dict, patch_document: Sequence[Dict]):
    modified_document = jsonpatch.JsonPatch(patch_document).apply(document)
    ...

def generate_canaries(...):
    inputs = get_list_of_inputs(...)
    for input in inputs:
        canary_file = generate_canary(input)

src/rpdk/core/project.py Outdated Show resolved Hide resolved
src/rpdk/core/project.py Outdated Show resolved Hide resolved
@marc-1010
Copy link
Contributor Author

Summary of Updates:

  • Refactored hardcoded values into defined constants
  • Refactored to use jsonpatch and removed unnecessary functions. A noteworthy changes are listed below:
    • jsonpath will enforce path validation. If a path is invalid, an exception will be raised. Previous behavior of skipping updates with invalid paths could lead consumers to ignore operations that are not passing.
    • Contract test PatchInputs.path values should be relative to the “Properties” key and include a leading slash(“/“). This behavior will be enforced in canary generation.
  • Revised algorithm for PatchInputs
    • Filter for supported “op” (ie remove, replace and add)
    • Translate “value” to dynamic values
    • use jsonpatch to apply translated patch(which includes dynamic values) to CreateInputs
    • Save

src/rpdk/core/project.py Show resolved Hide resolved
@rajdnp rajdnp merged commit d17ac7d into aws-cloudformation:master Jun 24, 2024
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants