Moto可以使用SecurityGroup模拟EC2
我最近问了这个问题:
我现在至少发现了为什么发生错误的原因。
我调用ec2.run_instances()
带有多个参数,例如:
ec2.run_instances(
ImageId=IMAGE_ID,
MinCount=count,
MaxCount=count,
InstanceType=INSTANCE_TYPE,
KeyName=KEY_NAME,
SecurityGroupIds=[SECURITY_GROUP_ID]
)
尝试模拟此问题时,我会发现一个错误,因为它找不到指定的SecurityGroupID。
当我评论SecurityGrouoids
之类的时:
...
InstanceType=INSTANCE_TYPE,
KeyName=KEY_NAME,
#SecurityGroupIds=[SECURITY_GROUP_ID]
)
...
moto
测试正常运行。
如何将SecurityGroup添加到实例中?我试图使用该解决方案 这个问题为我。
有人知道如何模拟 /添加SecurityGroup中的EC2模拟吗?
预先感谢
编辑:
测试本身看起来像这样:
@mock_ec2
def test_create_instances_with_decorator(self):
instance_count = 1
image_id = 'ami-0df635fd7dea02388'
client = boto3.client('ec2', region_name='eu-central-1')
client.create_security_group(Description="my-test-group",GroupName="launch-wizard-1")
mngr = aws_manager()
mngr.launchInstances(2)
instances = client.describe_instances()['Reservations'][0]['Instances']
print(instances)
assert len(instances) == 2
assert instances[0]['ImageId'] == image_id
要测试的代码看起来像这样:
SECURITY_GROUP_ID = "sg-0dc0a634bc153adc8"
REGION_NAME = "eu-central-1"
INSTANCE_TYPE = "t2.micro"
class aws_manager:
def __init__(self) -> None:
self.ec2 = boto3.client('ec2', region_name=REGION_NAME)
self.running_instances = []
def launchInstances(self, count: int):
instances = self.ec2.run_instances(
ImageId=IMAGE_ID,
MinCount=count,
MaxCount=count,
InstanceType=INSTANCE_TYPE,
KeyName=KEY_NAME,
SecurityGroupIds=[SECURITY_GROUP_ID]
)["Instances"][0]
I recently asked this question:
moto unit test EC2 - 'NoneType' object has no attribute 'id'
I now have at least found out about why this error occurs.
I call ec2.run_instances()
with multiple parameters, like this:
ec2.run_instances(
ImageId=IMAGE_ID,
MinCount=count,
MaxCount=count,
InstanceType=INSTANCE_TYPE,
KeyName=KEY_NAME,
SecurityGroupIds=[SECURITY_GROUP_ID]
)
When trying to mock this like shown in my other question, I get an error because it can't find the specified securityGroupId.
When I comment out the SecurityGrouoIds
like so:
...
InstanceType=INSTANCE_TYPE,
KeyName=KEY_NAME,
#SecurityGroupIds=[SECURITY_GROUP_ID]
)
...
the moto
test runs fine.
How can I add the securityGroup to the instance? I have tried to use the solution from
this question, but it didn't work out for me.
Does anyone have an Idea about how to mock / add the securityGroup to the ec2 mock?
Thanks in advance
Edit:
The test itself looks like this:
@mock_ec2
def test_create_instances_with_decorator(self):
instance_count = 1
image_id = 'ami-0df635fd7dea02388'
client = boto3.client('ec2', region_name='eu-central-1')
client.create_security_group(Description="my-test-group",GroupName="launch-wizard-1")
mngr = aws_manager()
mngr.launchInstances(2)
instances = client.describe_instances()['Reservations'][0]['Instances']
print(instances)
assert len(instances) == 2
assert instances[0]['ImageId'] == image_id
And the code to be tested looks like this:
SECURITY_GROUP_ID = "sg-0dc0a634bc153adc8"
REGION_NAME = "eu-central-1"
INSTANCE_TYPE = "t2.micro"
class aws_manager:
def __init__(self) -> None:
self.ec2 = boto3.client('ec2', region_name=REGION_NAME)
self.running_instances = []
def launchInstances(self, count: int):
instances = self.ec2.run_instances(
ImageId=IMAGE_ID,
MinCount=count,
MaxCount=count,
InstanceType=INSTANCE_TYPE,
KeyName=KEY_NAME,
SecurityGroupIds=[SECURITY_GROUP_ID]
)["Instances"][0]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论