- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我目前正在努力适应 TDD,在我正在进行的当前项目中,我正在尝试利用 AWS 的 Go SDK。这一切都很好,花花公子,我以前用过它,但我目前正在尝试模拟 *ec2.DescribeVolumesOutput
发送的值。
深入研究代码,我将其视为 *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"`
}
好吧.. 这很酷,但我想模拟的输出必须位于 Volumes []*Volume
locationName:"volumeSet"locationNameList:"item"type:"list"`所以让我们更深入一点,看看那是什么......
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"`
}
不错!这看起来像是我想模拟其值的一些数据!
但在过去的几天里,我并没有真正 mock 这些值(value)观。它们是否如此嵌套以至于这种类型的 mock 不值得付出努力?即使尝试使用 github.com/aws/aws-sdk-go/service/ec2/ec2iface
似乎也无法帮助我解决如何正确打包一些模拟值返回以进行测试的问题。我来参加 TDD 是不是全错了?我错过了一些非常明显的东西吗?我真的没有示例代码可以展示,因为我现在不再理解我想要做什么。
有没有人可以举例说明他们是如何 mock 这个的?
最佳答案
没有办法模拟类型,你只能模拟接口(interface)的实现。在你的情况下,我假设你正在尝试调用 DescribeVolumes并在响应中获取由您构造的值。
为此,您需要创建一个像
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
}
在测试中创建 MockEC2API
实例而不是真正的 ec2.EC2
并为其提供将被调用的函数并返回您准备好的 ec2.DescribeVolumesOutput
结果
...
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
关于amazon-web-services - 你如何模拟 *ec2.DescribeVolumes 输出的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56366301/
尽管我的 EC2 实例(带有针对 ECS 优化的 AIM)正在运行 ecs 代理,但容器并未在我的 EC2 中启动。为了确认 ecs-agent 正在我的 EC2 实例上运行,我检查了: ecs 日志
我想在一个任务定义中使用客户端和服务器在 aws ecs 中运行套接字程序。当我使用 awsvpc 网络模式并每次连接到本地主机上的服务器时,我都可以运行它。这很好,所以我不需要知道服务器的 IP 地
我有一个在 AWS ECS 上运行的 Docker 容器。我不想要负载均衡器,因为容器是用来处理 websocket 连接的。 https://aws.amazon.com/getting-start
我想在我的 AWS ECS/Fargate 集群上的公共(public) Docker 镜像中启动交互式 shell,以便从集群内部运行网络/连接测试。 似乎官方的方法是使用 aws ecs run-
我在 ECS Fargate 上托管了一个 Shiny 的应用程序。它运行得相当好,但偶尔在使用该应用程序时它会崩溃。我在事件选项卡中将其追溯到以下内容: service YYYY has start
在 AWS ECS 中创建容量提供程序时。我们正在填充的值 Target capacity %,在超过这个值后我们的集群缩小,但我很好奇当前集群的这个值是如何计算的,如果我想检查当前的是什么集群的值(
在 AWS ECS 中创建容量提供程序时。我们正在填充的值 Target capacity %,在超过这个值后我们的集群缩小,但我很好奇当前集群的这个值是如何计算的,如果我想检查当前的是什么集群的值(
我在 AWS 上设置了具有多个目标组和应用程序负载均衡器的 Fargate ecs 服务。它按预期运行。 然后我尝试使用本教程设置管道 https://docs.aws.amazon.com/Amaz
我有一个运行任务的 ECS 集群服务,可以使用 AWS 控制台查看其 Cloudwatch 日志流。 如何为 awslogs-stream-prefix 设置日期,因为我想按日期识别日志流。 "c
我正在使用两个容器部署 ECS Fargate 任务:1 个反向代理 nginx 和 1 个 python 服务器。对于每个我有一个 ECR 存储库,我有一个 CI/CD CodePipeline 设
我在 AWS ECS 上部署了一个 CloudFormation 堆栈,比如 teststack , 通过命令 aws cloudformation deploy --template-file ./
我这里有一些具体的用例。我需要自动扩展在 ECS Fargate 上运行的分布式 Web 应用程序。问题是所有节点都需要在内存中保留相同的数据(因此增加节点数量无助于内存压力)。因此,只有在水平(添加
我正在寻找有关为 ECS 任务分配内存的指南。我正在为希望在服务器成本上尽可能便宜的客户运行 Rails 应用程序。我正在查看具有 2 个 CPU 和 4 GB 内存的中等服务器大小。 大多数情况下,
我是 ECS 新手,我正在尝试使用 Cloudformation 部署它。 我通过查看文档以及从博客和一些文章中找到的一些示例来制作以下 cloudformation 模板。 但是,由于某种原因,它在
也许这很愚蠢,但如果我创建一个 ECS 任务定义(例如参见 https://aws.amazon.com/blogs/compute/better-together-amazon-ecs-and-aw
ECS 的容器定义允许您指定 memoryReservation对于每个容器: The soft limit (in MiB) of memory to reserve for the contain
我在 ca-central 区域创建了一个 AWS ECS 实例。它与每次更新服务时都会更改的动态公共(public) ip 一起使用。到目前为止一切都很好。 由于需要一个公网静态IP,所以我在同一区
Invalid 'containerPort' setting for container 'prerenderContainer'.(Service: AmazonECS; Status Code:
如果我在 ECS 服务级别进行自动扩展,我看不到使用容量提供程序扩展 ECS 集群的意义: https://docs.aws.amazon.com/AmazonECS/latest/developer
关闭。这个问题不符合 Stack Overflow guidelines 。它目前不接受答案。 想改进这个问题?更新问题,使其成为 Stack Overflow 的 on-topic。 1年前关闭。
我是一名优秀的程序员,十分优秀!