I have a Spring Boot 2.1 Application which has integration tests. For integration tests purposes i want start a RabbitMq container with testcontainers framework. When I start those on my local machine everything seems to work i can access my rabbitMQ during the IT tests. However once i execute under gitlab-ci i constantly get connection refused exceptions
Here is my application-it-properties
spring.rabbitmq.host=localhost
spring.rabbitmq.virtualHost=/
spring.rabbitmq.port=5673
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
spring.rabbitmq.dynamic=true
spring.rabbitmq.template.retry.enabled=true
spring.rabbitmq.listener.simple.acknowledgeMode=AUTO
spring.rabbitmq.listener.simple.concurrency=5
This is my verify step in gitlab-ci
verify:feature:
stage: verify
script:
- git config --global user.email gitlab@test.de
- git config --global user.name gitlab
- git fetch --all
- git checkout origin/develop
- git merge $CI_BUILD_REF --no-commit --no-ff
- mvn $MAVEN_CLI_OPTS verify sonar:sonar $SONAR_PREVIEW_CLI_OPTS
only:
- /feature.*/
And this is how i start my testcontainer RabbitMQ
@Slf4j
@RunWith(SpringRunner.class)
@TestPropertySource(locations = {"classpath:application-it.properties"})
@SpringBootTest
public class TransformerServiceApplicationIt {
private static final int EXPOSED_RABBITMQ_PORT = 5672;
private static final int EXPORTED_RABBITMQ_PORT = 5673;
/**
* Start the rabbitmq.
*/
static {
final Consumer<CreateContainerCmd> rabbitCmd = e -> e.withPortBindings(new PortBinding(Ports.Binding.bindPort(EXPORTED_RABBITMQ_PORT), new ExposedPort(EXPOSED_RABBITMQ_PORT)));
final GenericContainer rabbitMq = new GenericContainer("rabbitmq:3-management").withExposedPorts(EXPOSED_RABBITMQ_PORT)
.withCreateContainerCmdModifier(rabbitCmd);
rabbitMq.start();
}....
}
And this is my exception
[org.springframework.amqp.rabbit.core.RabbitTemplate]: Factory method 'rabbitTemplate' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'itRabbitMQConfig': Invocation of init method failed; nested exception is org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused (Connection refused)
My guess is that it has something to do with the hostname resolving for localhost on gitlab.
Try this:
static {
final GenericContainer rabbitMq = new GenericContainer("rabbitmq:3-management").withExposedPorts(EXPOSED_RABBITMQ_PORT);
rabbitMq.start();
// Pass the properties directly to the app. Do not use properties file.
System.setProperty("spring.rabbitmq.host", rabbitMq.getContainerIpAddress());
System.setProperty("spring.rabbitmq.port", rabbitMq.getMappedPort(5672).toString());
}