testcontainers / testcontainers-java

Testcontainers is a Java library that supports JUnit tests, providing lightweight, throwaway instances of common databases, Selenium web browsers, or anything else that can run in a Docker container.
https://testcontainers.org
MIT License
7.99k stars 1.64k forks source link

[Bug]: LocalStackContainer is not reused after test when withReuse is true (similar to #7097) #8795

Closed fu-hong-yi closed 3 months ago

fu-hong-yi commented 3 months ago

Module

LocalStack

Testcontainers version

1.19.8

Using the latest Testcontainers version?

Yes

Host OS

Mac OS

Host Arch

ARM

Docker version

Client:
 Cloud integration: v1.0.35+desktop.13
 Version:           26.0.0
 API version:       1.45
 Go version:        go1.21.8
 Git commit:        2ae903e
 Built:             Wed Mar 20 15:14:46 2024
 OS/Arch:           darwin/arm64
 Context:           desktop-linux

Server: Docker Desktop 4.29.0 (145265)
 Engine:
  Version:          26.0.0
  API version:      1.45 (minimum version 1.24)
  Go version:       go1.21.8
  Git commit:       8b79278
  Built:            Wed Mar 20 15:18:02 2024
  OS/Arch:          linux/arm64
  Experimental:     false
 containerd:
  Version:          1.6.28
  GitCommit:        ae07eda36dd25f8a1b98dfbf587313b99c0190bb
 runc:
  Version:          1.1.12
  GitCommit:        v1.1.12-0-g51d5e94
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

What happened?

Similar to #7097, it just doesn't seem to work? This is a local environment. The localstack container is re-created each time.

localStack = new LocalStackContainer(DockerImageName.parse("localstack/localstack:3.0"))
        .withServices(S3)
        .withExposedPorts(4566);
localStack.withReuse(true);
localStack.start();

testcontainers.properties is in classpath src/test/resources with testcontainers.reuse.enable=true

As can be seen in the logs, the mysql container with a similar configuration, is successfully reused. But the localstack container is getting re-created each time. ryuk is also started each time (without localstack and with just mysql container, ryuk is not started).

Relevant log output

13:54:09.028 [main] INFO  o.testcontainers.images.PullPolicy - Image pull policy will be performed by: DefaultPullPolicy()
13:54:09.029 [main] INFO  o.t.utility.ImageNameSubstitutor - Image name substitution will be performed by: DefaultImageNameSubstitutor (composite of 'ConfigurationFileImageNameSubstitutor' and 'PrefixingImageNameSubstitutor')
13:54:09.180 [main] INFO  o.t.d.DockerClientProviderStrategy - Loaded org.testcontainers.dockerclient.UnixSocketClientProviderStrategy from ~/.testcontainers.properties, will try it first
13:54:09.316 [main] INFO  o.t.d.DockerClientProviderStrategy - Found Docker environment with local Unix socket (unix:///var/run/docker.sock)
13:54:09.321 [main] INFO  o.testcontainers.DockerClientFactory - Docker host IP address is localhost
13:54:09.332 [main] INFO  o.testcontainers.DockerClientFactory - Connected to docker: 
  Server Version: 26.0.0
  API Version: 1.45
  Operating System: Docker Desktop
  Total Memory: 7841 MB
13:54:09.334 [main] INFO  o.testcontainers.DockerClientFactory - Checking the system...
13:54:09.334 [main] INFO  o.testcontainers.DockerClientFactory - ✔︎ Docker server version should be at least 1.6.0
13:54:09.356 [main] INFO  tc.localstack/localstack:3.5.0 - LOCALSTACK_HOST environment variable set to localhost (to match host-routable address for container)
13:54:09.356 [main] DEBUG tc.testcontainers/ryuk:0.7.0 - Starting container: testcontainers/ryuk:0.7.0
13:54:09.357 [main] DEBUG tc.testcontainers/ryuk:0.7.0 - Trying to start container: testcontainers/ryuk:0.7.0 (attempt 1/1)
13:54:09.357 [main] DEBUG tc.testcontainers/ryuk:0.7.0 - Starting container: testcontainers/ryuk:0.7.0
13:54:09.357 [main] INFO  tc.testcontainers/ryuk:0.7.0 - Creating container for image: testcontainers/ryuk:0.7.0
13:54:09.549 [main] INFO  tc.testcontainers/ryuk:0.7.0 - Container testcontainers/ryuk:0.7.0 is starting: ****5f74
13:54:09.791 [main] INFO  tc.testcontainers/ryuk:0.7.0 - Container testcontainers/ryuk:0.7.0 started in PT0.434077S
13:54:09.796 [main] DEBUG tc.localstack/localstack:3.5.0 - Starting container: localstack/localstack:3.5.0
13:54:09.796 [main] DEBUG tc.localstack/localstack:3.5.0 - Trying to start container: localstack/localstack:3.5.0 (attempt 1/1)
13:54:09.796 [main] DEBUG tc.localstack/localstack:3.5.0 - Starting container: localstack/localstack:3.5.0
13:54:09.796 [main] INFO  tc.localstack/localstack:3.5.0 - Creating container for image: localstack/localstack:3.5.0
13:54:10.020 [main] DEBUG tc.localstack/localstack:3.5.0 - Can't find a reusable running container with hash: ***56bb
13:54:10.046 [main] INFO  tc.localstack/localstack:3.5.0 - Container localstack/localstack:3.5.0 is starting: ****2628
13:54:11.884 [main] INFO  tc.localstack/localstack:3.5.0 - Container localstack/localstack:3.5.0 started in PT2.088242S
13:54:17.559 [main] DEBUG tc.mysql:8.0.37 - Starting container: mysql:8.0.37
13:54:17.559 [main] DEBUG tc.mysql:8.0.37 - Trying to start container: mysql:8.0.37 (attempt 1/3)
13:54:17.559 [main] DEBUG tc.mysql:8.0.37 - Starting container: mysql:8.0.37
13:54:17.559 [main] INFO  tc.mysql:8.0.37 - Creating container for image: mysql:8.0.37
13:54:17.582 [main] INFO  tc.mysql:8.0.37 - Reusing container with ID: ****b56a and hash: ****bf6a
13:54:17.583 [main] INFO  tc.mysql:8.0.37 - Reusing existing container (****b56a) and not creating a new one
13:54:17.592 [main] INFO  tc.mysql:8.0.37 - Waiting for database connection to become available at jdbc:mysql://localhost:64647/m3_dev using query 'SELECT 1'
13:54:17.598 [main] DEBUG tc.mysql:8.0.37 - Trying to create JDBC connection using com.mysql.cj.jdbc.Driver to jdbc:mysql://localhost:64647/test with properties: {password=test, user=test}
13:54:17.687 [main] INFO  tc.mysql:8.0.37 - Container mysql:8.0.37 started in PT0.128024S

Additional Information

No response

eddumelendez commented 3 months ago

testcontainers.properties is in classpath src/test/resources with testcontainers.reuse.enable=true

testcontainers.reuse.enable=true should be add ~/.testcontainers.properties. See https://java.testcontainers.org/features/reuse/

I was able to reproduce the problem. Closing in favor of https://github.com/testcontainers/testcontainers-java/issues/8814