Skip to content

Add test for SSM GetParameterHistory API #11841

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion localstack-core/localstack/testing/pytest/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -1665,7 +1665,10 @@ def _create_parameter(**kwargs):
yield _create_parameter

for param in params:
aws_client.ssm.delete_parameter(Name=param)
try:
aws_client.ssm.delete_parameter(Name=param)
except Exception as e:
LOG.debug("error cleaning up parameter %s: %s", param, e)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

When I used the existing create_parameter method, I got the following error. Because the test calling create_parameter three times with the Overwrite=True parameter.

ERROR tests/aws/services/ssm/test_ssm.py::TestSSM::test_get_parameter_history - botocore.errorfactory.ParameterNotFound: An error occurred (ParameterNotFound) when calling the DeleteParameter operation: Parameter param-36e2b5a6 not found.

Since the parameter only needs to be deleted once, I added exception handling, referencing the existing implementation😀

https://github.com/localstack/localstack/blob/master/localstack-core/localstack/testing/pytest/fixtures.py#L163-L164



@pytest.fixture
Expand Down
19 changes: 19 additions & 0 deletions tests/aws/services/ssm/test_ssm.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,25 @@ def test_get_parameter_by_arn(self, create_parameter, aws_client, snapshot, clea
snapshot.match("Parameter", parameter_by_arn)
snapshot.add_transformer(snapshot.transform.key_value("Name"))

@markers.aws.validated
@markers.snapshot.skip_snapshot_verify(paths=["$..Tier", "$..Policies"])
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I believe that LocalStack does not support the Tier and Policies response fields. Therefore, I used the skip_snapshot_verify marker. Is this correct?

$ awslocal ssm put-parameter --name sandbox --type String --value value1
{
    "Version": 1
}

$ awslocal ssm put-parameter --name sandbox --type String --value value2 --overwrite
{
    "Version": 2
}

$ awslocal ssm put-parameter --name sandbox --type String --value value3 --overwrite
{
    "Version": 3
}

$ awslocal ssm get-parameter --name sandbox
{
    "Parameter": {
        "Name": "sandbox",
        "Type": "String",
        "Value": "value3",
        "Version": 3,
        "LastModifiedDate": "2025-01-28T21:51:59.532000+09:00",
        "ARN": "arn:aws:ssm:ap-northeast-1:000000000000:parameter/sandbox",
        "DataType": "text"
    }
}

def test_get_parameter_history(self, create_parameter, aws_client, snapshot):
param_name = f"param-{short_uid()}"
create_parameter(Name=param_name, Value="test", Type="String")
create_parameter(Name=param_name, Value="test2", Type="String", Overwrite=True)
create_parameter(Name=param_name, Value="test3", Type="String", Overwrite=True)

response = aws_client.ssm.get_parameter_history(Name=param_name)
assert len(response["Parameters"]) == 3
assert response["Parameters"][0]["Value"] == "test"
assert response["Parameters"][0]["Version"] == 1
assert response["Parameters"][2]["Value"] == "test3"
assert response["Parameters"][2]["Version"] == 3
Comment on lines +171 to +175
Copy link
Member

Choose a reason for hiding this comment

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

Typically we'd use our snapshot library in these cases, which will also catch changes to the API if we re-validate the case in the future. You can find more information on this in our contribution guidelines.

Please let me know if you'd like to tackle this yourself, otherwise I can also generate this for you! 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! I will add snapshot test soon :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Question: When using snapshots test, are regular assertions like this unnecessary? Or is it better to have both? Looking at the existing tests in the same file, it seems there are tests using assertions. If it’s not needed, I’ll remove it 😀


snapshot.match("get-parameter-history-response", response)
snapshot.add_transformer(snapshot.transform.key_value("Name"))
snapshot.add_transformer(snapshot.transform.key_value("LastModifiedUser"))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for your kind support. I captured the snapshot in my personal AWS account 👍

$ export SNAPSHOT_UPDATE=1
$ export TEST_TARGET=AWS_CLOUD
$ make test TEST_PATH=tests/aws/services/ssm


@markers.aws.validated
def test_get_inexistent_maintenance_window(self, aws_client):
invalid_name = "mw-00000000000000000"
Expand Down
53 changes: 51 additions & 2 deletions tests/aws/services/ssm/test_ssm.snapshot.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"tests/aws/services/ssm/test_ssm.py::TestSSM::test_get_parameter_by_arn": {
"recorded-date": "16-07-2024, 17:17:40",
"recorded-date": "28-01-2025, 12:43:53",
"recorded-content": {
"Parameter": {
"ARN": "arn:<partition>:ssm:<region>:111111111111:parameter/<name:1>",
Expand All @@ -14,7 +14,7 @@
}
},
"tests/aws/services/ssm/test_ssm.py::TestSSM::test_parameters_with_path": {
"recorded-date": "08-01-2025, 17:04:53",
"recorded-date": "28-01-2025, 12:43:55",
"recorded-content": {
"put-parameter-response": {
"Tier": "Standard",
Expand Down Expand Up @@ -91,5 +91,54 @@
}
}
}
},
"tests/aws/services/ssm/test_ssm.py::TestSSM::test_get_parameter_history": {
"recorded-date": "28-01-2025, 12:43:53",
"recorded-content": {
"get-parameter-history-response": {
"Parameters": [
{
"DataType": "text",
"Labels": [],
"LastModifiedDate": "<datetime>",
"LastModifiedUser": "<last-modified-user:1>",
"Name": "<name:1>",
"Policies": [],
"Tier": "Standard",
"Type": "String",
"Value": "test",
"Version": 1
},
{
"DataType": "text",
"Labels": [],
"LastModifiedDate": "<datetime>",
"LastModifiedUser": "<last-modified-user:1>",
"Name": "<name:1>",
"Policies": [],
"Tier": "Standard",
"Type": "String",
"Value": "test2",
"Version": 2
},
{
"DataType": "text",
"Labels": [],
"LastModifiedDate": "<datetime>",
"LastModifiedUser": "<last-modified-user:1>",
"Name": "<name:1>",
"Policies": [],
"Tier": "Standard",
"Type": "String",
"Value": "test3",
"Version": 3
}
],
"ResponseMetadata": {
"HTTPHeaders": {},
"HTTPStatusCode": 200
}
}
}
}
}
34 changes: 32 additions & 2 deletions tests/aws/services/ssm/test_ssm.validation.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,38 @@
{
"tests/aws/services/ssm/test_ssm.py::TestSSM::test_describe_parameters": {
"last_validated_date": "2025-01-28T12:43:49+00:00"
},
"tests/aws/services/ssm/test_ssm.py::TestSSM::test_get_inexistent_maintenance_window": {
"last_validated_date": "2025-01-28T12:43:53+00:00"
},
"tests/aws/services/ssm/test_ssm.py::TestSSM::test_get_inexistent_secret": {
"last_validated_date": "2025-01-28T12:43:51+00:00"
},
"tests/aws/services/ssm/test_ssm.py::TestSSM::test_get_parameter_by_arn": {
"last_validated_date": "2024-07-16T17:17:40+00:00"
"last_validated_date": "2025-01-28T12:43:53+00:00"
},
"tests/aws/services/ssm/test_ssm.py::TestSSM::test_get_parameter_history": {
"last_validated_date": "2025-01-28T12:43:53+00:00"
},
"tests/aws/services/ssm/test_ssm.py::TestSSM::test_get_parameters_and_secrets": {
"last_validated_date": "2025-01-28T12:43:51+00:00"
},
"tests/aws/services/ssm/test_ssm.py::TestSSM::test_get_parameters_by_path_and_filter_by_labels": {
"last_validated_date": "2025-01-28T12:43:52+00:00"
},
"tests/aws/services/ssm/test_ssm.py::TestSSM::test_get_secret_parameter": {
"last_validated_date": "2025-01-28T12:43:51+00:00"
},
"tests/aws/services/ssm/test_ssm.py::TestSSM::test_hierarchical_parameter[/<param>//b//c]": {
"last_validated_date": "2025-01-28T12:43:50+00:00"
},
"tests/aws/services/ssm/test_ssm.py::TestSSM::test_hierarchical_parameter[<param>/b/c]": {
"last_validated_date": "2025-01-28T12:43:50+00:00"
},
"tests/aws/services/ssm/test_ssm.py::TestSSM::test_parameters_with_path": {
"last_validated_date": "2025-01-08T17:04:53+00:00"
"last_validated_date": "2025-01-28T12:43:55+00:00"
},
"tests/aws/services/ssm/test_ssm.py::TestSSM::test_put_parameters": {
"last_validated_date": "2025-01-28T12:43:50+00:00"
}
}
Loading