0

Background

In my application, it needs to connect to zookeeper to get some useful information. So I start a zookeeper container in my integration test with port binding like -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();

Then I use the following command to run the test. It goes smoothly connecting to localhost:2859 without any problems.

mvn -Dtest=xxxIntegrationTest.java test

Problem

However, when I use jenkins to run the same test with same command, it cannot connect to localhost:2859, complaining that

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)

I don't know why it behaves differently in these two senarios. Could anyone give me some suggestions?

lsongseven
  • 21
  • 5
  • The reason Testcontainers uses random port mapping in the first place is to avoid conflicts with another processes already using that port, which can happen if you run tests on CI. Something related could be happening in your case. I would suggest to put a breakpoint in the code and see if the container is running with docker ps, but you probably can't do that with Jenkins. If you don't figure it out you can try removing setPortBindings. https://www.testcontainers.org/features/networking/#exposing-container-ports-to-the-host explains how to obtain connection details. – Vitaly Chura Oct 28 '20 at 11:36
  • 1
    Thanks a lot. But I find that the difference is caused by ip. There are many work nodes in my Jenkins, it seems that the started zookeeper container resides in another work node (with ip a.b.c.d) so it should connect to a.b.c.d:2859 instead of 127.0.0.1:2859. Thanks to the testcontainer members' reply: https://github.com/testcontainers/testcontainers-java/issues/3407 – lsongseven Oct 29 '20 at 08:17

0 Answers0