Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow multiple volume binds with ZeebeVolume #813

Merged
merged 1 commit into from
Dec 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Loading