首页 > 解决方案 > 在本地环境中运行 testcontainer test OK 但在 jenkins 中失败

问题描述

背景

在我的应用程序中,它需要连接到 zookeeper 以获取一些有用的信息。因此,我在集成测试中启动了一个 Zookeeper 容器,并使用 -p 2859:2181 之类的端口绑定。

        zookeeper = new GenericContainer<>("zookeeper:3.6.1")
                .withEnv("ZOOKEEPER_CLIENT_PORT", "2181")
                .withExposedPorts(2181);
        zookeeper.setPortBindings(Arrays.asList("2859:2181"));
        zookeeper.start();

然后我使用以下命令运行测试。它顺利连接到 localhost:2859 没有任何问题。

mvn -Dtest=xxxIntegrationTest.java test

问题

但是,当我使用 jenkins 使用相同的命令运行相同的测试时,它无法连接到 localhost:2859,抱怨说

2020-10-28 12:16:04.282 INFO  [main-SendThread(localhost:2859)] Opening socket connection to server localhost/127.0.0.1:2859. Will not attempt to authenticate using SASL (unknown error)
2020-10-28 12:16:04.283 WARN  [main-SendThread(localhost:2859)] Session 0x0 for server null, unexpected error, closing socket connection and attempting reconnect
java.net.ConnectException: Connection refused
    at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
    at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717)
    at org.apache.zookeeper.ClientCnxnSocketNIO.doTransport(ClientCnxnSocketNIO.java:350)
    at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1068)

我不知道为什么它在这两个场景中表现不同。谁能给我一些建议?

标签: dockerjenkinstestcontainers

解决方案


推荐阅读