use of org.bf2.test.k8s.KubeClusterException 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);
}
}
Aggregations