use of com.google.copybara.shell.Command in project copybara by google.
the class CommandRunnerTest method testTimeout.
@Test
public void testTimeout() throws Exception {
Command command = bashCommand("" + "echo stdout msg\n" + ">&2 echo stderr msg\n" + "sleep 10\n");
CommandTimeoutException e = assertThrows(CommandTimeoutException.class, () -> runCommand(new CommandRunner(command, Duration.ofSeconds(1))));
assertThat(e.getOutput().getStdout()).contains("stdout msg");
assertThat(e.getOutput().getStderr()).contains("stderr msg");
assertThat(e.getMessage()).containsMatch("Command '.*' killed by Copybara after timeout \\(1s\\)");
assertThat(e.getTimeout()).isEquivalentAccordingToCompareTo(Duration.ofSeconds(1));
}
use of com.google.copybara.shell.Command in project copybara by google.
the class CommandRunnerTest method testCommand.
@Test
public void testCommand() throws Exception {
Command command = new Command(new String[] { "echo", "hello", "world" });
CommandOutputWithStatus result = runCommand(new CommandRunner(command));
assertThat(result.getTerminationStatus().success()).isTrue();
assertThat(result.getStdout()).isEqualTo("hello world\n");
assertWithMessage("Not empty: %s", new String(outContent.toByteArray(), UTF_8)).that(outContent.toByteArray()).isEmpty();
assertWithMessage("Not empty: %s", new String(errContent.toByteArray(), UTF_8)).that(errContent.toByteArray()).isEmpty();
assertLogContains("Executing [echo hello world]", "'echo' STDOUT: hello world", "Command 'echo' finished");
}
use of com.google.copybara.shell.Command in project copybara by google.
the class CommandRunnerTest method testObserverCanTerminate.
@Test
public void testObserverCanTerminate() throws Exception {
KillableObserver tester = new KillableObserver() {
@Override
public void startObserving(Killable killable) {
try {
Thread.sleep(1000);
} catch (InterruptedException ignored) {
// ignored
}
killable.kill();
}
@Override
public void stopObserving(Killable killable) {
}
};
Command command = bashCommand("" + "echo stdout msg\n" + ">&2 echo stderr msg\n" + "sleep 10\n");
AbnormalTerminationException e = assertThrows(AbnormalTerminationException.class, () -> runCommand(new CommandRunner(command, Duration.ofSeconds(90)).withObserver(tester)));
assertThat(e).hasMessageThat().containsMatch("Process terminated by signal 15");
assertThat(e).hasMessageThat().doesNotContainMatch("Command '.*' killed by Copybara after timeout \\(1s\\)");
}
use of com.google.copybara.shell.Command in project copybara by google.
the class CommandRunnerTest method testMaxCommandLength.
@Test
public void testMaxCommandLength() throws Exception {
String[] args = new String[10000];
args[0] = "echo";
for (int i = 1; i < 10000; i++) {
args[i] = "hello world!";
}
Command command = new Command(args);
CommandOutputWithStatus result = runCommand(new CommandRunner(command).withVerbose(true));
assertThat(result.getTerminationStatus().success()).isTrue();
assertLogContains("...", "'echo' STDOUT: hello world!", "Command 'echo' finished");
}
use of com.google.copybara.shell.Command in project copybara by google.
the class CommandRunnerTest method bashCommand.
private Command bashCommand(String bashScript) throws IOException {
Path tempFile = Files.createTempFile("test", "file");
Files.write(tempFile, bashScript.getBytes(UTF_8));
Files.setPosixFilePermissions(tempFile, PosixFilePermissions.fromString("rwxr-xr--"));
return new Command(new String[] { tempFile.toAbsolutePath().toString() });
}
Aggregations