Skip to content

Commit

Permalink
test: verify that volumes are cleanup up automatically
Browse files Browse the repository at this point in the history
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
  • Loading branch information
lenaschoenburg committed Mar 8, 2024
1 parent 21d75bd commit 6eb459b
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions core/src/test/java/io/zeebe/containers/ZeebeVolumeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 =
Expand Down

0 comments on commit 6eb459b

Please sign in to comment.