ShellTest: add tests for attempting to run nonexistent program, error return
Author: Colin P. Mccabe
Reviewers: Ismael Juma
Closes from cmccabe/KAFKA-5744
@@ -20,7 +20,11 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.Timeout;
+import java.io.IOException;
+
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
import static org.junit.Assume.assumeTrue;
public class ShellTest {
@@ -41,4 +45,28 @@ public class ShellTest {
String output = Shell.execCommand("head", "-c", Integer.toString(length), "/dev/zero");
assertEquals(length, output.length());
}
+
+ private final static String NONEXISTENT_PATH = "/dev/a/path/that/does/not/exist/in/the/filesystem";
+
+ @Test
+ public void testAttemptToRunNonExistentProgram() throws Exception {
+ assumeTrue(!OperatingSystem.IS_WINDOWS);
+ try {
+ Shell.execCommand(NONEXISTENT_PATH);
+ fail("Expected to get an exception when trying to run a program that does not exist");
+ } catch (IOException e) {
+ assertTrue(e.getMessage().contains("No such file"));
+ }
+ }
+
+ @Test
+ public void testRunProgramWithErrorReturn() throws Exception {
+ assumeTrue(!OperatingSystem.IS_WINDOWS);
+ try {
+ Shell.execCommand("head", "-c", "0", NONEXISTENT_PATH);
+ fail("Expected to get an exception when trying to head a nonexistent file");
+ } catch (Shell.ExitCodeException e) {
+ assertTrue(e.getMessage().contains("No such file"));
+ }
+ }
}
