moto 没有模拟 ec2.describe_security_groups 函数调用

moto not mocking ec2.describe_security_groups function call

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。知道如何锁定它吗?

您可能需要创建一个 SG,并可以描述创建的 SG。 source

import boto3
from moto import mock_ec2


@mock_ec2
def test_mocking_getting_security_groups():
    region = 'us-east-2'
    vpc_security_group_id = 'default'
    session = boto3.Session(profile_name='shakeel_aws')
    ec2_client = session.client('ec2', region)

    sg = ec2_client.create_security_group(GroupName="test-sg", Description="Test SG")
    print(sg["GroupId"])
    print(ec2_client.describe_security_groups())
    print(ec2_client.describe_security_groups(GroupIds=[sg["GroupId"]]))


test_mocking_getting_security_groups()

输出:

sg-27a9b9cf
{'SecurityGroups': [{'Description': 'default group', 'GroupName': 'default', 'IpPermissions': [], 'OwnerId': '123456789012', 'GroupId': 'sg-7af61a21', 'IpPermissionsEgress': [{'IpProtocol': '-1', 'IpRanges': [{'CidrIp': '0.0.0.0/0'}], 'UserIdGroupPairs': []}], 'Tags': []}, {'Description': 'Test SG', 'GroupName': 'test-sg', 'IpPermissions': [], 'OwnerId': '123456789012', 'GroupId': 'sg-27a9b9cf', 'IpPermissionsEgress': [{'IpProtocol': '-1', 'IpRanges': [{'CidrIp': '0.0.0.0/0'}], 'UserIdGroupPairs': []}], 'Tags': []}, {'Description': 'default VPC security group', 'GroupName': 'default', 'IpPermissions': [], 'OwnerId': '123456789012', 'GroupId': 'sg-4ec8ebd5', 'IpPermissionsEgress': [{'IpProtocol': '-1', 'IpRanges': [{'CidrIp': '0.0.0.0/0'}], 'UserIdGroupPairs': []}], 'Tags': [], 'VpcId': 'vpc-b1373745'}], 'ResponseMetadata': {'RequestId': '59dbff89-35bd-4eac-99ed-be587EXAMPLE', 'HTTPStatusCode': 200, 'HTTPHeaders': {'server': 'amazon.com'}, 'RetryAttempts': 0}}
{'SecurityGroups': [{'Description': 'Test SG', 'GroupName': 'test-sg', 'IpPermissions': [], 'OwnerId': '123456789012', 'GroupId': 'sg-27a9b9cf', 'IpPermissionsEgress': [{'IpProtocol': '-1', 'IpRanges': [{'CidrIp': '0.0.0.0/0'}], 'UserIdGroupPairs': []}], 'Tags': []}], 'ResponseMetadata': {'RequestId': '59dbff89-35bd-4eac-99ed-be587EXAMPLE', 'HTTPStatusCode': 200, 'HTTPHeaders': {'server': 'amazon.com'}, 'RetryAttempts': 0}}