Skip to content

Commit

Permalink
Allign files to checkstyle
Browse files Browse the repository at this point in the history
  • Loading branch information
obabec committed Feb 28, 2024
1 parent fb935dd commit d214c9c
Show file tree
Hide file tree
Showing 33 changed files with 207 additions and 164 deletions.
41 changes: 23 additions & 18 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@
<name>Jakub Stejskal</name>
<email>[email protected]</email>
</developer>
<developer>
<id>obabec</id>
<name>Ondrej Babec</name>
<email>[email protected]</email>
</developer>
</developers>

<properties>
Expand Down Expand Up @@ -151,6 +156,24 @@
<!-- </execution>-->
<!-- </executions>-->
<!-- </plugin>-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>${maven.checkstyle.version}</version>
<executions>
<execution>
<id>validate</id>
<phase>validate</phase>
<configuration>
<consoleOutput>true</consoleOutput>
<failsOnError>true</failsOnError>
</configuration>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-source-plugin</artifactId>
<version>${maven.source.plugin.version}</version>
Expand Down Expand Up @@ -251,24 +274,6 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>${maven.checkstyle.version}</version>
<executions>
<execution>
<id>validate</id>
<phase>validate</phase>
<configuration>
<consoleOutput>true</consoleOutput>
<failsOnError>true</failsOnError>
</configuration>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
*/
package io.skodjob.testframe;

import java.util.Collections;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;

public class LoggerUtils {

private static final Logger LOGGER = LoggerFactory.getLogger(LoggerUtils.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/*
* Copyright Skodjob authors.
* License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html).
*/
package io.skodjob.testframe;

import java.time.Duration;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
*/
package io.skodjob.testframe;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yaml.snakeyaml.Yaml;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
Expand All @@ -18,6 +14,10 @@
import java.util.Objects;
import java.util.function.Function;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yaml.snakeyaml.Yaml;

/**
* Class which holds environment variables for system tests.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
*/
package io.skodjob.testframe.clients;

import java.util.Arrays;

import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.ConfigBuilder;
import io.fabric8.kubernetes.client.KubernetesClient;
Expand All @@ -13,8 +15,6 @@
import io.skodjob.testframe.TestFrameEnv;
import io.skodjob.testframe.executor.Exec;

import java.util.Arrays;

public class KubeClient {

private KubernetesClient client;
Expand Down Expand Up @@ -73,13 +73,15 @@ private String createLocalKubeconfig() {
if (TestFrameEnv.KUBE_URL != null && TestFrameEnv.KUBE_TOKEN != null) {
createLocalOcKubeconfig(TestFrameEnv.KUBE_TOKEN, TestFrameEnv.KUBE_URL);
} else {
createLocalOcKubeconfig(TestFrameEnv.KUBE_USERNAME, TestFrameEnv.KUBE_PASSWORD, TestFrameEnv.KUBE_URL);
createLocalOcKubeconfig(TestFrameEnv.KUBE_USERNAME, TestFrameEnv.KUBE_PASSWORD,
TestFrameEnv.KUBE_URL);
}
} else {
if (TestFrameEnv.KUBE_URL != null && TestFrameEnv.KUBE_TOKEN != null) {
createLocalKubectlContext(TestFrameEnv.KUBE_TOKEN, TestFrameEnv.KUBE_URL);
} else {
createLocalKubectlContext(TestFrameEnv.KUBE_USERNAME, TestFrameEnv.KUBE_PASSWORD, TestFrameEnv.KUBE_URL);
createLocalKubectlContext(TestFrameEnv.KUBE_USERNAME, TestFrameEnv.KUBE_PASSWORD,
TestFrameEnv.KUBE_URL);
}
}
return TestFrameEnv.USER_PATH + "/test.kubeconfig";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@
*/
package io.skodjob.testframe.clients.cmdClient;

import io.skodjob.testframe.clients.KubeClusterException;
import io.skodjob.testframe.executor.Exec;
import io.skodjob.testframe.executor.ExecResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.nio.file.NoSuchFileException;
import java.util.ArrayList;
Expand All @@ -21,6 +15,12 @@
import java.util.function.Consumer;
import java.util.stream.Collectors;

import io.skodjob.testframe.clients.KubeClusterException;
import io.skodjob.testframe.executor.Exec;
import io.skodjob.testframe.executor.ExecResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static java.lang.String.join;
import static java.util.Arrays.asList;

Expand Down Expand Up @@ -89,7 +89,8 @@ public String getEvents() {
@SuppressWarnings("unchecked")
public K create(File... files) {
try (Context context = defaultContext()) {
Map<File, ExecResult> execResults = execRecursive(CREATE, files, Comparator.comparing(File::getName).reversed());
Map<File, ExecResult> execResults = execRecursive(CREATE, files, Comparator.comparing(File::getName)
.reversed());
for (Map.Entry<File, ExecResult> entry : execResults.entrySet()) {
if (!entry.getValue().exitStatus()) {
LOGGER.warn("Failed to create {}!", entry.getKey().getAbsolutePath());
Expand All @@ -104,7 +105,8 @@ public K create(File... files) {
@SuppressWarnings("unchecked")
public K apply(File... files) {
try (Context context = defaultContext()) {
Map<File, ExecResult> execResults = execRecursive(APPLY, files, Comparator.comparing(File::getName).reversed());
Map<File, ExecResult> execResults = execRecursive(APPLY, files, Comparator.comparing(File::getName)
.reversed());
for (Map.Entry<File, ExecResult> entry : execResults.entrySet()) {
if (!entry.getValue().exitStatus()) {
LOGGER.warn("Failed to apply {}!", entry.getKey().getAbsolutePath());
Expand All @@ -119,7 +121,8 @@ public K apply(File... files) {
@SuppressWarnings("unchecked")
public K delete(File... files) {
try (Context context = defaultContext()) {
Map<File, ExecResult> execResults = execRecursive(DELETE, files, Comparator.comparing(File::getName).reversed());
Map<File, ExecResult> execResults = execRecursive(DELETE, files, Comparator.comparing(File::getName)
.reversed());
for (Map.Entry<File, ExecResult> entry : execResults.entrySet()) {
if (!entry.getValue().exitStatus()) {
LOGGER.warn("Failed to delete {}!", entry.getKey().getAbsolutePath());
Expand All @@ -135,7 +138,8 @@ private Map<File, ExecResult> execRecursive(String subcommand, File[] files, Com
for (File f : files) {
if (f.isFile()) {
if (f.getName().endsWith(".yaml")) {
execResults.put(f, Exec.exec(null, namespacedCommand(subcommand, "-f", f.getAbsolutePath()), 0, false, false));
execResults.put(f, Exec.exec(null, namespacedCommand(subcommand, "-f",
f.getAbsolutePath()), 0, false, false));
}
} else if (f.isDirectory()) {
File[] children = f.listFiles();
Expand Down Expand Up @@ -187,7 +191,8 @@ public K deleteContentInNamespace(String yamlContent) {
@SuppressWarnings("unchecked")
public K applyContent(String yamlContent) {
try (Context context = defaultContext()) {
Exec.exec(yamlContent, command(Arrays.asList(APPLY, "-f", "-"), false), 0, true, true);
Exec.exec(yamlContent, command(Arrays.asList(APPLY, "-f", "-"), false), 0,
true, true);
return (K) this;
}
}
Expand All @@ -196,7 +201,8 @@ public K applyContent(String yamlContent) {
@SuppressWarnings("unchecked")
public K deleteContent(String yamlContent) {
try (Context context = defaultContext()) {
Exec.exec(yamlContent, command(Arrays.asList(DELETE, "-f", "-"), false), 0, true, false);
Exec.exec(yamlContent, command(Arrays.asList(DELETE, "-f", "-"), false), 0,
true, false);
return (K) this;
}
}
Expand Down Expand Up @@ -280,7 +286,9 @@ public String toString() {

@Override
public List<String> list(String resourceType) {
return Arrays.stream(Exec.exec(namespacedCommand(GET, resourceType, "-o", "jsonpath={range .items[*]}{.metadata.name} ")).out().trim().split(" +"))
return Arrays.stream(Exec.exec(namespacedCommand(GET, resourceType,
"-o", "jsonpath={range .items[*]}{.metadata.name} "))
.out().trim().split(" +"))
.filter(s -> !s.trim().isEmpty()).collect(Collectors.toList());
}

Expand Down Expand Up @@ -330,8 +338,10 @@ public String logs(String pod, String container) {
@Override
public String searchInLog(String resourceType, String resourceName, long sinceSeconds, String... grepPattern) {
try {
return Exec.exec("bash", "-c", join(" ", namespacedCommand("logs", resourceType + "/" + resourceName, "--since=" + sinceSeconds + "s",
"|", "grep", " -e " + join(" -e ", grepPattern), "-B", "1"))).out();
return Exec.exec("bash", "-c", join(" ",
namespacedCommand("logs", resourceType + "/" + resourceName, "--since=" + sinceSeconds + "s",
"|", "grep", " -e " + join(" -e ", grepPattern), "-B", "1")))
.out();
} catch (KubeClusterException e) {
if (e.result != null && e.result.returnCode() == 1) {
LOGGER.info("{} not found", Arrays.stream(grepPattern).toList());
Expand All @@ -343,9 +353,11 @@ public String searchInLog(String resourceType, String resourceName, long sinceSe
}

@Override
public String searchInLog(String resourceType, String resourceName, String resourceContainer, long sinceSeconds, String... grepPattern) {
public String searchInLog(String resourceType, String resourceName, String resourceContainer,
long sinceSeconds, String... grepPattern) {
try {
return Exec.exec("bash", "-c", join(" ", namespacedCommand("logs", resourceType + "/" + resourceName, "-c " + resourceContainer, "--since=" + sinceSeconds + "s",
return Exec.exec("bash", "-c", join(" ", namespacedCommand("logs",
resourceType + "/" + resourceName, "-c " + resourceContainer, "--since=" + sinceSeconds + "s",
"|", "grep", " -e " + join(" -e ", grepPattern), "-B", "1"))).out();
} catch (KubeClusterException e) {
if (e.result != null && e.result.exitStatus()) {
Expand All @@ -359,7 +371,9 @@ public String searchInLog(String resourceType, String resourceName, String resou

@Override
public List<String> listResourcesByLabel(String resourceType, String label) {
return asList(Exec.exec(namespacedCommand(GET, resourceType, "-l", label, "-o", "jsonpath={range .items[*]}{.metadata.name} ")).out().split("\\s+"));
return asList(Exec.exec(namespacedCommand(GET, resourceType,
"-l", label, "-o", "jsonpath={range .items[*]}{.metadata.name} "))
.out().split("\\s+"));
}

private List<String> command(List<String> rest, boolean namespaced) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
*/
package io.skodjob.testframe.clients.cmdClient;

import io.skodjob.testframe.executor.ExecResult;

import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

import io.skodjob.testframe.executor.ExecResult;

import static java.util.Arrays.asList;
import static java.util.stream.Collectors.toList;

Expand Down Expand Up @@ -192,7 +192,8 @@ default String logs(String pod) {
* @param grepPattern Grep patterns for search
* @return Grep result as string
*/
String searchInLog(String resourceType, String resourceName, String resourceContainer, long sinceSeconds, String... grepPattern);
String searchInLog(String resourceType, String resourceName, String resourceContainer,
long sinceSeconds, String... grepPattern);

String getResourceAsJson(String resourceType, String resourceName);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
*/
package io.skodjob.testframe.clients.cmdClient;

import io.skodjob.testframe.executor.Exec;

import java.util.List;
import java.util.Map;

import io.skodjob.testframe.executor.Exec;

/**
* A {@link KubeCmdClient} implementation wrapping {@code oc}.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@
*/
package io.skodjob.testframe.executor;

import io.fabric8.kubernetes.api.model.EnvVar;
import io.skodjob.testframe.clients.KubeClusterException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -33,6 +28,11 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import io.fabric8.kubernetes.api.model.EnvVar;
import io.skodjob.testframe.clients.KubeClusterException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static java.lang.String.join;

public class Exec {
Expand Down Expand Up @@ -163,7 +163,8 @@ public static ExecResult exec(String input, List<String> command, int timeout, b
* @param throwErrors throw error if exec fail
* @return results
*/
public static ExecResult exec(String input, List<String> command, int timeout, boolean logToOutput, boolean throwErrors) {
public static ExecResult exec(String input, List<String> command, int timeout, boolean logToOutput,
boolean throwErrors) {
return exec(input, command, Collections.emptySet(), timeout, logToOutput, throwErrors);
}

Expand All @@ -178,7 +179,8 @@ public static ExecResult exec(String input, List<String> command, int timeout, b
* @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) {
public static ExecResult exec(String input, List<String> command, Set<EnvVar> envVars, int timeout,
boolean logToOutput, boolean throwErrors) {
int ret = 1;
ExecResult execResult;
try {
Expand All @@ -204,7 +206,9 @@ public static ExecResult exec(String input, List<String> command, Set<EnvVar> en
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------";
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;
Expand Down Expand Up @@ -251,7 +255,8 @@ public static ExecResult exec(String input, List<String> command, Set<EnvVar> en
* @throws InterruptedException
* @throws ExecutionException
*/
public int execute(String input, List<String> commands, Set<EnvVar> envVars, long timeoutMs) throws IOException, InterruptedException, ExecutionException {
public int execute(String input, List<String> commands, Set<EnvVar> envVars, long timeoutMs)
throws IOException, InterruptedException, ExecutionException {
LOGGER.trace("Running command - " + join(" ", commands.toArray(new String[0])));
ProcessBuilder builder = new ProcessBuilder();
builder.command(commands);
Expand Down Expand Up @@ -368,7 +373,8 @@ public static boolean isExecutableOnPath(String cmd) {
*/
public static String cutExecutorLog(String log) {
if (log.length() > MAXIMUM_EXEC_LOG_CHARACTER_SIZE) {
LOGGER.warn("Executor log is too long. Going to strip it and print only first {} characters", MAXIMUM_EXEC_LOG_CHARACTER_SIZE);
LOGGER.warn("Executor log is too long. Going to strip it and print only first {} characters",
MAXIMUM_EXEC_LOG_CHARACTER_SIZE);
return log.substring(0, MAXIMUM_EXEC_LOG_CHARACTER_SIZE);
}
return log;
Expand Down
Loading

0 comments on commit d214c9c

Please sign in to comment.