Search in sources :

Example 1 with Command

use of org.bf2.srs.fleetmanager.execution.impl.tasks.TestTask.Command 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 Command

use of org.bf2.srs.fleetmanager.execution.impl.tasks.TestTask.Command in project srs-fleet-manager by bf2fc6cc711aee1a0c2a.

the class DataCollectingWorker method execute.

@Override
public void execute(Task aTask, WorkerContext ctl) {
    boolean finished = false;
    try {
        TestTask task = (TestTask) aTask;
        Command command = task.getCommands().peekFirst();
        if (command != null && command.done()) {
            task.getCommands().removeFirst();
            command = task.getCommands().peekFirst();
        }
        if (command != null) {
            command.execute(ctl, task);
        } else {
            // Do not record stop caused by end of commands
            finished = true;
            ctl.stop();
        }
        data.recordSuccess();
    } catch (RetryExecutionControlException ex) {
        if (ex.isForce())
            data.recordForceRetry();
        else
            data.recordRetry();
        throw ex;
    } catch (StopExecutionControlException ex) {
        if (!finished)
            data.recordStop();
        throw ex;
    } catch (Exception ex) {
        data.recordException();
        throw ex;
    } finally {
        if (!finished)
            data.recordExecution();
    }
}
Also used : RetryExecutionControlException(org.bf2.srs.fleetmanager.execution.manager.impl.RetryExecutionControlException) Command(org.bf2.srs.fleetmanager.execution.impl.tasks.TestTask.Command) StopExecutionControlException(org.bf2.srs.fleetmanager.execution.manager.impl.StopExecutionControlException) TestTask(org.bf2.srs.fleetmanager.execution.impl.tasks.TestTask) StopExecutionControlException(org.bf2.srs.fleetmanager.execution.manager.impl.StopExecutionControlException) RetryExecutionControlException(org.bf2.srs.fleetmanager.execution.manager.impl.RetryExecutionControlException)

Example 3 with Command

use of org.bf2.srs.fleetmanager.execution.impl.tasks.TestTask.Command in project kas-fleetshard by bf2fc6cc711aee1a0c2a.

the class SuiteUnitTest method testExecutorError.

@SequentialTest
void testExecutorError() {
    ExecBuilder command = Exec.builder().withCommand("ppppeeeepppaaa", "jenda").logToOutput(false).throwErrors(true).timeout(60);
    assertThrows(KubeClusterException.class, command::exec);
}
Also used : ExecBuilder(org.bf2.test.executor.ExecBuilder) SequentialTest(org.bf2.systemtest.framework.SequentialTest)

Example 4 with Command

use of org.bf2.srs.fleetmanager.execution.impl.tasks.TestTask.Command 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)

Example 5 with Command

use of org.bf2.srs.fleetmanager.execution.impl.tasks.TestTask.Command in project srs-fleet-manager by bf2fc6cc711aee1a0c2a.

the class DataCollectingWorker method finallyExecute.

@Override
public void finallyExecute(Task aTask, WorkerContext ctl, Optional<Exception> error) {
    TestTask task = (TestTask) aTask;
    try {
        Command command = task.getFinalCommand();
        if (command != null)
            command.execute(ctl, task);
        data.recordFinallyExecuteSuccess();
    } finally {
        data.recordCounter(task.getCounter());
        data.recordFinallyExecuteAttempt();
        data.signalFinished();
    }
}
Also used : Command(org.bf2.srs.fleetmanager.execution.impl.tasks.TestTask.Command) TestTask(org.bf2.srs.fleetmanager.execution.impl.tasks.TestTask)

Aggregations

TestTask (org.bf2.srs.fleetmanager.execution.impl.tasks.TestTask)2 Command (org.bf2.srs.fleetmanager.execution.impl.tasks.TestTask.Command)2 IOException (java.io.IOException)1 ExecutionException (java.util.concurrent.ExecutionException)1 Matcher (java.util.regex.Matcher)1 RetryExecutionControlException (org.bf2.srs.fleetmanager.execution.manager.impl.RetryExecutionControlException)1 StopExecutionControlException (org.bf2.srs.fleetmanager.execution.manager.impl.StopExecutionControlException)1 SequentialTest (org.bf2.systemtest.framework.SequentialTest)1 ExecBuilder (org.bf2.test.executor.ExecBuilder)1 ExecResult (org.bf2.test.executor.ExecResult)1 KubeClusterException (org.bf2.test.k8s.KubeClusterException)1