Search in sources :

Example 16 with CommandRunner

use of com.google.copybara.util.CommandRunner 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 17 with CommandRunner

use of com.google.copybara.util.CommandRunner 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 18 with CommandRunner

use of com.google.copybara.util.CommandRunner in project copybara by google.

the class CommandRunnerTest method testCommandWithCustomRunner.

@Test
public void testCommandWithCustomRunner() throws Exception {
    Command command = bashCommand("");
    CommandException e = assertThrows(CommandException.class, () -> runCommand(new CommandRunner(command).withCommandExecutor(new CommandExecutor() {

        @Override
        public TerminationStatus getCommandOutputWithStatus(Command cmd, byte[] input, KillableObserver cmdMonitor, OutputStream stdoutStream, OutputStream stderrStream) throws CommandException {
            throw new CommandException(cmd, "OH NOES!");
        }
    })));
    assertThat(e).hasMessageThat().contains("OH NOES!");
}
Also used : Command(com.google.copybara.shell.Command) KillableObserver(com.google.copybara.shell.KillableObserver) TerminationStatus(com.google.copybara.shell.TerminationStatus) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) CommandExecutor(com.google.copybara.util.CommandRunner.CommandExecutor) CommandException(com.google.copybara.shell.CommandException) CommandRunner(com.google.copybara.util.CommandRunner) Test(org.junit.Test)

Aggregations

CommandRunner (com.google.copybara.util.CommandRunner)18 Command (com.google.copybara.shell.Command)17 Test (org.junit.Test)10 CommandOutputWithStatus (com.google.copybara.util.CommandOutputWithStatus)7 ArrayList (java.util.ArrayList)5 CommandException (com.google.copybara.shell.CommandException)4 BadExitStatusWithOutputException (com.google.copybara.util.BadExitStatusWithOutputException)3 RepoException (com.google.copybara.exception.RepoException)2 KillableObserver (com.google.copybara.shell.KillableObserver)2 CommandTimeoutException (com.google.copybara.util.CommandTimeoutException)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ImmutableList (com.google.common.collect.ImmutableList)1 GitEnvironment (com.google.copybara.git.GitEnvironment)1 AbnormalTerminationException (com.google.copybara.shell.AbnormalTerminationException)1 Killable (com.google.copybara.shell.Killable)1 TerminationStatus (com.google.copybara.shell.TerminationStatus)1 CommandExecutor (com.google.copybara.util.CommandRunner.CommandExecutor)1 BufferedWriter (java.io.BufferedWriter)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1