标签: testcontainers

使用 DockerComposeContainer 从 compose 文件仅启动一项服务

是否可以以某种方式在 Testcontainers 中仅运行特定服务DockerComposeContainer

按照官方文档的例子,我们可以只启动Redis吗?对我来说,所有服务始终启动(并且我是否使用该withExposedService方法并不重要)。

我的意思是这样的方法:

new DockerComposeContainer(new File("src/test/resources/compose-test.yml"))
        .composeUpService("redis_1")
Run Code Online (Sandbox Code Playgroud)

java docker docker-compose testcontainers

5
推荐指数
1
解决办法
936
查看次数

使用测试容器时“未找到可链接容器”

我想使用测试容器(https://www.testcontainers.org/usage.html

于是我导入了对应的Maven依赖:

<dependency>
    <groupId>org.testcontainers</groupId>
    <artifactId>testcontainers</artifactId>
    <version>1.10.1</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.testcontainers</groupId>
    <artifactId>oracle-xe</artifactId>
    <version>1.10.1</version>
    <scope>test</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)

然后我右键单击任务栏上的 docker 图标 -> 设置 -> 常规并检查该项目:

Expose daemon on tcp://localhost:2375 without TLS
Run Code Online (Sandbox Code Playgroud)

按照 testcontainers 站点上的说明设置环境变量:

DOCKER_CERT_PATH=C:\Users\username\.docker
DOCKER_HOST=https://localhost:2375
DOCKER_TLS_VERIFY=1
Run Code Online (Sandbox Code Playgroud)

并使用以下代码创建了 JUnit 测试:

@Test
public void test() {
   OracleContainer oracleXE = new OracleContainer();
...
Run Code Online (Sandbox Code Playgroud)

但是我得到了错误:

Error:(82, 27) java: cannot access org.testcontainers.containers.traits.LinkableContainer
  class file for org.testcontainers.containers.traits.LinkableContainer not found
Run Code Online (Sandbox Code Playgroud)

我在谷歌上搜索了“linkablecontainer not found”和“org.testcontainers.containers.traits.LinkableContainer not found”,但没有结果。

有什么想法出了什么问题吗?

java docker testcontainers

5
推荐指数
1
解决办法
516
查看次数

错误无法连接到 CircleCi 中的 Ryuk

CircleCI 的配置。在本地计算机上,当您运行 CircleCI 时,一切都会过去。在这种情况下,服务器出现了很多错误,其中之一就是

java.lang.IllegalStateException: Can not connect to Ryuk

同时,将来在早期启动的容器中连接测试时会出现错误test-containers,我认为这是由于连接到 时出现错误Ryuk。令人困惑的是,在本地计算机上一切正常,而在服务器上一切正常。

java scala circleci testcontainers

5
推荐指数
1
解决办法
2862
查看次数

如何存储测试容器中的容器日志?

Test-containers项目允许从 compose 文件启动 docker 容器。它们与 JUnit 集成。但是当测试阶段完成时,我所拥有的只是 Maven 日志 - 那时所有启动的容器都已被删除。例如,如果我运行容器 4 次进行 4 次测试,那就太好ApplicationDatabase。所以maven目标文件夹应该包含以下内容:target/test-containers/<start-id>/Application.logtarget/test-containers/<start-id>/Database.log.

有没有办法配置测试容器或 JUnit 或两者将日志从已启动的容器重定向到某个文件夹?

java junit maven docker testcontainers

5
推荐指数
0
解决办法
1806
查看次数

使用插件启动ElasticSearch的测试容器

我正在使用 testcontainers.org docker.elastic.co/elasticsearch/elasticsearch-oss:7.3.2,我想用它来测试我正在更新的插件,但我找不到在测试环境中安装它的方法。

我可以尝试复制里面的文件并安装它

ElasticsearchContainer container = new ElasticsearchContainer("docker.elastic.co/elasticsearch/elasticsearch-oss:$ELASTIC_VERSION")
String pluginPath = "/usr/share/elasticsearch/$PLUGIN_FILE_NAME"
container.start()
container.copyFileToContainer(MountableFile.forHostPath(new File(PLUGIN_PLUGIN_PATH).toPath()), pluginPath)
ExecResult result = container.execInContainer("bin/elasticsearch-plugin", "install", "file://$pluginPath")
Run Code Online (Sandbox Code Playgroud)

但是容器已经启动并且弹性搜索已经在运行,所以插件不会被加载,所以我需要杀死它并复制它的创建方式,听起来像是很多黑客攻击。有更好的方法来做到这一点吗?

java elasticsearch testcontainers

5
推荐指数
1
解决办法
1599
查看次数

Java Testcontainers - 无法连接到公开端口

我使用 javax.mail 实现了 POP3 服务器和客户端,只是为了尝试使用 Docker 进行集成测试。因此,我基于 openjdk:8-jre 映像创建了两个 Docker 映像,并将我的 jar 复制到其中并启动它。根据我的配置(见下文),它正在工作。他们正在互相交谈。

但是,由于想要进行多个集成测试,为每个测试构建一个映像并启动它们将是一件很乏味的事情。我也不知道如何自动化结果。但后来我偶然发现了 TestContainers,这似乎对实施这些测试有很大帮助。

因此,我开始使用 POP3 服务器映像作为 GenericContainer 将这些测试移植到 TestContainers,并在 JUnit 测试方法中启动我的 POP3 客户端类。我公开了 POP3 服务器正在侦听的端口 24999。但是当我尝试连接到服务器时,出现以下错误:

com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localhost, 32782; timeout -1;
  nested exception is:
    java.net.ConnectException: Connection refused
...
Run Code Online (Sandbox Code Playgroud)

TestContainers 中可能缺少一些设置。请你帮助我好吗。

这是我正在使用的代码:

public class DockerPop3AutocryptKeyProvidingAndReceivingTest {
    @Test
    public void test() throws InterruptedException {
        GenericContainer container = new GenericContainer<>("immerfroehlich/emailfilter:latest")
                .withExposedPorts(24999);
        
        container.start();
        
        String host = container.getContainerIpAddress();
        String port = container.getFirstMappedPort().toString();

        //The following is simplified, but copied …
Run Code Online (Sandbox Code Playgroud)

java junit docker dockerfile testcontainers

5
推荐指数
2
解决办法
2万
查看次数

使用Flyway和Spring boot迁移docker testcontainers环境中的模式

我正在尝试在 Spring Boot 应用程序中使用 testcontainers 和 Flyway 设置测试环境。所有这一切都应该通过 DinD 方案运行。

目前测试示例如下:

import com.testapp.testapp.entity.TestEntity
import com.testapp.testapp.service.TestService
import org.junit.Test
import org.junit.runner.RunWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.context.annotation.Import
import org.springframework.test.context.ContextConfiguration
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner
import org.testcontainers.containers.PostgreSQLContainer
import org.testcontainers.spock.Testcontainers
import spock.lang.Shared
import spock.lang.Specification

@Testcontainers
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@SpringBootTest
@Import([FlywayAutoConfiguration.class])
class ApplicationTests extends Specification {

    @Shared
    PostgreSQLContainer postgreSQLContainer = new PostgreSQLContainer()
            .withDatabaseName("testdb")
            .withUsername("postgres")
            .withPassword("12345")

    @Autowired
    public TestService testappService;

    @Test
    def "entity_created"(){
        given: "init test entity"
        UUID uuid = UUID.randomUUID();
        when: "create test entity"
        def testEntity = …
Run Code Online (Sandbox Code Playgroud)

spock docker spring-boot-test testcontainers

5
推荐指数
1
解决办法
5372
查看次数

使用 testcontainers 播种 mysql 数据库

我的情况非常简单:AWS 中现有的 mysql 数据库我想使用 testcontainers 进行测试。

\n\n

我遵循了官方指南(https://www.testcontainers.org/modules/databases/)及其示例(https://github.com/testcontainers/testcontainers-java/blob/master/modules/jdbc-test/ src/test/java/org/testcontainers/junit/SimpleMySQLTest.java),但是我无法加载使用 mysqldump 创建的转储。

\n\n

每次我都会收到以下错误:

\n\n
Caused by: java.sql.SQLSyntaxErrorException: Access denied; you need (at least one of) the SUPER privilege(s) for this operation\n
Run Code Online (Sandbox Code Playgroud)\n\n

根据其他用户的建议,我尝试了以下方法:

\n\n
MySQLContainer mysql = (MySQLContainer) new MySQLContainer()\n                .withInitScript("foodmart_department.sql")\n                .withUsername("root")\n                .withPassword("test")\n                .withLogConsumer(new Slf4jLogConsumer(logger));\n        mysql.start();\n
Run Code Online (Sandbox Code Playgroud)\n\n

但它挂起几分钟并因以下错误而终止:

\n\n
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".\nSLF4J: Defaulting to no-operation (NOP) logger implementation\nSLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.\n\n        \xe2\x84\xb9\xef\xb8\x8e Checking the system...\n        \xe2\x9c\x94 Docker version should be at …
Run Code Online (Sandbox Code Playgroud)

java mysql testcontainers

5
推荐指数
1
解决办法
5677
查看次数

如何覆盖 testcontainers 中的 jna.tmpdir 和 java.io.tmpdir 属性?

TestContainers 有 Native.java 类,它从“jna.tmpdir”键获取 JNA 属性。如何覆盖该键的值?喜欢:

\n\n
jna.tmpdir=/data/builds/compose-tmp \njava.io.tmpdir=/data/builds/compose-tmp\n
Run Code Online (Sandbox Code Playgroud)\n\n

获取 jna 属性的部分代码:\xc2\xa0

\n\n
\npackage com.sun.jna;\n\npublic final class Native implements Version {\n\n...\n\n   static File getTempDir() throws IOException {\n       File jnatmp;\n       String prop = System.getProperty("jna.tmpdir");\n       if (prop != null) {\n           jnatmp = new File(prop);\n           jnatmp.mkdirs();\n       }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

testcontainers

5
推荐指数
0
解决办法
1260
查看次数

无法获取 Docker 镜像

我有 5 个春季批量测试,正在使用 testcontainers 在 Jenkins 管道上运行。我仅在其中一项测试中收到以下错误:

\n\n
12:51:53  17 Mar 2020;17:51:41.498 [user:] [request:] [main] INFO \n12:51:53                  o.testcontainers.DockerClientFactory - Ryuk started - will monitor and terminate Testcontainers containers on JVM exit\n12:51:53          [37m[1m\xe2\x84\xb9\xef\xb8\x8e Checking the system...[0m[0m\n12:51:53          [32m\xe2\x9c\x94 Docker version should be at least 1.6.0[0m\n12:51:53  17 Mar 2020;17:51:41.508 [user:] [request:] [main] DEBUG\n12:51:53                  c.g.d.core.command.AbstrDockerCmd - Cmd: f6fc0588843eb76a458bdc25cdc942fed474945aa3cfe7b635220dd1935dbfaa,<null>,true,<null>,<null>,<null>,<null>,{df,-P},<null>,<null>,com.github.dockerjava.core.exec.ExecCreateCmdExec@5cbe877d\n12:51:53  17 Mar 2020;17:51:41.644 [user:] [request:] [tc-okhttp-stream-2116511124] DEBUG\n12:51:53                  c.g.d.c.c.ExecStartResultCallback - STDOUT: Filesystem           1024-blocks    Used Available Capacity Mounted on\n12:51:53  overlay              314561516  62506464 252055052  20% /\n12:51:53  tmpfs                    65536         0 …
Run Code Online (Sandbox Code Playgroud)

java spring jenkins-pipeline testcontainers

5
推荐指数
1
解决办法
2万
查看次数