Skip to content

Commit

Permalink
Merge pull request #813 from camunda-community-hub/np-638-binds
Browse files Browse the repository at this point in the history
fix: allow multiple volume binds with ZeebeVolume
  • Loading branch information
npepinpe authored Dec 29, 2024
2 parents 72579c8 + 82f3889 commit a6a6fa3
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 10 deletions.
22 changes: 14 additions & 8 deletions core/src/main/java/io/zeebe/containers/ZeebeVolume.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,22 +80,28 @@ 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());
}

/**
* Convenience method which mounts the volume to a Zeebe broker's data folder.
*
* @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));
}

/**
Expand Down
37 changes: 35 additions & 2 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,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;
Expand Down Expand Up @@ -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 {
Expand All @@ -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);
Expand All @@ -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);
}
}

0 comments on commit a6a6fa3

Please sign in to comment.