Search in sources :

Example 1 with ExecResult

use of org.bf2.test.executor.ExecResult in project kas-fleetshard by bf2fc6cc711aee1a0c2a.

the class Exec method exec.

/**
 * Method executes external command
 *
 * @param command     arguments for command
 * @param envVars
 * @param timeout     timeout for execution
 * @param logToOutput log output or not
 * @param throwErrors look for errors in output and throws exception if true
 * @return execution results
 */
public static ExecResult exec(String input, List<String> command, Set<EnvVar> envVars, int timeout, boolean logToOutput, boolean throwErrors) {
    int ret = 1;
    ExecResult execResult;
    try {
        Exec executor = new Exec();
        LOGGER.info("Command: {}", String.join(" ", command));
        ret = executor.execute(input, command, envVars, timeout);
        synchronized (LOCK) {
            if (logToOutput) {
                LOGGER.info("RETURN code: {}", ret);
                if (!executor.out().isEmpty()) {
                    LOGGER.info("======STDOUT START=======");
                    LOGGER.info("{}", cutExecutorLog(executor.out()));
                    LOGGER.info("======STDOUT END======");
                }
                if (!executor.err().isEmpty()) {
                    LOGGER.info("======STDERR START=======");
                    LOGGER.info("{}", cutExecutorLog(executor.err()));
                    LOGGER.info("======STDERR END======");
                }
            }
        }
        execResult = new ExecResult(ret, executor.out(), executor.err());
        if (throwErrors && ret != 0) {
            String msg = "`" + join(" ", command) + "` got status code " + ret + " and stderr:\n------\n" + executor.stdErr + "\n------\nand stdout:\n------\n" + executor.stdOut + "\n------";
            Matcher matcher = ERROR_PATTERN.matcher(executor.err());
            KubeClusterException t = null;
            if (matcher.find()) {
                switch(matcher.group(1)) {
                    case "NotFound":
                        t = new KubeClusterException.NotFound(execResult, msg);
                        break;
                    case "AlreadyExists":
                        t = new KubeClusterException.AlreadyExists(execResult, msg);
                        break;
                    default:
                        break;
                }
            }
            matcher = INVALID_PATTERN.matcher(executor.err());
            if (matcher.find()) {
                t = new KubeClusterException.InvalidResource(execResult, msg);
            }
            if (t == null) {
                t = new KubeClusterException(execResult, msg);
            }
            throw t;
        }
        return new ExecResult(ret, executor.out(), executor.err());
    } catch (IOException | ExecutionException e) {
        throw new KubeClusterException(e);
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new KubeClusterException(e);
    }
}
Also used : Matcher(java.util.regex.Matcher) IOException(java.io.IOException) KubeClusterException(org.bf2.test.k8s.KubeClusterException) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with ExecResult

use of org.bf2.test.executor.ExecResult in project kas-fleetshard by bf2fc6cc711aee1a0c2a.

the class SuiteUnitTest method testExecutor.

@ParallelTest
void testExecutor() {
    ExecResult result = Exec.builder().withCommand("ls", System.getProperty("user.dir")).logToOutput(false).throwErrors(true).timeout(60).exec();
    assertTrue(result.exitStatus());
}
Also used : ExecResult(org.bf2.test.executor.ExecResult) ParallelTest(org.bf2.systemtest.framework.ParallelTest)

Example 3 with ExecResult

use of org.bf2.test.executor.ExecResult in project kas-fleetshard by bf2fc6cc711aee1a0c2a.

the class BaseCmdKubeClient method process.

@Override
@SuppressWarnings("unchecked")
public K process(Map<String, String> parameters, String file, Consumer<String> c) {
    List<String> command = command(asList(PROCESS, "-f", file), false);
    command.addAll(parameters.entrySet().stream().map(e -> e.getKey() + "=" + e.getValue()).collect(Collectors.toList()));
    ExecResult exec = Exec.builder().throwErrors(true).withCommand(command).exec();
    c.accept(exec.out());
    return (K) this;
}
Also used : ExecResult(org.bf2.test.executor.ExecResult)

Aggregations

ExecResult (org.bf2.test.executor.ExecResult)2 IOException (java.io.IOException)1 ExecutionException (java.util.concurrent.ExecutionException)1 Matcher (java.util.regex.Matcher)1 ParallelTest (org.bf2.systemtest.framework.ParallelTest)1 KubeClusterException (org.bf2.test.k8s.KubeClusterException)1