Techdebt: Replace sure with regular assertions in RAM (#6681)

This commit is contained in:
kbalk 2023-08-17 03:29:10 -04:00 committed by GitHub
parent a6f140ddab
commit a36d8fcba4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,8 +1,8 @@
import re
import time import time
from datetime import datetime from datetime import datetime
import boto3 import boto3
import sure # noqa # pylint: disable=unused-import
from botocore.exceptions import ClientError from botocore.exceptions import ClientError
import pytest import pytest
@ -20,16 +20,20 @@ def test_create_resource_share():
# then # then
resource = response["resourceShare"] resource = response["resourceShare"]
resource["allowExternalPrincipals"].should.be.ok assert resource["allowExternalPrincipals"] is True
resource["creationTime"].should.be.a(datetime) assert isinstance(resource["creationTime"], datetime)
resource["lastUpdatedTime"].should.be.a(datetime) assert isinstance(resource["lastUpdatedTime"], datetime)
resource["name"].should.equal("test") assert resource["name"] == "test"
resource["owningAccountId"].should.equal(ACCOUNT_ID) assert resource["owningAccountId"] == ACCOUNT_ID
resource["resourceShareArn"].should.match( assert re.match(
r"arn:aws:ram:us-east-1:\d{12}:resource-share/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}" (
r"arn:aws:ram:us-east-1:\d{12}:resource-share"
r"/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"
),
resource["resourceShareArn"],
) )
resource["status"].should.equal("ACTIVE") assert resource["status"] == "ACTIVE"
resource.should_not.have.key("featureSet") assert "featureSet" not in resource
# creating a resource share with the name should result in a second one # creating a resource share with the name should result in a second one
# not overwrite/update the old one # not overwrite/update the old one
@ -44,18 +48,22 @@ def test_create_resource_share():
# then # then
resource = response["resourceShare"] resource = response["resourceShare"]
resource["allowExternalPrincipals"].should_not.be.ok assert resource["allowExternalPrincipals"] is False
resource["creationTime"].should.be.a(datetime) assert isinstance(resource["creationTime"], datetime)
resource["lastUpdatedTime"].should.be.a(datetime) assert isinstance(resource["lastUpdatedTime"], datetime)
resource["name"].should.equal("test") assert resource["name"] == "test"
resource["owningAccountId"].should.equal(ACCOUNT_ID) assert resource["owningAccountId"] == ACCOUNT_ID
resource["resourceShareArn"].should.match( assert re.match(
r"arn:aws:ram:us-east-1:\d{12}:resource-share/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}" (
r"arn:aws:ram:us-east-1:\d{12}:resource-share"
r"/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"
),
resource["resourceShareArn"],
) )
resource["status"].should.equal("ACTIVE") assert resource["status"] == "ACTIVE"
response = client.get_resource_shares(resourceOwner="SELF") response = client.get_resource_shares(resourceOwner="SELF")
response["resourceShares"].should.have.length_of(2) assert len(response["resourceShares"]) == 2
@mock_ram @mock_ram
@ -68,10 +76,10 @@ def test_create_resource_share_errors():
with pytest.raises(ClientError) as e: with pytest.raises(ClientError) as e:
client.create_resource_share(name="test", resourceArns=["inalid-arn"]) client.create_resource_share(name="test", resourceArns=["inalid-arn"])
ex = e.value ex = e.value
ex.operation_name.should.equal("CreateResourceShare") assert ex.operation_name == "CreateResourceShare"
ex.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400) assert ex.response["ResponseMetadata"]["HTTPStatusCode"] == 400
ex.response["Error"]["Code"].should.contain("MalformedArnException") assert "MalformedArnException" in ex.response["Error"]["Code"]
ex.response["Error"]["Message"].should.equal( assert ex.response["Error"]["Message"] == (
"The specified resource ARN inalid-arn is not valid. " "The specified resource ARN inalid-arn is not valid. "
"Verify the ARN and try again." "Verify the ARN and try again."
) )
@ -83,11 +91,12 @@ def test_create_resource_share_errors():
name="test", resourceArns=[f"arn:aws:iam::{ACCOUNT_ID}:role/test"] name="test", resourceArns=[f"arn:aws:iam::{ACCOUNT_ID}:role/test"]
) )
ex = e.value ex = e.value
ex.operation_name.should.equal("CreateResourceShare") assert ex.operation_name == "CreateResourceShare"
ex.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400) assert ex.response["ResponseMetadata"]["HTTPStatusCode"] == 400
ex.response["Error"]["Code"].should.contain("MalformedArnException") assert "MalformedArnException" in ex.response["Error"]["Code"]
ex.response["Error"]["Message"].should.equal( assert (
"You cannot share the selected resource type." ex.response["Error"]["Message"]
== "You cannot share the selected resource type."
) )
# invalid principal ID # invalid principal ID
@ -101,10 +110,10 @@ def test_create_resource_share_errors():
], ],
) )
ex = e.value ex = e.value
ex.operation_name.should.equal("CreateResourceShare") assert ex.operation_name == "CreateResourceShare"
ex.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400) assert ex.response["ResponseMetadata"]["HTTPStatusCode"] == 400
ex.response["Error"]["Code"].should.contain("InvalidParameterException") assert "InvalidParameterException" in ex.response["Error"]["Code"]
ex.response["Error"]["Message"].should.equal( assert ex.response["Error"]["Message"] == (
"Principal ID invalid is malformed. Verify the ID and try again." "Principal ID invalid is malformed. Verify the ID and try again."
) )
@ -132,7 +141,7 @@ def test_create_resource_share_with_organization():
) )
# then # then
response["resourceShare"]["name"].should.equal("test") assert response["resourceShare"]["name"] == "test"
# share in an OU # share in an OU
# when # when
@ -145,7 +154,7 @@ def test_create_resource_share_with_organization():
) )
# then # then
response["resourceShare"]["name"].should.equal("test") assert response["resourceShare"]["name"] == "test"
@mock_ram @mock_ram
@ -169,10 +178,10 @@ def test_create_resource_share_with_organization_errors():
], ],
) )
ex = e.value ex = e.value
ex.operation_name.should.equal("CreateResourceShare") assert ex.operation_name == "CreateResourceShare"
ex.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400) assert ex.response["ResponseMetadata"]["HTTPStatusCode"] == 400
ex.response["Error"]["Code"].should.contain("UnknownResourceException") assert "UnknownResourceException" in ex.response["Error"]["Code"]
ex.response["Error"]["Message"].should.equal( assert ex.response["Error"]["Message"] == (
"Organization o-unknown could not be found." "Organization o-unknown could not be found."
) )
@ -187,10 +196,10 @@ def test_create_resource_share_with_organization_errors():
], ],
) )
ex = e.value ex = e.value
ex.operation_name.should.equal("CreateResourceShare") assert ex.operation_name == "CreateResourceShare"
ex.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400) assert ex.response["ResponseMetadata"]["HTTPStatusCode"] == 400
ex.response["Error"]["Code"].should.contain("UnknownResourceException") assert "UnknownResourceException" in ex.response["Error"]["Code"]
ex.response["Error"]["Message"].should.equal( assert ex.response["Error"]["Message"] == (
"OrganizationalUnit ou-unknown in unknown organization could not be found." "OrganizationalUnit ou-unknown in unknown organization could not be found."
) )
@ -205,18 +214,22 @@ def test_get_resource_shares():
response = client.get_resource_shares(resourceOwner="SELF") response = client.get_resource_shares(resourceOwner="SELF")
# then # then
response["resourceShares"].should.have.length_of(1) assert len(response["resourceShares"]) == 1
resource = response["resourceShares"][0] resource = response["resourceShares"][0]
resource["allowExternalPrincipals"].should.be.ok assert resource["allowExternalPrincipals"] is True
resource["creationTime"].should.be.a(datetime) assert isinstance(resource["creationTime"], datetime)
resource["featureSet"].should.equal("STANDARD") assert resource["featureSet"] == "STANDARD"
resource["lastUpdatedTime"].should.be.a(datetime) assert isinstance(resource["lastUpdatedTime"], datetime)
resource["name"].should.equal("test") assert resource["name"] == "test"
resource["owningAccountId"].should.equal(ACCOUNT_ID) assert resource["owningAccountId"] == ACCOUNT_ID
resource["resourceShareArn"].should.match( assert re.match(
r"arn:aws:ram:us-east-1:\d{12}:resource-share/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}" (
r"arn:aws:ram:us-east-1:\d{12}:resource-share"
r"/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"
),
resource["resourceShareArn"],
) )
resource["status"].should.equal("ACTIVE") assert resource["status"] == "ACTIVE"
@mock_ram @mock_ram
@ -229,10 +242,10 @@ def test_get_resource_shares_errors():
with pytest.raises(ClientError) as e: with pytest.raises(ClientError) as e:
client.get_resource_shares(resourceOwner="invalid") client.get_resource_shares(resourceOwner="invalid")
ex = e.value ex = e.value
ex.operation_name.should.equal("GetResourceShares") assert ex.operation_name == "GetResourceShares"
ex.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400) assert ex.response["ResponseMetadata"]["HTTPStatusCode"] == 400
ex.response["Error"]["Code"].should.contain("InvalidParameterException") assert "InvalidParameterException" in ex.response["Error"]["Code"]
ex.response["Error"]["Message"].should.equal( assert ex.response["Error"]["Message"] == (
"invalid is not a valid resource owner. " "invalid is not a valid resource owner. "
"Specify either SELF or OTHER-ACCOUNTS and try again." "Specify either SELF or OTHER-ACCOUNTS and try again."
) )
@ -250,19 +263,23 @@ def test_update_resource_share():
# then # then
resource = response["resourceShare"] resource = response["resourceShare"]
resource["allowExternalPrincipals"].should.be.ok assert resource["allowExternalPrincipals"] is True
resource["name"].should.equal("test-update") assert resource["name"] == "test-update"
resource["owningAccountId"].should.equal(ACCOUNT_ID) assert resource["owningAccountId"] == ACCOUNT_ID
resource["resourceShareArn"].should.match( assert re.match(
r"arn:aws:ram:us-east-1:\d{12}:resource-share/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}" (
r"arn:aws:ram:us-east-1:\d{12}:resource-share"
r"/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"
),
resource["resourceShareArn"],
) )
resource["status"].should.equal("ACTIVE") assert resource["status"] == "ACTIVE"
resource.should_not.have.key("featureSet") assert "featureSet" not in resource
creation_time = resource["creationTime"] creation_time = resource["creationTime"]
resource["lastUpdatedTime"].should.be.greater_than(creation_time) assert resource["lastUpdatedTime"] > creation_time
response = client.get_resource_shares(resourceOwner="SELF") response = client.get_resource_shares(resourceOwner="SELF")
response["resourceShares"].should.have.length_of(1) assert len(response["resourceShares"]) == 1
@mock_ram @mock_ram
@ -278,11 +295,12 @@ def test_update_resource_share_errors():
name="test-update", name="test-update",
) )
ex = e.value ex = e.value
ex.operation_name.should.equal("UpdateResourceShare") assert ex.operation_name == "UpdateResourceShare"
ex.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400) assert ex.response["ResponseMetadata"]["HTTPStatusCode"] == 400
ex.response["Error"]["Code"].should.contain("UnknownResourceException") assert "UnknownResourceException" in ex.response["Error"]["Code"]
ex.response["Error"]["Message"].should.equal( assert ex.response["Error"]["Message"] == (
f"ResourceShare arn:aws:ram:us-east-1:{ACCOUNT_ID}:resource-share/not-existing could not be found." f"ResourceShare arn:aws:ram:us-east-1:{ACCOUNT_ID}"
":resource-share/not-existing could not be found."
) )
@ -297,14 +315,14 @@ def test_delete_resource_share():
response = client.delete_resource_share(resourceShareArn=arn) response = client.delete_resource_share(resourceShareArn=arn)
# then # then
response["returnValue"].should.be.ok assert response["returnValue"] is True
response = client.get_resource_shares(resourceOwner="SELF") response = client.get_resource_shares(resourceOwner="SELF")
response["resourceShares"].should.have.length_of(1) assert len(response["resourceShares"]) == 1
resource = response["resourceShares"][0] resource = response["resourceShares"][0]
resource["status"].should.equal("DELETED") assert resource["status"] == "DELETED"
creation_time = resource["creationTime"] creation_time = resource["creationTime"]
resource["lastUpdatedTime"].should.be.greater_than(creation_time) assert resource["lastUpdatedTime"] > creation_time
@mock_ram @mock_ram
@ -319,11 +337,12 @@ def test_delete_resource_share_errors():
resourceShareArn=f"arn:aws:ram:us-east-1:{ACCOUNT_ID}:resource-share/not-existing" resourceShareArn=f"arn:aws:ram:us-east-1:{ACCOUNT_ID}:resource-share/not-existing"
) )
ex = e.value ex = e.value
ex.operation_name.should.equal("DeleteResourceShare") assert ex.operation_name == "DeleteResourceShare"
ex.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400) assert ex.response["ResponseMetadata"]["HTTPStatusCode"] == 400
ex.response["Error"]["Code"].should.contain("UnknownResourceException") assert "UnknownResourceException" in ex.response["Error"]["Code"]
ex.response["Error"]["Message"].should.equal( assert ex.response["Error"]["Message"] == (
f"ResourceShare arn:aws:ram:us-east-1:{ACCOUNT_ID}:resource-share/not-existing could not be found." f"ResourceShare arn:aws:ram:us-east-1:{ACCOUNT_ID}"
":resource-share/not-existing could not be found."
) )
@ -339,7 +358,7 @@ def test_enable_sharing_with_aws_organization():
response = client.enable_sharing_with_aws_organization() response = client.enable_sharing_with_aws_organization()
# then # then
response["returnValue"].should.be.ok assert response["returnValue"] is True
@mock_ram @mock_ram
@ -353,11 +372,12 @@ def test_enable_sharing_with_aws_organization_errors():
with pytest.raises(ClientError) as e: with pytest.raises(ClientError) as e:
client.enable_sharing_with_aws_organization() client.enable_sharing_with_aws_organization()
ex = e.value ex = e.value
ex.operation_name.should.equal("EnableSharingWithAwsOrganization") assert ex.operation_name == "EnableSharingWithAwsOrganization"
ex.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400) assert ex.response["ResponseMetadata"]["HTTPStatusCode"] == 400
ex.response["Error"]["Code"].should.contain("OperationNotPermittedException") assert "OperationNotPermittedException" in ex.response["Error"]["Code"]
ex.response["Error"]["Message"].should.equal( assert ex.response["Error"]["Message"] == (
"Unable to enable sharing with AWS Organizations. " "Unable to enable sharing with AWS Organizations. "
"Received AccessDeniedException from AWSOrganizations with the following error message: " "Received AccessDeniedException from AWSOrganizations with the "
"following error message: "
"You don't have permissions to access this resource." "You don't have permissions to access this resource."
) )