Skip to content

Commit eb30bc1

Browse files
committed
Skip execInContainer(...) null or empty command parts.
1 parent 2b53c47 commit eb30bc1

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
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,11 @@
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;
23+
import java.util.function.Predicate;
2124
import java.util.stream.Collectors;
2225

2326
/**
@@ -199,7 +202,10 @@ public Container.ExecResult execInContainer(
199202
String containerId = containerInfo.getId();
200203
String containerName = containerInfo.getName();
201204

202-
String[] command = execConfig.getCommand();
205+
String[] command = Arrays.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: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@
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;
912
import org.testcontainers.utility.TestEnvironment;
1013

14+
import java.io.IOException;
1115
import java.util.Collections;
1216

1317
import static org.assertj.core.api.Assertions.assertThat;
@@ -36,6 +40,19 @@ void shouldExecuteCommand() throws Exception {
3640
// We expect to reach this point for modern Docker versions.
3741
}
3842

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

5875
final GenericContainer.ExecResult result = redis.execInContainer(
59-
ExecConfig.builder().workDir("/opt").command(new String[] { "pwd" }).build()
76+
ExecConfig.builder().workDir("/opt").command(new String[]{"pwd"}).build()
6077
);
6178
assertThat(result.getStdout()).startsWith("/opt");
6279
}
@@ -69,9 +86,29 @@ void shouldExecuteCommandWithEnvVars() throws Exception {
6986
ExecConfig
7087
.builder()
7188
.envVars(Collections.singletonMap("TESTCONTAINERS", "JAVA"))
72-
.command(new String[] { "env" })
89+
.command(new String[]{"env"})
7390
.build()
7491
);
7592
assertThat(result.getStdout()).contains("TESTCONTAINERS=JAVA");
7693
}
94+
95+
96+
private GenericContainer minioContainer;
97+
98+
void testIt() throws IOException, InterruptedException {
99+
minioContainer.execInContainer(
100+
"mc", debug(),
101+
"mb", "--region=eu-central-1",
102+
"$alias/mynewbucket"
103+
);
104+
}
105+
106+
private String debug() {
107+
if (false) {
108+
return "--debug";
109+
} else {
110+
return "";
111+
}
112+
}
113+
77114
}

0 commit comments

Comments
 (0)