Search in sources :

Example 1 with CommandLineException

use of com.thoughtworks.go.util.command.CommandLineException in project gocd by gocd.

the class ProcessWrapperTest method shouldThrowExceptionWhenExecutableDoesNotExist.

@Test
public void shouldThrowExceptionWhenExecutableDoesNotExist() throws IOException {
    CommandLine line = CommandLine.createCommandLine("doesnotexist");
    try {
        ProcessOutputStreamConsumer outputStreamConsumer = inMemoryConsumer();
        line.execute(outputStreamConsumer, new EnvironmentVariableContext(), null);
        fail("Expected exception");
    } catch (CommandLineException e) {
        assertThat(e.getMessage(), containsString("Make sure this command can execute manually."));
        assertThat(e.getMessage(), containsString("doesnotexist"));
        assertThat(e.getResult(), notNullValue());
    }
}
Also used : CommandLine(com.thoughtworks.go.util.command.CommandLine) EnvironmentVariableContext(com.thoughtworks.go.util.command.EnvironmentVariableContext) ProcessOutputStreamConsumer(com.thoughtworks.go.util.command.ProcessOutputStreamConsumer) CommandLineException(com.thoughtworks.go.util.command.CommandLineException) Test(org.junit.Test)

Example 2 with CommandLineException

use of com.thoughtworks.go.util.command.CommandLineException in project gocd by gocd.

the class GarageServiceTest method shouldFailWhenGitIsNotFoundInPath.

@Test
public void shouldFailWhenGitIsNotFoundInPath() throws Exception {
    HttpLocalizedOperationResult result = new HttpLocalizedOperationResult();
    CommandLine commandLine = mock(CommandLine.class);
    GarageService spy = spy(garageService);
    doReturn(commandLine).when(spy).getGit();
    doReturn(commandLine).when(commandLine).withArg(anyString());
    doReturn(commandLine).when(commandLine).withWorkingDir(any(File.class));
    when(commandLine.run(any(InMemoryStreamConsumer.class), eq(GarageService.PROCESS_TAG))).thenThrow(new CommandLineException("failed to execute git"));
    spy.gc(result);
    assertThat(result.isSuccessful(), is(false));
    assertThat(result.hasMessage(), is(true));
    verify(spy, times(1)).getGit();
}
Also used : HttpLocalizedOperationResult(com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult) CommandLine(com.thoughtworks.go.util.command.CommandLine) InMemoryStreamConsumer(com.thoughtworks.go.util.command.InMemoryStreamConsumer) File(java.io.File) CommandLineException(com.thoughtworks.go.util.command.CommandLineException) Test(org.junit.Test)

Example 3 with CommandLineException

use of com.thoughtworks.go.util.command.CommandLineException in project gocd by gocd.

the class ProcessManager method startProcess.

Process startProcess(ProcessBuilder processBuilder, String commandLineForDisplay) {
    Process process;
    try {
        LOG.debug("[Command Line] START command {}", commandLineForDisplay);
        process = processBuilder.start();
        LOG.debug("[Command Line] END command {}", commandLineForDisplay);
    } catch (IOException e) {
        LOG.error("[Command Line] Failed executing [{}]", commandLineForDisplay);
        LOG.error("[Command Line] Agent's Environment Variables: {}", System.getenv());
        throw new CommandLineException(String.format("Error while executing [%s] \n Make sure this command can execute manually.", commandLineForDisplay), e);
    }
    return process;
}
Also used : IOException(java.io.IOException) CommandLineException(com.thoughtworks.go.util.command.CommandLineException)

Example 4 with CommandLineException

use of com.thoughtworks.go.util.command.CommandLineException in project gocd by gocd.

the class ExecCommandExecutor method executeCommandLine.

private int executeCommandLine(final BuildSession buildSession, final CommandLine commandLine, String... input) {
    final AtomicInteger exitCode = new AtomicInteger(-1);
    final CountDownLatch canceledOrDone = new CountDownLatch(1);
    buildSession.submitRunnable(new Runnable() {

        @Override
        public void run() {
            try {
                exitCode.set(commandLine.run(buildSession.processOutputStreamConsumer(), null, input));
            } catch (CommandLineException e) {
                LOG.error("Command failed", e);
                String message = format("Error happened while attempting to execute '%s'. \nPlease make sure [%s] can be executed on this agent.\n", commandLine.toStringForDisplay(), commandLine.getExecutable());
                String path = System.getenv("PATH");
                buildSession.println(message);
                buildSession.println(format("[Debug Information] Environment variable PATH: %s", path));
                LOG.error("[Command Line] {}. Path: {}", message, path);
            } finally {
                canceledOrDone.countDown();
            }
        }
    });
    Future<?> cancelMonitor = buildSession.submitRunnable(new Runnable() {

        @Override
        public void run() {
            try {
                buildSession.waitUntilCanceled();
            } catch (InterruptedException e) {
            // ignore
            } finally {
                canceledOrDone.countDown();
            }
        }
    });
    try {
        canceledOrDone.await();
    } catch (InterruptedException e) {
        LOG.error("Building thread interrupted", e);
    }
    cancelMonitor.cancel(true);
    return exitCode.get();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CountDownLatch(java.util.concurrent.CountDownLatch) CommandLineException(com.thoughtworks.go.util.command.CommandLineException)

Example 5 with CommandLineException

use of com.thoughtworks.go.util.command.CommandLineException in project gocd by gocd.

the class P4Fixture method stopP4d.

private void stopP4d(P4Client p4) {
    try {
        p4.admin("stop");
        ConsoleResult consoleResult = p4.checkConnection();
        while (!consoleResult.failed()) {
            try {
                // Wait for the server to shutdown
                Thread.sleep(100);
            } catch (InterruptedException ignored) {
            }
        }
    } catch (CommandLineException expected) {
        // Stopping p4d on windows returns the following failure:
        if (expected.getResult().errorAsString().contains("WSAECONNRESET") || expected.getResult().errorAsString().contains("WSAECONNREFUSED")) {
            return;
        }
        if (expected.getResult().errorAsString().contains("Connection refused") || expected.getResult().errorAsString().contains("Connection reset")) {
            return;
        }
        throw expected;
    }
}
Also used : ConsoleResult(com.thoughtworks.go.util.command.ConsoleResult) CommandLineException(com.thoughtworks.go.util.command.CommandLineException)

Aggregations

CommandLineException (com.thoughtworks.go.util.command.CommandLineException)6 CommandLine (com.thoughtworks.go.util.command.CommandLine)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Test (org.junit.Test)2 HttpLocalizedOperationResult (com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult)1 ConsoleResult (com.thoughtworks.go.util.command.ConsoleResult)1 EnvironmentVariableContext (com.thoughtworks.go.util.command.EnvironmentVariableContext)1 InMemoryStreamConsumer (com.thoughtworks.go.util.command.InMemoryStreamConsumer)1 ProcessOutputStreamConsumer (com.thoughtworks.go.util.command.ProcessOutputStreamConsumer)1 File (java.io.File)1 IOException (java.io.IOException)1