Skip to content

Commit ce32e63

Browse files
committed
Skip execInContainer(...) null or empty command parts.
1 parent 43c6a97 commit ce32e63

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

core/src/main/java/org/testcontainers/containers/ExecInContainerPattern.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
import java.io.IOException;
1717
import java.nio.charset.Charset;
1818
import java.nio.charset.StandardCharsets;
19+
import java.util.Arrays;
1920
import java.util.List;
2021
import java.util.Map;
22+
import java.util.Objects;
2123
import java.util.stream.Collectors;
2224

2325
/**
@@ -199,7 +201,11 @@ public Container.ExecResult execInContainer(
199201
String containerId = containerInfo.getId();
200202
String containerName = containerInfo.getName();
201203

202-
String[] command = execConfig.getCommand();
204+
String[] command = Arrays
205+
.stream(execConfig.getCommand())
206+
.filter(Objects::nonNull)
207+
.filter(str -> !str.trim().isEmpty())
208+
.toArray(String[]::new);
203209
log.debug("{}: Running \"exec\" command: {}", containerName, String.join(" ", command));
204210
final ExecCreateCmd execCreateCmd = dockerClient
205211
.execCreateCmd(containerId)

core/src/test/java/org/testcontainers/junit/ExecInContainerTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import org.assertj.core.api.Assumptions;
44
import org.junit.jupiter.api.AutoClose;
55
import org.junit.jupiter.api.Test;
6+
import org.junit.jupiter.params.ParameterizedTest;
7+
import org.junit.jupiter.params.provider.NullSource;
8+
import org.junit.jupiter.params.provider.ValueSource;
69
import org.testcontainers.TestImages;
710
import org.testcontainers.containers.ExecConfig;
811
import org.testcontainers.containers.GenericContainer;
@@ -36,6 +39,19 @@ void shouldExecuteCommand() throws Exception {
3639
// We expect to reach this point for modern Docker versions.
3740
}
3841

42+
@ParameterizedTest
43+
@NullSource
44+
@ValueSource(strings = { "", " ", " " })
45+
void shouldExecuteCommandAndSkipCommand(String commandParam) throws Exception {
46+
Assumptions.assumeThat(TestEnvironment.dockerExecutionDriverSupportsExec()).isTrue();
47+
48+
final GenericContainer.ExecResult result = redis.execInContainer("redis-cli", commandParam, "role");
49+
assertThat(result.getStdout())
50+
.as("Output for \"redis-cli role\" command should start with \"master\"")
51+
.startsWith("master");
52+
assertThat(result.getStderr()).as("Stderr for \"redis-cli role\" command should be empty").isEmpty();
53+
}
54+
3955
@Test
4056
void shouldExecuteCommandWithUser() throws Exception {
4157
// The older "lxc" execution driver doesn't support "exec". At the time of writing (2016/03/29),

0 commit comments

Comments
 (0)