Moto单元测试EC2 - ' nontype'对象没有属性' id'
我想在我的aws_manager
中统一一些方法:
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]
我的测试看起来像这样:
import unittest
import boto3
from moto import mock_ec2
import aws_manager
@mock_ec2
class TestAwsManager(unittest.TestCase):
def test_launchInstances(self):
manager = aws_manager.aws_manager()
manager.launchInstances(2)
client = boto3.client('ec2',region_name='eu-central-1')
instances = client.describe_instances()
print(instances)
但是在打印时,实例变量看起来像这样:
{'Reservations': [], 'ResponseMetadata': {'RequestId': 'fdcdcab1-ae5c-489e-9c33-4637c5dda355', 'HTTPStatusCode': 200, 'HTTPHeaders': {'server': 'amazon.com'}, 'RetryAttempts': 0}}
我也会遇到此错误:
[ERROR][2022-07-02 22:30:42,452]: 'NoneType' object has no attribute 'id'
我是否监督了某些东西? 客户端不应该包含实例吗?
提前致谢
I want to unittest some methods inside my aws_manager
:
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]
my test looks like this:
import unittest
import boto3
from moto import mock_ec2
import aws_manager
@mock_ec2
class TestAwsManager(unittest.TestCase):
def test_launchInstances(self):
manager = aws_manager.aws_manager()
manager.launchInstances(2)
client = boto3.client('ec2',region_name='eu-central-1')
instances = client.describe_instances()
print(instances)
But the instances variable looks like this when printed:
{'Reservations': [], 'ResponseMetadata': {'RequestId': 'fdcdcab1-ae5c-489e-9c33-4637c5dda355', 'HTTPStatusCode': 200, 'HTTPHeaders': {'server': 'amazon.com'}, 'RetryAttempts': 0}}
I also get this error:
[ERROR][2022-07-02 22:30:42,452]: 'NoneType' object has no attribute 'id'
Have I overseen something? Shouldn't client
contain the instances?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这应该是您的代码模拟EC2和安全组ID。
This should be your code the mock ec2 and security group id.