python - moto 不模拟 ec2.describe_security_groups 函数调用
问题描述
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,并且可以描述创建的一个。 资源
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}}
推荐阅读
- java - Java:在定制的通用向量中间插入元素
- php-extension - 无法在 xampp php 7.3.2 中启用 php_intl.dll
- mysql - 为什么 GROUP_CONCAT 让我的查询更快?
- reactjs - Highcharts 对官方库的反应不能很好地与 Typescript 配合使用
- java - 此 Java 代码中的漏洞是什么?
- python - 连续执行多个python脚本
- python - 为什么我不能在 Django 中打印选择模型实例?
- python - 使用 Python Selenium 定位元素
- regex - 约束所需的正则表达式解决方案
- python - 一串数字的顺序返回