douju2331 2019-05-29 18:08
浏览 27
已采纳

如何模拟* ec2.DescribeVolumesOutput的值

I am currently trying to get used to TDD and on a current project I am working on I am trying to leverage AWS's Go SDK. Which is all fine and dandy and I have used it before but I am currently trying to mock the value that *ec2.DescribeVolumesOutput sends.

Diving into the code I see this as what returns for *ec2.DescribeVolumesOutput:

type DescribeVolumesOutput struct {
    _ struct{} `type:"structure"`

    // The NextToken value to include in a future DescribeVolumes request. When
    // the results of a DescribeVolumes request exceed MaxResults, this value can
    // be used to retrieve the next page of results. This value is null when there
    // are no more results to return.
    NextToken *string `locationName:"nextToken" type:"string"`

    // Information about the volumes.
    Volumes []*Volume `locationName:"volumeSet" locationNameList:"item" type:"list"`
}

Okay.. That's cool, but what I want to mock the output of must live inside of Volumes []*VolumelocationName:"volumeSet" locationNameList:"item" type:"list"` so let's go a little deeper and see what that is...

type Volume struct {
    _ struct{} `type:"structure"`

    // Information about the volume attachments.
    Attachments []*VolumeAttachment `locationName:"attachmentSet" locationNameList:"item" type:"list"`

    // The Availability Zone for the volume.
    AvailabilityZone *string `locationName:"availabilityZone" type:"string"`

    // The time stamp when volume creation was initiated.
    CreateTime *time.Time `locationName:"createTime" type:"timestamp"`

    // Indicates whether the volume will be encrypted.
    Encrypted *bool `locationName:"encrypted" type:"boolean"`

    // The number of I/O operations per second (IOPS) that the volume supports.
    // For Provisioned IOPS SSD volumes, this represents the number of IOPS that
    // are provisioned for the volume. For General Purpose SSD volumes, this represents
    // the baseline performance of the volume and the rate at which the volume accumulates
    // I/O credits for bursting. For more information, see Amazon EBS Volume Types
    // (https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSVolumeTypes.html)
    // in the Amazon Elastic Compute Cloud User Guide.
    //
    // Constraints: Range is 100-16,000 IOPS for gp2 volumes and 100 to 64,000IOPS
    // for io1 volumes, in most Regions. The maximum IOPS for io1 of 64,000 is guaranteed
    // only on Nitro-based instances (https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-types.html#ec2-nitro-instances).
    // Other instance families guarantee performance up to 32,000 IOPS.
    //
    // Condition: This parameter is required for requests to create io1 volumes;
    // it is not used in requests to create gp2, st1, sc1, or standard volumes.
    Iops *int64 `locationName:"iops" type:"integer"`

    // The full ARN of the AWS Key Management Service (AWS KMS) customer master
    // key (CMK) that was used to protect the volume encryption key for the volume.
    KmsKeyId *string `locationName:"kmsKeyId" type:"string"`

    // The size of the volume, in GiBs.
    Size *int64 `locationName:"size" type:"integer"`

    // The snapshot from which the volume was created, if applicable.
    SnapshotId *string `locationName:"snapshotId" type:"string"`

    // The volume state.
    State *string `locationName:"status" type:"string" enum:"VolumeState"`

    // Any tags assigned to the volume.
    Tags []*Tag `locationName:"tagSet" locationNameList:"item" type:"list"`

    // The ID of the volume.
    VolumeId *string `locationName:"volumeId" type:"string"`

    // The volume type. This can be gp2 for General Purpose SSD, io1 for Provisioned
    // IOPS SSD, st1 for Throughput Optimized HDD, sc1 for Cold HDD, or standard
    // for Magnetic volumes.
    VolumeType *string `locationName:"volumeType" type:"string" enum:"VolumeType"`
}

Nice! this looks like some data that I want to mock the values of!

But over the last couple of days I have had no luck in actually mocking these values. Are they so nested that this type of mocking is not worth the effort? Even trying to use the github.com/aws/aws-sdk-go/service/ec2/ec2iface does not seem to help me wrap my head around how to properly package some mock value returns to test. Am I coming at TDD all wrong? am I missing something super obvious? I do not really have example code to show since I now no longer understand what I am trying to do.

Does anyone possibly have an example of how they have mocked this?

  • 写回答

1条回答 默认 最新

  • douguanyan9928 2019-05-29 21:37
    关注

    There is no way to mock the type, you are able to mock only implementation of the interface. In your case I assume that you are trying to call DescribeVolumes and in the response get value constructed by you.
    To do that you need to create a mock like

    type MockEC2API struct {
        ec2iface.EC2API // embedding of the interface is needed to skip implementation of all methods
        DescribeVolumesMethod func(*ec2.DescribeVolumesInput) (*ec2.DescribeVolumesOutput, error)
    }
    
    func (m *MockEC2API) DescribeVolumes(in *ec2.DescribeVolumesInput) (*ec2.DescribeVolumesOutput, error) {
        if m.DescribeVolumesMethod != nil {
            return m.DescribeVolumesMethod(in)
        }
        return nil, nil // return any value you think is good for you
    }
    

    In the test create instance of MockEC2API instead of the real ec2.EC2 and provide it with function that will be called and return your's prepared ec2.DescribeVolumesOutput result

    ...
    ec2 := &MockEC2API{
        DescribeVolumesMethod: func(*ec2.DescribeVolumesInput) (*ec2.DescribeVolumesOutput, error) {
            return &ec2.DescribeVolumesOutput{...your initialization...}, nil
        }
    }
    ...
    output, err := ec2.DescribeVolumes(in) // this output will be your prepared initialization
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于RTKLIB框架写的精密单点定位-AR
  • ¥15 PFENet的预训练权重
  • ¥15 程序哪有错误怎么改?
  • ¥15 交换机和交换机之间的链路带宽以及主机带宽的理解
  • ¥15 ai创想家对战模式代码
  • ¥15 集合A由3个2行4列二维数组构成,从集合A中任意取一个二维数组元素、如果该二维数组元素的对应列位置的上、下两数都是奇数,而且仅有2个列是奇数/奇数,则该数组有意义,并放入集合B中打印输出。
  • ¥15 电信IPV6 无法外网访问吗
  • ¥15 有偿求效果比较好的遥感影像匹配的c++代码
  • ¥15 博主,你好,我下载了你的智能网联汽车辅助驾驶安全信息检测系统,现在不会运行,可以教我吗,
  • ¥15 怎么在excle输入下列公式