简体   繁体   English

如何在集成测试中模拟 Amazon S3

[英]How to mock Amazon S3 in an integration test

I'm trying to get an "walking skeleton" of my app up that will use S3 for persistence.我正在尝试构建我的应用程序的“行走骨架”,它将使用 S3 进行持久性。 I would like to use a fake S3 service so each developer's desktop can read/write at will.我想使用一个假的 S3 服务,这样每个开发人员的桌面都可以随意读/写。

I thought mocks3 would be perfect, as I could get a jetty server up in my jUnit tests.我认为mocks3会很完美,因为我可以在我的 jUnit 测试中安装一个码头服务器。 The problem is that mocks3 doesn't allow any writes .问题是 mocks3 不允许任何writes Not even to set it up as far as I can tell.据我所知,甚至没有设置它。

So how do others do this?那么其他人是如何做到这一点的呢?

There is also an Findify s3mock tool written exactly for this purpose.还有一个专门为此目的编写的 Findify s3mock工具。 It mocks the essential parts of AWS S3 API on top of local filesystem:它在本地文件系统之上模拟 AWS S3 API 的基本部分:

import io.findify.s3mock.S3Mock

S3Mock api = S3Mock.create(8001, "/tmp/s3");
api.start();

AmazonS3Client client = new AmazonS3Client(new AnonymousAWSCredentials());
// use local API mock, not the AWS one
client.setEndpoint("http://127.0.0.1:8001");
client.createBucket("testbucket");
client.putObject("testbucket", "file/name", "contents");

It's also easily embeddable and configuration-less.它还易于嵌入且无需配置。

Another option is S3 ninja - emulates the S3 API for development and testing purposes.另一种选择是S3 ninja - 模拟 S3 API 用于开发和测试目的。

If you're ok with depending on a running docker container, and want something well-supported, you could consider using localstack如果您可以依赖正在运行的 docker 容器,并且想要得到很好的支持,您可以考虑使用localstack

Before running your tests, start S3 like so:在运行测试之前,像这样启动 S3:

docker run --name localstack -d -p 5000:5000 -e SERVICES=s3:5000 localstack/localstack

And then stop it when tests complete like so:然后在测试完成时停止它,如下所示:

docker stop localstack

You'll need to configure your S3 client to point to localhost:5000 for tests.您需要将 S3 客户端配置为指向 localhost:5000 进行测试。 In Java, this can be done like so:在 Java 中,可以这样做:

        AmazonS3ClientBuilder.standard()
            .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(
                 "http://localhost:5000", 
                 "us-west-2"))
            .build();

Have a look at Adobe's S3Mock .看看Adobe 的 S3Mock This S3 Mock server can be started via a Docker container or JUnit 4/5 rules.此 S3 Mock 服务器可以通过 Docker 容器或 JUnit 4/5 规则启动。

Tornado, a python web framework, has an example app that is just what you're looking for. Tornado 是一个 python web 框架,它有一个示例应用程序,正是您正在寻找的。

https://github.com/facebook/tornado/blob/master/demos/s3server/s3server.py https://github.com/facebook/tornado/blob/master/demos/s3server/s3server.py

It can be used out of the box.它可以开箱即用。

You can use scality s3server, in can run on your machine either using node.js or via docker and it gives you a local S3 service instance.您可以使用 scality s3server,可以使用 node.js 或 docker 在您的机器上运行,它为您提供本地 S3 服务实例。 It's open source under a BSD license github.com/scality/s3它是在 BSD 许可下开源的github.com/scality/s3

One option is to scrap the jetty server and use Apache VFS and the S3 plugin .一种选择是废弃码头服务器并使用Apache VFSS3 插件 With that, you can use the memory or file-based storage implementations for the integration testing.这样,您可以使用 memory 或基于文件的存储实现进行集成测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM