From 6eb459beee698ffb77d2d422335ed2acdecc8a4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ole=20Sch=C3=B6nburg?= Date: Fri, 8 Mar 2024 14:23:52 +0100 Subject: [PATCH] test: verify that volumes are cleanup up automatically Uses reflection to access the internal cleanup helper and invoke them as if all tests were finished. Verifies that the `ZeebeVolume` is automatically deleted because the correct labels are applied --- .../io/zeebe/containers/ZeebeVolumeTest.java | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/core/src/test/java/io/zeebe/containers/ZeebeVolumeTest.java b/core/src/test/java/io/zeebe/containers/ZeebeVolumeTest.java index 53946cd6..b63bb37d 100644 --- a/core/src/test/java/io/zeebe/containers/ZeebeVolumeTest.java +++ b/core/src/test/java/io/zeebe/containers/ZeebeVolumeTest.java @@ -22,6 +22,8 @@ import com.github.dockerjava.api.command.InspectVolumeResponse; import com.github.dockerjava.api.model.AccessMode; import com.github.dockerjava.api.model.Bind; +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; import java.util.Map; import org.junit.jupiter.api.Test; import org.testcontainers.DockerClientFactory; @@ -97,6 +99,39 @@ void shouldAttachToZeebeBroker() { } } + @Test + void shouldCleanupVolume() { + // given + final DockerClient client = DockerClientFactory.lazyClient(); + final ZeebeVolume volume = ZeebeVolume.newVolume(); + final Runnable performCleanup = getCleanupRunnable(); + + // when + performCleanup.run(); + + // then + assertThat(client.listVolumesCmd().exec().getVolumes()) + .as("the volume should be removed") + .noneMatch(v -> v.getName().equals(volume.getName())); + } + + private Runnable getCleanupRunnable() { + return () -> { + try { + final Class reaperClass = + Class.forName("org.testcontainers.utility.JVMHookResourceReaper"); + Constructor constructor = reaperClass.getDeclaredConstructor(); + constructor.setAccessible(true); + final Object reaper = constructor.newInstance(); + final Method performCleanup = reaperClass.getDeclaredMethod("performCleanup"); + performCleanup.setAccessible(true); + performCleanup.invoke(reaper); + } catch (Exception e) { + throw new RuntimeException(e); + } + }; + } + private void assertVolumeIsCorrectlyMounted( final DockerClient client, final ZeebeVolume volume, final String containerId) { final InspectContainerResponse containerResponse =