import boto3
....
@mock_ec2
def test_mocking_getting_security_groups(self):
region = 'us-east-2'
vpc_security_group_id = 'default'
session = boto3.Session(profile_name=profile)
ec2_client = session.client('ec2', region)
print(ec2_client.describe_security_groups())
print(ec2_client.describe_security_groups(GroupIds=['sg-3e2bcf04']))
我有这个测试用例和第一个打印件
{'SecurityGroups': [{'Description': 'default group', 'GroupName': 'default', 'IpPermissions': [], 'OwnerId': '123456789012', 'GroupId': 'sg-0b13b4ba', 'IpPermissionsEgress': [{'IpProtocol': '-1', 'IpRanges': [{'CidrIp': '0.0.0.0/0'}], 'UserIdGroupPairs': []}], 'Tags': []}, {'Description': 'default VPC security group', 'GroupName': 'default', 'IpPermissions': [], 'OwnerId': '123456789012', 'GroupId': 'sg-df20018b', 'IpPermissionsEgress': [{'IpProtocol': '-1', 'IpRanges': [{'CidrIp': '0.0.0.0/0'}], 'UserIdGroupPairs': []}], 'Tags': [], 'VpcId': 'vpc-1940c2c1'}], 'ResponseMetadata': {'RequestId': '59dbff89-35bd-4eac-99ed-be587EXAMPLE', 'HTTPStatusCode': 200, 'HTTPHeaders': {'server': 'amazon.com'}, 'RetryAttempts': 0}}
看起来不错,似乎是正确模拟的响应。但后来:ec2_client.describe_security_groups(GroupIds=['sg-3e2bcf04'])
失败
E botocore.exceptions.ClientError: An error occurred (InvalidGroup.NotFound) when calling the DescribeSecurityGroups operation: The security group '{'sg-3e2bcf04'}' does not exist
还有什么我需要模拟的吗?
编辑:似乎它在每次运行时都会生成一个不可预测的随机 GroupID。知道如何锁定它吗?