繁体   English   中英

使用 Testcontainers/Localstack 和 Spring 进行集成测试 Boot:为临时文件设置目录

[英]Integration testing with Testcontainers/Localstack and Spring Boot: Set directory for temporary files

我正在使用 Testcontainers 的(Localstack 模块)[https://www.testcontainers.org/modules/localstack/] 通过 Spring Boot 进行高级集成测试,将 AmazonS3 客户端替换为来自 Localstack 的客户端。 我已经大致如下设置了我的集成测试(仅相关部分):

// FurnitureDetailsControllerIT.kt

@Testcontainers
@SpringBootTest
@ExtendWith(SpringExtension::class)
@AutoConfigureMockMvc
@DirtiesContext
class FurnitureDetailsControllerIT {

  @Autowired
  private val amazonS3: AmazonS3? = null

  
  companion object {
    @Container
    var localStack: LocalStackContainer =
      LocalStackContainer(DockerImageName.parse("localstack/localstack:latest")).withServices(LocalStackContainer.Service.S3)

    @JvmStatic
    @DynamicPropertySource
    fun properties(registry: DynamicPropertyRegistry) {
      registry.add("cloud.aws.s3.endpoint") { localStack.getEndpointOverride(LocalStackContainer.Service.S3) }
      registry.add("cloud.aws.credentials.access-key") { localStack.accessKey }
      registry.add("cloud.aws.credentials.secret-key") { localStack.secretKey }
    }
  }
(...)
}

在本地运行集成测试时一切都运行良好(从我的 IDE 或使用 Maven 的命令行),这意味着我可以在日志中看到 localstack 容器在旋转:

13:54:16.360 [main] INFO 🐳 [remote-docker.artifactory.mycompany.com/localstack/localstack:0.13.0] - Container remote-docker.artifactory.mycompany.com/localstack/localstack:0.13.0 started in PT4.6099665S

但是,在 Jenkins 上运行时,构建失败,因为出现错误消息。


2022-09-28 14:27:19.971  INFO 1185 --- [           main] ?.a.s.com/localstack/localstack:latest]  : Container remote-docker.artifactory.mycompany.com/localstack/localstack:latest is starting: ec383bda85f2636c3bef3f6b3938ac169636e4542244e3067b5184b6b03a6e35

2022-09-28 14:28:20.612 ERROR 1185 --- [           main] ?.a.s.com/localstack/localstack:latest]  : Could not start container

org.testcontainers.containers.ContainerLaunchException: Timed out waiting for log output matching '.*Ready\.

我猜这与Localstack需要的临时目录有关。 在我的 Windows 机器上运行时,我必须在测试恢复之前手动单击“Share It”:

用于文件共享的 Docker 桌面通知 (Windows)

我想因为测试在 Jenkins 上失败了(因为我不能在那里点击“Share It”)启动容器最终会超时并且测试会失败。

我读到您可以使用TMPDIR环境变量配置目录,如https://docs.localstack.cloud/localstack/configuration/中所述。 但是不推荐使用该变量。 将临时目录设置为 Jenkins 工作目录而不必明确设置为共享该目录的最佳方法是什么?

注意:我已经成功地使用了 Testcontainers 的MongoBD 模块,它启动了一个 dockerized MongoDB 实例。 这完美无缺,所以我不确定这是否是 Testcontainers 或 Localstack 的问题。

事实证明,这毕竟是公司代理的问题。 由于某些原因,LocalStack 需要在运行时下载依赖项:

来自https://docs.localstack.cloud/localstack/configuration/

OUTBOUND_HTTP_PROXY:HTTP 用于下载 LocalStack 自身外部的运行时依赖项和连接的代理

由于我们的 Jenkins 在公司代理后面,我必须设置以下环境变量: SKIP_SSL_CERT_DOWNLOADOUTBOUND_HTTP_PROXYOUTBOUND_HTTPS_PROXY 最重要的是,我不得不将版本硬编码为1.1.0 ,而不是使用latest标签。

所以我的代码现在看起来像这样:

    LocalStackContainer(DockerImageName.parse("localstack/localstack:1.1.0"))
      .withServices(LocalStackContainer.Service.S3)
      .withEnv("SKIP_SSL_CERT_DOWNLOAD", "true")
      .withEnv("OUTBOUND_HTTP_PROXY", "http://{proxy}:{port}")
      .withEnv("OUTBOUND_HTTPS_PROXY", "http://{proxy}:{port}")
      .apply { start() }

暂无
暂无

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

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