Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testcontainers create two containers in a multi module project #3270

Closed
Cory00 opened this issue Sep 26, 2020 · 1 comment
Closed

Testcontainers create two containers in a multi module project #3270

Cory00 opened this issue Sep 26, 2020 · 1 comment

Comments

@Cory00
Copy link

Cory00 commented Sep 26, 2020

Hello,

I am trying to use testcontainers for integration tests with postgres in a multi module project.
Here is the scenario:
Module A is a library and contains the services, repositories and entities (I am using JPA). The entitymanager factory and datasource are configured in a @configuration class.
Module B is a spring boot application that contains the controller and a dependency to module A to call the services/connect to database.

From module A I am using mockmvc to test the whole functionality (from controller to service).
I tried two approaches:

  1. Using jdbc containers (https://www.testcontainers.org/modules/databases/jdbc/)
    With this approach TWO containers are created for one test. First container is removed immediately after initializing and a new one is created when Module B is hit.

[Test worker] docker[postgres:latest] : Starting container with ID: d32556d8b2f355feea8ed61a66611db54e2199d48aa20eaca6e1f068ee5bd573
[Test worker] docker[postgres:latest] : Container postgres:latest is starting: d32556d8b2f355feea8ed61a66611db54e2199d48aa20eaca6e1f068ee5bd573
[...]
[ Test worker] o.t.containers.output.WaitingConsumer : STDERR: 2020-09-26 11:04:55.529 UTC [1] LOG: database system is ready to accept connections
[ Test worker] org.testcontainers.ext.ScriptUtils : Executing database script from file:src/test/resources/integration/init_database.sql
[...]
[ Test worker] org.testcontainers.ext.ScriptUtils : Executed database script from file:src/test/resources/integration/init_database.sql in 245 ms.
[ Test worker] o.testcontainers.utility.ResourceReaper : Removed container and associated volume(s): postgres:latest
[...]
[ Test worker] docker[postgres:latest] : Creating container for image: postgres:latest
[ Test worker] o.t.utility.RegistryAuthLocator : Looking up auth config for image: postgres:latest at registry: index.docker.io
[ Test worker] o.t.utility.RegistryAuthLocator : No matching Auth Configs - falling back to defaultAuthConfig [null]
[ Test worker] o.t.d.a.AuthDelegatingDockerClientConfig : Effective auth config [null]
[ Test worker] docker[postgres:latest] : Starting container with ID: 5e9a660c0df6bd93c139bc05ef986a1360ceefdd7ede8c1438d28512978abf47
[ Test worker] docker[postgres:latest] : Container postgres:latest is starting: 5e9a660c0df6bd93c139bc05ef986a1360ceefdd7ede8c1438d28512978abf47

  1. Using static containers
    With this approach only one container gets created but when module B is hit it tries to connect to the real database, not to the containers. So it seems that the spring datasource properties are not overridden.
    Caused by: org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections. at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:285) at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49) at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:217) at org.postgresql.Driver.makeConnection(Driver.java:458) at org.postgresql.Driver.connect(Driver.java:260) at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:677) at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:189) at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriverManager(DriverManagerDataSource.java:155) at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriver(DriverManagerDataSource.java:146) at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnectionFromDriver(AbstractDriverBasedDataSource.java:205) at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnection(AbstractDriverBasedDataSource.java:169) at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:122) at org.hibernate.internal.NonContextualJdbcConnectionAccess.obtainConnection(NonContextualJdbcConnectionAccess.java:38) at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.acquireConnectionIfNeeded(LogicalConnectionManagedImpl.java:104)

Can you please point me to a possible solution to this?

@Cory00 Cory00 changed the title Testcontainers create two contains in a multi module project Testcontainers create two container in a multi module project Sep 26, 2020
@Cory00 Cory00 changed the title Testcontainers create two container in a multi module project Testcontainers create two containers in a multi module project Sep 26, 2020
@bsideup
Copy link
Member

bsideup commented Sep 27, 2020

It is currently not possible to "share" a container between multiple JVMs unless you're using something like the reusable containers, although it would keep the container running even after Module B.

Regarding the 2nd solution, it seems that you have a misconfigured test setup because Testcontainers always randomizes the ports and localhost:5432 can only appear if you hardcode it and/or not setting it correctly. For that, please refer to your framework's docs.

@bsideup bsideup closed this as completed Sep 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants