简体   繁体   English

Amazon Kinesis + 集成测试

[英]Amazon Kinesis + Integration Tests

I'm currently working on a series of web-services which we need to integrate with Kinesis - the implementation has been done, however we have a series of integration tests (our web-services are all using Spring Boot so we use the @WebIntegrationTest annotation on our test classes to start up a local instance of the server and then call our resources with a TestRestTemplate ) which are currently trying and failing to connect to the real Kinesis.我目前正在研究一系列需要与 Kinesis 集成的网络服务 - 实现已经完成,但是我们有一系列集成测试(我们的网络服务都使用 Spring Boot,所以我们使用@WebIntegrationTest在我们的测试类上添加注释以启动服务器的本地实例,然后使用TestRestTemplate调用我们的资源,这些资源当前正在尝试连接到真正的 Kinesis,但未能成功。

Although in ordinary unit tests it's not a problem to mock out calls to the methods within the Kinesis library, we can't really do this in the integration tests as the whole application stack is wired up with Spring.尽管在普通的单元测试中模拟对 Kinesis 库中方法的调用不是问题,但在集成测试中我们无法真正做到这一点,因为整个应用程序堆栈都与 Spring 相连。 For a few other things (such as OAuth2 and calls to our other web-services) we've been able to use WireMock to mock out the actual endpoints - what I'd really like to do is use WireMock in this fashion to mock out the call to the AmazonKinesisClient but I can't find any advice on how to do this.对于其他一些事情(例如 OAuth2 和对我们其他网络服务的调用),我们已经能够使用 WireMock 来模拟实际端点 - 我真正想做的是以这种方式使用 WireMock 来模拟对AmazonKinesisClient的调用,但我找不到有关如何执行此操作的任何建议。

Alternatively I have seen that some AWS components have test libraries written by third parties which allow you to run a local version of it (eg: DynamoDbLocal) but can't find such a solution for Kinesis.或者,我看到一些 AWS 组件具有第三方编写的测试库,允许您运行它的本地版本(例如:DynamoDbLocal),但找不到 Kinesis 这样的解决方案。

Is anyone able to give me some advice on how to run integration tests with Kinesis?有没有人能给我一些关于如何使用 Kinesis 运行集成测试的建议?

It might already be too late to give the solution but I will add what my team has done to replicate AWS resources locally as we use a lot of Kinesis, DynamoDb, S3 and cloudWatch. 提供解决方案可能已经太晚了,但我将添加我的团队在本地复制AWS资源所做的工作,因为我们使用了大量的Kinesis,DynamoDb,S3和cloudWatch。

We have created wrappers around Localstack -> https://github.com/localstack/localstack that allow us to spin up local instances of the necessary services as docker containers using docker-compose . 我们在Localstack周围创建了包装器 - > https://github.com/localstack/localstack ,它允许我们使用docker-compose将所需服务的本地实例作为docker容器。

A typical docker-compose.yml file for us looks like: 我们的典型docker-compose.yml文件如下所示:

version: '2'
services:
  localstack:
    image: "localstack/localstack"
    environment:
      - SERVICES=kinesis,dynamodb,cloudwatch
    ports:
      - "4568"
      - "4569"
      - "4582"

Then during the setup phase for the integration-tests, our wrapper fires up docker-compose up and runs the tests against the local infrastructure. 然后,在集成测试的设置阶段,我们的包装器启动docker-compose up并针对本地基础结构运行测试。 Later during tear-down, the wrapper kills the containers. 在拆卸过程中,包装机会杀死容器。

I ran into the same issue and the only mock implementation I found so far was a nodejs one: https://github.com/mhart/kinesalite It does the trick - I managed to run my Java Kinesis client against it, just had to set the endpoint on the kinesis.properties: 我遇到了同样的问题,到目前为止我发现的唯一一个模拟实现是一个nodejs: https//github.com/mhart/kinesalite这样做 - 我设法运行我的Java Kinesis客户端,只需要在kinesis.properties上设置端点:

kinesisEndpoint=http://localhost:4567

The downside is that it is not trivial to use it during build time tests - need to figure a way to start the mock kinesis before the test (using a maven plugin or something), didn't get to it yet.. 缺点是在构建时测试期间使用它并不是一件容易的事 - 需要想方设法在测试之前启动模拟运动(使用maven插件或其他东西),但还没达到它...

Just a small addition to the existing answers.只是对现有答案的一个小补充。 BTW, they are great, you should really use tools like localstack to start fake AWS services before the test during the test phase.顺便说一句,它们很棒,在测试阶段,您真的应该在测试之前使用localstack 之类的工具来启动虚假的 AWS 服务。

If you're using JUnit 5 in your tests, your life could be even simpler with JUnit 5 extensions for AWS , a few JUnit 5 extensions that could be useful for testing AWS-related code.如果您在测试中使用 JUnit 5,使用适用于 AWS 的 JUnit 5 扩展,您的生活可能会更加简单,一些 JUnit 5 扩展可用于测试与 AWS 相关的代码。 These extensions can be used to inject clients for AWS service mocks provided by tools like localstack.这些扩展可用于为 localstack 等工具提供的 AWS 服务模拟注入客户端。 Both AWS Java SDK v 2.x and v 1.x are supported:支持 AWS Java SDK v 2.x 和 v 1.x:

@ExtendWith(DynamoDB.class)
class AmazonDynamoDBInjectionTest {
    @AWSClient(
        endpoint = Endpoint.class
    )
    private AmazonDynamoDB client;

    @Test
    void test() throws Exception {
        Assertions.assertNotNull(client);

        Assertions.assertEquals(
            Collections.singletonList("table"),
            client.listTables().getTableNames().stream().sorted().collect(Collectors.toList())
        );
    }
}

Here, client will be just injected in your test class and configured according to the Endpoint configuration class.在这里,客户端将仅注入您的测试类并根据Endpoint配置类进行配置。

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

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