Search in sources :

Example 21 with Command

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));
}
Also used : CommandTimeoutException(com.google.copybara.util.CommandTimeoutException) Command(com.google.copybara.shell.Command) CommandRunner(com.google.copybara.util.CommandRunner) Test(org.junit.Test)

Example 22 with Command

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");
}
Also used : Command(com.google.copybara.shell.Command) CommandOutputWithStatus(com.google.copybara.util.CommandOutputWithStatus) CommandRunner(com.google.copybara.util.CommandRunner) Test(org.junit.Test)

Example 23 with Command

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\\)");
}
Also used : KillableObserver(com.google.copybara.shell.KillableObserver) Command(com.google.copybara.shell.Command) Killable(com.google.copybara.shell.Killable) AbnormalTerminationException(com.google.copybara.shell.AbnormalTerminationException) CommandRunner(com.google.copybara.util.CommandRunner) Test(org.junit.Test)

Example 24 with Command

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");
}
Also used : Command(com.google.copybara.shell.Command) CommandOutputWithStatus(com.google.copybara.util.CommandOutputWithStatus) CommandRunner(com.google.copybara.util.CommandRunner) Test(org.junit.Test)

Example 25 with Command

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() });
}
Also used : Path(java.nio.file.Path) Command(com.google.copybara.shell.Command)

Aggregations

Command (com.google.copybara.shell.Command)28 CommandRunner (com.google.copybara.util.CommandRunner)17 CommandException (com.google.copybara.shell.CommandException)14 CommandOutputWithStatus (com.google.copybara.util.CommandOutputWithStatus)10 Test (org.junit.Test)10 IOException (java.io.IOException)8 BadExitStatusWithOutputException (com.google.copybara.util.BadExitStatusWithOutputException)7 ImmutableList (com.google.common.collect.ImmutableList)6 ArrayList (java.util.ArrayList)5 Path (java.nio.file.Path)4 RepoException (com.google.copybara.exception.RepoException)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 ValidationException (com.google.copybara.exception.ValidationException)2 KillableObserver (com.google.copybara.shell.KillableObserver)2 CommandTimeoutException (com.google.copybara.util.CommandTimeoutException)2 Matcher (java.util.regex.Matcher)2 GitEnvironment (com.google.copybara.git.GitEnvironment)1 AbnormalTerminationException (com.google.copybara.shell.AbnormalTerminationException)1 BadExitStatusException (com.google.copybara.shell.BadExitStatusException)1 CommandResult (com.google.copybara.shell.CommandResult)1