【问题标题】:NotImplementedError: ElasticNetworkInterfaces(AmazonVPC).describe_network_interface_attribute is not yet implementedNotImplementedError: ElasticNetworkInterfaces(AmazonVPC).describe_network_interface_attribute 尚未实现
【发布时间】:2021-12-28 20:27:32
【问题描述】:

如何在 test_elastic_network_interfaces_get_by_description 函数中描述 mock_ec2 的网络接口属性?

@mock_ec2 def test_elastic_network_interfaces_get_by_description():
    ec2 = boto3.resource("ec2", region_name="us-east-1")
    ec2_client= ec2.meta.client

    vpc = ec2.create_vpc(CidrBlock="10.0.0.0/16")
    vpc.reload()
    subnet = ec2.create_subnet(
        VpcId=vpc.id, CidrBlock="10.0.0.0/24", 
        AvailabilityZone="us-east-1a")

    desc = str(uuid4())
    eni1 = ec2.create_network_interface(
        SubnetId=subnet.id, PrivateIpAddress="10.0.10.5", Description=desc )

    # The status of the new interface should be 'available'
    waiter = ec2_client.get_waiter("network_interface_available")
    waiter.wait(NetworkInterfaceIds=[eni1.id])

    eni1.modify_attribute(SourceDestCheck={'Value': False})
    eni1.reload()
    response = eni1.describe_attribute(Attribute='description')

python 3.7
模拟 4.0.3
摩托 2.1.0
当然是 2.0.0
aiobotocore 1.4.2
boto3 1.18.2 botocore 1.20.106
覆盖率 5.5

需要实施或修复什么才能运行或通过测试?

【问题讨论】:

    标签: python amazon-ec2 mocking boto3 moto


    【解决方案1】:

    如果您询问如何在 Moto 中实现此功能,文档应该在这里提供帮助:http://docs.getmoto.org/en/latest/docs/contributing/new_feature.html

    TLDR:

    有一个脚本可以为新功能生成脚手架。

    • 查看 Moto 源代码
    • 安装项目 (make init)
    • 运行脚手架脚本:scripts/scaffold.py

    始终可以在 Moto 中打开问题或创建 PR 草案 - 如果您想贡献,他们很乐意提供帮助。 https://github.com/spulec/moto

    【讨论】:

      【解决方案2】:

      如果在 moto 库的更新或脚手架脚本之后,该方法尚未在库中实现并继续出现这样的错误:

      未实现错误: ElasticNetworkInterfaces(AmazonVPC).describe_network_interface_attribute 尚未实施

      一个简单的测试解决方案是一个monkeypatch函数,它可以模拟未实现的方法。

      def client_mock(Attribute='description'):
          return {
              'Attachment': {
                  'AttachTime': 34545,
                  'DeleteOnTermination': False,
                  'DeviceIndex': 123,
                  'NetworkCardIndex': 123,
                  'InstanceId': 'eni1.instanceid',
              },
              'Description': {
                  'Value': 'Vpc network inteface'
              },
              'Groups': [
                  {
                      'GroupName': 'name group',
                      'GroupId': 'grupoid'
                  },
              ],
              'NetworkInterfaceId': 'eni.id',
              'SourceDestCheck': {
                  'Value': False
              }
          }
      

      并将其与相应的函数或方法的传递属性放在一起

      monkeypatch.setattr(eni1, "describe_attribute", client_mock)
      

      如果您知道更好的解决方案,请发布!

      【讨论】:

        猜你喜欢
        • 2018-09-30
        • 2016-11-29
        • 1970-01-01
        • 2018-08-13
        • 2012-05-18
        • 2022-08-11
        • 1970-01-01
        • 2018-11-04
        • 2018-03-04
        相关资源
        最近更新 更多