Reputation: 11325
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']))
I have this test case and the first prints
{'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}}
which seems fine, seems like it's the properly mocked response. But then: ec2_client.describe_security_groups(GroupIds=['sg-3e2bcf04'])
fails
E botocore.exceptions.ClientError: An error occurred (InvalidGroup.NotFound) when calling the DescribeSecurityGroups operation: The security group '{'sg-3e2bcf04'}' does not exist
Is there something else I need to mock out?
EDIT: It seems like it generates a non predictable random GroupID at every run. Any idea how to lock it down?
Upvotes: 0
Views: 541
Reputation: 2035
You may need to create one SG and can describe the created one. 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()
Output:
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}}
Upvotes: 1