简体   繁体   English

是否可以有条件地使用测试容器与本地运行的 Docker 容器?

[英]Is it possible to conditionally use test containers vs a locally running Docker container?

My company's application uses test containers for multiple images, plus flyway to setup schemas, etc. Our test suite is robust but long running.我公司的应用程序使用多个图像的测试容器,以及设置模式的 flyway 等。我们的测试套件功能强大但运行时间长。 So I'm looking for ways to speed up the test suite execution.所以我正在寻找加速测试套件执行的方法。 This is primarily for devs running the test suite on our local machines, vs something for the CI/CD where the overall execution time is less annoying.这主要适用于在我们的本地机器上运行测试套件的开发人员,而对于 CI/CD 来说,整体执行时间不那么烦人。

To clarify, our suite takes ~5mins to run, so it's not like catastrophically bad.澄清一下,我们的套件需要大约 5 分钟才能运行,所以它并不像灾难性的那样糟糕。 But I'm trying to cut that down.但我正在努力减少它。

One idea I have is related to the use of testcontainers.我的一个想法与使用测试容器有关。 Starting the suite takes 45-60 secs, so eliminating this overhead will be very beneficial.启动套件需要 45-60 秒,因此消除此开销将非常有益。

My thought is using local Docker containers for local development.我的想法是使用本地 Docker 容器进行本地开发。 Rather than creating the whole container from scratch when starting the suite, we could set everything up via Docker Compose or something like that, and use those containers for the test.与其在启动套件时从头开始创建整个容器,我们可以通过 Docker Compose 或类似的工具设置所有内容,然后使用这些容器进行测试。 This would be controlled by an environment variable, so this could be optionally turned on.这将由环境变量控制,因此可以选择打开。

We're using the @Testcontainers framework integration, and I don't see any way to conditionally control the instantiation of testcontainers.我们正在使用@Testcontainers 框架集成,我看不出有任何方法可以有条件地控制测试容器的实例化。 I'm hoping there is some solution out there.我希望那里有一些解决方案。

I recognize 2 questions in this one, I'll try to answer both:我在这个问题中认识到 2 个问题,我将尝试回答这两个问题:

  • A: Hybrid running of TestContainers controlled and Locally started containers; A:TestContainers控制和Locally启动的容器混合运行;
  • B: Speeding up tests by avoiding the full flyway migrations. B:通过避免完整的飞行路径迁移来加速测试。

About Hybrid running:关于混合运行:

As far as I know, TestContainers doesn't support this out of the box.据我所知,TestContainers 不支持这个开箱即用。

You could however work around this by using Spring Profiles .但是,您可以使用Spring Profiles解决此问题。 Have 1 profile (for the build server), where the containers are started by TestContainers, and one where you setup the properties to connect with your prestarted containers.有 1 个配置文件(用于构建服务器),其中容器由 TestContainers 启动,还有一个配置文件用于设置属性以与预先启动的容器连接。

About Speeding up tests:关于加速测试:

I see you are using Flyway for migrations.我看到您正在使用 Flyway 进行迁移。 If you use TestContainers (and an empty database), those scripts will run each time, which might be timeconsuming.如果您使用 TestContainers(和一个空数据库),这些脚本每次都会运行,这可能很耗时。 You could commit a container into a new image with those data/migrations already completed.您可以将容器提交到已完成这些数据/迁移的新映像中。 I described that in this answer: https://stackoverflow.com/a/72265528/2082699我在这个答案中描述了这一点: https ://stackoverflow.com/a/72265528/2082699

If you manually control the container lifecycle, rather than using the @Testcontainers integration, this is possible.如果您手动控制容器生命周期,而不是使用@Testcontainers集成,这是可能的。 You can check if a service is already available on a known port and if not, you start a Testcontainers-based container instead and configure your SUT accordingly.您可以检查服务是否在已知端口上可用,如果没有,则启动基于 Testcontainers 的容器并相应地配置您的 SUT。

Furthermore, as others have mentioned, you can use the withReuse(true) feature and also enable reuse in your ~/.testcontainers.properties :此外,正如其他人所提到的,您可以使用withReuse(true)功能并在~/.testcontainers.properties中启用重用:

testcontainers.reuse.enable = true

This will make containers survive the JVM process lifecycle.这将使容器在 JVM 进程生命周期中存活下来。

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

相关问题 在 docker 容器内运行 maven 集成测试 - Running maven integration test inside docker container 是否可以在 Docker 容器中使用whatsapp web? - Is it possible to use whatsapp web in a Docker container? 在Docker容器中运行时偶发的Robolectric测试失败 - Sporadic Robolectric test failures when running inside Docker container 使用 Jib 插件的 Docker 图像:容器运行时无法在本地命中 rest 端点? - Docker image using Jib plugin: Cant hit rest endpoint locally when container running? JUnit有条件地运行测试方法 - JUnit running test methods conditionally 微服务部署 --- 简单的 jars 与 docker 容器 - Microservice deployment --- simple jars vs docker containers 多 WAR tomcat 与 Docker 容器 - Multi-WAR tomcat vs Docker containers 在AWS上使用Kubernetes创建和运行docker容器集群 - Creating and running docker containers cluster with Kubernetes on AWS 在Docker容器中使用Flyway和嵌入式Postgresql运行测试时出现java.net.ConnectException - java.net.ConnectException when running test with Flyway and embedded Postgresql inside Docker container 多容器 Docker 应用程序 - 容器之间的连接被拒绝 - Multi Container Docker app - Connection Refused between containers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM