Reuse containers with Testcontainers to improve build times

แชร์
ฝัง
  • เผยแพร่เมื่อ 4 ธ.ค. 2024

ความคิดเห็น • 9

  • @martinvestergaard9030
    @martinvestergaard9030 4 ปีที่แล้ว +3

    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).

    • @rieckpil
      @rieckpil  4 ปีที่แล้ว

      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

  • @kevinviglucci1850
    @kevinviglucci1850 3 ปีที่แล้ว +2

    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"?

    • @rieckpil
      @rieckpil  3 ปีที่แล้ว +1

      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.

    • @vigscodes
      @vigscodes 3 ปีที่แล้ว

      @@rieckpil Thanks for the reply!

  • @manojreddy6624
    @manojreddy6624 2 ปีที่แล้ว

    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.

    • @rieckpil
      @rieckpil  2 ปีที่แล้ว

      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?

  • @eugenfann
    @eugenfann 2 ปีที่แล้ว

    Hi @Rieckpil, great videos! Why don't you just use: jdbc:tc:mysql:8.0:///db_name?TC_REUSABLE=true for Reuse? Danke:))

    • @rieckpil
      @rieckpil  2 ปีที่แล้ว

      Thanks, Eugen! That's also a valid approach. I guess it's just personal preference on my end to go this route :)