From 82f3889ca966e5c2473b4a7cd50bd4451e8d057d Mon Sep 17 00:00:00 2001 From: Nicolas Pepin-Perreault Date: Sun, 29 Dec 2024 21:53:54 +0100 Subject: [PATCH] fix: allow multiple volume binds with ZeebeVolume --- .../java/io/zeebe/containers/ZeebeVolume.java | 22 +++++++---- .../io/zeebe/containers/ZeebeVolumeTest.java | 37 ++++++++++++++++++- 2 files changed, 49 insertions(+), 10 deletions(-) diff --git a/core/src/main/java/io/zeebe/containers/ZeebeVolume.java b/core/src/main/java/io/zeebe/containers/ZeebeVolume.java index 705b27e9..e8043ef9 100644 --- a/core/src/main/java/io/zeebe/containers/ZeebeVolume.java +++ b/core/src/main/java/io/zeebe/containers/ZeebeVolume.java @@ -80,11 +80,12 @@ public Bind asBind(final String mountPath) { } /** - * Returns a pre-configured {@link Bind} which mounts this volume to the data folder of a Zeebe * - * broker. + * Convenience method which mounts the volume to a Zeebe broker's data folder. + * + * @param command the create command of the Zeebe broker container */ - public Bind asZeebeBind() { - return asBind(ZeebeDefaults.getInstance().getDefaultDataPath()); + public void attachVolumeToContainer(final CreateContainerCmd command) { + attachVolumeToContainer(command, ZeebeDefaults.getInstance().getDefaultDataPath()); } /** @@ -92,10 +93,15 @@ public Bind asZeebeBind() { * * @param command the create command of the Zeebe broker container */ - public void attachVolumeToContainer(final CreateContainerCmd command) { - final HostConfig hostConfig = - Objects.requireNonNull(command.getHostConfig()).withBinds(asZeebeBind()); - command.withHostConfig(hostConfig); + public void attachVolumeToContainer(final CreateContainerCmd command, final String mountPath) { + final HostConfig hostConfig = Objects.requireNonNull(command.getHostConfig()); + final Bind[] binds = hostConfig.getBinds(); + final Bind[] newBinds = new Bind[binds.length + 1]; + + System.arraycopy(binds, 0, newBinds, 0, binds.length); + newBinds[binds.length] = asBind(mountPath); + + command.withHostConfig(hostConfig.withBinds(newBinds)); } /** diff --git a/core/src/test/java/io/zeebe/containers/ZeebeVolumeTest.java b/core/src/test/java/io/zeebe/containers/ZeebeVolumeTest.java index d87db70f..79a851ef 100644 --- a/core/src/test/java/io/zeebe/containers/ZeebeVolumeTest.java +++ b/core/src/test/java/io/zeebe/containers/ZeebeVolumeTest.java @@ -22,6 +22,7 @@ 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; @@ -117,6 +118,35 @@ void shouldCleanupVolume() { .noneMatch(v -> v.getName().equals(volume.getName())); } + @Test + void shouldNotOverwriteOtherVolumeBinds() { + // given + //noinspection resource + final DockerClient client = DockerClientFactory.lazyClient(); + try (final ZeebeBrokerContainer container = new ZeebeBrokerContainer()) { + final ZeebeVolume data = ZeebeVolume.newVolume(c -> c.withName("data")); + final ZeebeVolume logs = ZeebeVolume.newVolume(c -> c.withName("logs")); + + // when + container + .withCreateContainerCmdModifier(data::attachVolumeToContainer) + .withCreateContainerCmdModifier( + cmd -> + logs.attachVolumeToContainer( + cmd, ZeebeDefaults.getInstance().getDefaultLogsPath())) + .start(); + + // then + final InspectContainerResponse containerResponse = + client.inspectContainerCmd(container.getContainerId()).exec(); + final Bind[] volumeBinds = containerResponse.getHostConfig().getBinds(); + assertThat(volumeBinds).hasSize(2); + + assertVolumeBind(data, volumeBinds[0], ZeebeDefaults.getInstance().getDefaultDataPath()); + assertVolumeBind(logs, volumeBinds[1], ZeebeDefaults.getInstance().getDefaultLogsPath()); + } + } + private Runnable getCleanupRunnable() { return () -> { try { @@ -141,7 +171,10 @@ private void assertVolumeIsCorrectlyMounted( final Bind[] volumeBinds = containerResponse.getHostConfig().getBinds(); assertThat(volumeBinds).hasSize(1); - final Bind bind = volumeBinds[0]; + assertVolumeBind(volume, volumeBinds[0], ZeebeDefaults.getInstance().getDefaultDataPath()); + } + + private void assertVolumeBind(ZeebeVolume volume, Bind bind, final String mountPath) { assertThat(bind.getAccessMode()) .as("the volume should be mounted in read-write") .isEqualTo(AccessMode.rw); @@ -150,6 +183,6 @@ private void assertVolumeIsCorrectlyMounted( .isEqualTo(volume.getName()); assertThat(bind.getVolume().getPath()) .as("the volume path should be the default Zeebe data path") - .isEqualTo(ZeebeDefaults.getInstance().getDefaultDataPath()); + .isEqualTo(mountPath); } }