I found this solution simpler: 1. Make a TestWithContainer class with a static container (important: don't annotate the container field with @Container, and don't annotate the class with @Testcontainers). 2. In the static block, initialize the container, just as you did. 3. Do not use .withReuse(true) 4. Any test class that should reuse the container should extend TestWithContainer Now, the container is reused across all tests classes extending TestWithContainer. The container is only started once, and is properly closed and deleted after all tests are run (because .withReuse(true) wasn't used).
Hey Martin, yes that's a great way of sharing the container across multiple tests. You can then also add .withReuse(true) to this setup if you don't want to delete them afterwards to have faster subsequent tests. Thanks a lot for this input, Philip
When are the containers cleanup up? Will something eventually come around and destroy those containers, or would they now be running on the host "forever"?
Hi rieckpil, I am using Cassandra test-container, In my local machine all test cases are running successfully , But in Jenkins pipeline I am seeing below error. Can you please assist ? [2022-08-18T16:36:33.592Z] 2022-08-18 16:36:29,889 [Test worker] WARN org.testcontainers.utility.TestcontainersConfiguration - Attempted to read Testcontainers configuration file at file:/root/.testcontainers.properties but the file was not found. Exception message: FileNotFoundException: /root/.testcontainers.properties (No such file or directory) [2022-08-18T16:36:33.592Z] 2022-08-18 16:36:29,896 [Test worker] INFO org.testcontainers.utility.ImageNameSubstitutor - Image name substitution will be performed by: DefaultImageNameSubstitutor (composite of 'ConfigurationFileImageNameSubstitutor' and 'PrefixingImageNameSubstitutor') [2022-08-18T16:36:34.172Z] 2022-08-18 16:36:30,261 [Test worker] INFO org.testcontainers.dockerclient.DockerMachineClientProviderStrategy - docker-machine executable was not found on PATH ([/usr/local/openjdk-11/bin, /usr/local/sbin, /usr/local/bin, /usr/sbin, /usr/bin, /sbin, /bin, /opt/ant/bin, /home/jenkins/.local/bin]) [2022-08-18T16:36:34.172Z] 2022-08-18 16:36:30,262 [Test worker] ERROR org.testcontainers.dockerclient.DockerClientProviderStrategy - Could not find a valid Docker environment. Please check configuration. Attempted configurations were: [2022-08-18T16:36:34.172Z] 2022-08-18 16:36:30,262 [Test worker] ERROR org.testcontainers.dockerclient.DockerClientProviderStrategy - EnvironmentAndSystemPropertyClientProviderStrategy: failed with exception DockerClientException (Private key must be accompanied by certificate chain) [2022-08-18T16:36:34.172Z] 2022-08-18 16:36:30,262 [Test worker] ERROR org.testcontainers.dockerclient.DockerClientProviderStrategy - UnixSocketClientProviderStrategy: failed with exception InvalidConfigurationException (Could not find unix domain socket). Root cause NoSuchFileException (/var/run/docker.sock) [2022-08-18T16:36:34.172Z] 2022-08-18 16:36:30,262 [Test worker] ERROR org.testcontainers.dockerclient.DockerClientProviderStrategy - As no valid configuration was found, execution cannot continue [2022-08-18T16:36:34.172Z] [2022-08-18T16:36:34.172Z] Gradle Test Executor 1 finished executing tests.
I found this solution simpler:
1. Make a TestWithContainer class with a static container (important: don't annotate the container field with @Container, and don't annotate the class with @Testcontainers).
2. In the static block, initialize the container, just as you did.
3. Do not use .withReuse(true)
4. Any test class that should reuse the container should extend TestWithContainer
Now, the container is reused across all tests classes extending TestWithContainer. The container is only started once, and is properly closed and deleted after all tests are run (because .withReuse(true) wasn't used).
Hey Martin,
yes that's a great way of sharing the container across multiple tests. You can then also add .withReuse(true) to this setup if you don't want to delete them afterwards to have faster subsequent tests.
Thanks a lot for this input,
Philip
When are the containers cleanup up? Will something eventually come around and destroy those containers, or would they now be running on the host "forever"?
With the reuse feature active, they'll be running forever until you shut down your/restart your Docker engine/operating system or manually stop them.
@@rieckpil Thanks for the reply!
Hi rieckpil,
I am using Cassandra test-container, In my local machine all test cases are running successfully , But in Jenkins pipeline I am seeing below error. Can you please assist ?
[2022-08-18T16:36:33.592Z] 2022-08-18 16:36:29,889 [Test worker] WARN org.testcontainers.utility.TestcontainersConfiguration - Attempted to read Testcontainers configuration file at file:/root/.testcontainers.properties but the file was not found. Exception message: FileNotFoundException: /root/.testcontainers.properties (No such file or directory)
[2022-08-18T16:36:33.592Z] 2022-08-18 16:36:29,896 [Test worker] INFO org.testcontainers.utility.ImageNameSubstitutor - Image name substitution will be performed by: DefaultImageNameSubstitutor (composite of 'ConfigurationFileImageNameSubstitutor' and 'PrefixingImageNameSubstitutor')
[2022-08-18T16:36:34.172Z] 2022-08-18 16:36:30,261 [Test worker] INFO org.testcontainers.dockerclient.DockerMachineClientProviderStrategy - docker-machine executable was not found on PATH ([/usr/local/openjdk-11/bin, /usr/local/sbin, /usr/local/bin, /usr/sbin, /usr/bin, /sbin, /bin, /opt/ant/bin, /home/jenkins/.local/bin])
[2022-08-18T16:36:34.172Z] 2022-08-18 16:36:30,262 [Test worker] ERROR org.testcontainers.dockerclient.DockerClientProviderStrategy - Could not find a valid Docker environment. Please check configuration. Attempted configurations were:
[2022-08-18T16:36:34.172Z] 2022-08-18 16:36:30,262 [Test worker] ERROR org.testcontainers.dockerclient.DockerClientProviderStrategy - EnvironmentAndSystemPropertyClientProviderStrategy: failed with exception DockerClientException (Private key must be accompanied by certificate chain)
[2022-08-18T16:36:34.172Z] 2022-08-18 16:36:30,262 [Test worker] ERROR org.testcontainers.dockerclient.DockerClientProviderStrategy - UnixSocketClientProviderStrategy: failed with exception InvalidConfigurationException (Could not find unix domain socket). Root cause NoSuchFileException (/var/run/docker.sock)
[2022-08-18T16:36:34.172Z] 2022-08-18 16:36:30,262 [Test worker] ERROR org.testcontainers.dockerclient.DockerClientProviderStrategy - As no valid configuration was found, execution cannot continue
[2022-08-18T16:36:34.172Z]
[2022-08-18T16:36:34.172Z] Gradle Test Executor 1 finished executing tests.
As far as I can read that long log output, your Jenkins doesn't have a Docker engine installed. Can you try to run "docker info" within Jenkins?
Hi @Rieckpil, great videos! Why don't you just use: jdbc:tc:mysql:8.0:///db_name?TC_REUSABLE=true for Reuse? Danke:))
Thanks, Eugen! That's also a valid approach. I guess it's just personal preference on my end to go this route :)