Moto单元测试EC2 - ' nontype'对象没有属性' id'

发布于 2025-02-13 01:44:44 字数 1405 浏览 1 评论 0原文

我想在我的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森林散布 2025-02-20 01:44:44

这应该是您的代码模拟EC2和安全组ID。

def ec2():
"""
Returns a mock EC2 resource
"""

with mock_ec2():

    ec2_res = boto3.resource("ec2", region_name="us-west-1")

    ec2_res.create_key_pair(KeyName="your_key")

    yield ec2_res

@pytest.fixture
def security_group_id(ec2):
    """
    Returns a mock EC2 instance
    """
    security_group = ec2.create_security_group(
        GroupName="your_group_name",
        Description="test",
    )
    yield security_group.id

This should be your code the mock ec2 and security group id.

def ec2():
"""
Returns a mock EC2 resource
"""

with mock_ec2():

    ec2_res = boto3.resource("ec2", region_name="us-west-1")

    ec2_res.create_key_pair(KeyName="your_key")

    yield ec2_res

@pytest.fixture
def security_group_id(ec2):
    """
    Returns a mock EC2 instance
    """
    security_group = ec2.create_security_group(
        GroupName="your_group_name",
        Description="test",
    )
    yield security_group.id
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文