Skip to content
Permalink
Browse files

feat: use logger in hooks and junit extensions.

  • Loading branch information...
iocanel committed Jul 31, 2019
1 parent 4b6d1a7 commit b3148dd7431dce488519bf6d9c42e1a007ec484e
@@ -15,6 +15,8 @@
*/
package io.dekorate.kubernetes.hook;

import io.dekorate.Logger;
import io.dekorate.LoggerFactory;
import io.dekorate.hook.ProjectHook;
import io.dekorate.kubernetes.config.KubernetesConfig;
import io.dekorate.project.Project;
@@ -26,6 +28,7 @@

private final File dockerFile;
private final String image;
private Logger LOGGER = LoggerFactory.getLogger();

public DockerBuildHook(Project project, KubernetesConfig config ) {
super(project);
@@ -45,6 +48,7 @@ public void warmup() {

@Override
public void run() {
LOGGER.info("Performing docker build.");
exec("docker", "build", "-f" + dockerFile.getAbsolutePath(), "-t" + image, project.getRoot().toAbsolutePath().toString());
}
}
@@ -15,6 +15,8 @@
*/
package io.dekorate.kubernetes.hook;

import io.dekorate.Logger;
import io.dekorate.LoggerFactory;
import io.dekorate.hook.ProjectHook;
import io.dekorate.kubernetes.config.KubernetesConfig;
import io.dekorate.project.Project;
@@ -23,6 +25,7 @@
public class DockerPushHook extends ProjectHook {

private final String image;
private Logger LOGGER = LoggerFactory.getLogger();

public DockerPushHook(Project project, KubernetesConfig config) {
super(project);
@@ -41,6 +44,7 @@ public void warmup() {

@Override
public void run() {
exec("docker", "push", image);
LOGGER.info("Performing docker push.");
exec("docker", "push", image);
}
}
@@ -15,13 +15,16 @@
*/
package io.dekorate.kubernetes.hook;

import io.dekorate.Logger;
import io.dekorate.LoggerFactory;
import io.dekorate.hook.ProjectHook;
import io.dekorate.project.Project;

public class ScaleDeploymentHook extends ProjectHook {

private final String name;
private final int replicas;
private Logger LOGGER = LoggerFactory.getLogger();

public ScaleDeploymentHook(Project project, String name, int replicas) {
super(project);
@@ -41,6 +44,7 @@ public void warmup() {

@Override
public void run() {
exec("kubectl", "scale", "deployment/" + name, "--replicas="+replicas);
LOGGER.info("Performing docker build.");
exec("kubectl", "scale", "deployment/" + name, "--replicas="+replicas);
}
}
@@ -15,6 +15,8 @@
*/
package io.dekorate.openshift.hook;

import io.dekorate.Logger;
import io.dekorate.LoggerFactory;
import io.dekorate.deps.kubernetes.api.model.HasMetadata;
import io.dekorate.deps.kubernetes.api.model.KubernetesList;
import io.dekorate.deps.kubernetes.api.model.Secret;
@@ -36,6 +38,7 @@
private final OpenshiftConfig config;
private final OpenShiftClient client = new DefaultOpenShiftClient();
private final KubernetesList kubernetesList;
private final Logger LOGGER = LoggerFactory.getLogger();

public OcBuildHook(String name, OpenshiftConfig config, Project project, KubernetesList kubernetesList) {
super(project);
@@ -50,7 +53,7 @@ public void init () {
.filter(i -> config.isAutoDeployEnabled() || i instanceof BuildConfig || i instanceof ImageStream || i instanceof Secret)
.forEach(i -> {
HasMetadata item = client.resource(i).createOrReplace();
System.out.println("Applied: " + item.getKind() + " " + i.getMetadata().getName());
LOGGER.info("Applied: " + item.getKind() + " " + i.getMetadata().getName());
});
OpenshiftUtils.waitForImageStreamTags(items, 2, TimeUnit.MINUTES);
}
@@ -63,6 +66,7 @@ public void warmup() {
@Override
public void run() {
if (project.getBuildInfo().getOutputFile().getParent().toFile().exists()) {
LOGGER.info("Performing s2i build.");
exec("oc", "start-build", name, "--from-dir=" + project.getBuildInfo().getOutputFile().getParent().toAbsolutePath().toString(), "--follow");
} else {
throw new IllegalStateException("Can't trigger binary build. " + project.getBuildInfo().getOutputFile().toAbsolutePath().toString() + " does not exist!");
@@ -26,6 +26,8 @@
import org.junit.jupiter.api.extension.ExecutionCondition;
import org.junit.jupiter.api.extension.ExtensionContext;

import io.dekorate.Logger;
import io.dekorate.LoggerFactory;
import io.dekorate.deps.kubernetes.api.model.HasMetadata;
import io.dekorate.deps.kubernetes.api.model.KubernetesList;
import io.dekorate.deps.kubernetes.api.model.Pod;
@@ -45,17 +47,24 @@
import io.dekorate.testing.WithPod;
import io.dekorate.testing.WithProject;
import io.dekorate.testing.config.KubernetesIntegrationTestConfig;
import io.dekorate.utils.Strings;

public class KubernetesExtension implements ExecutionCondition, BeforeAllCallback, AfterAllCallback,
WithKubernetesIntegrationTestConfig, WithPod, WithKubernetesClient, WithKubernetesResources, WithEvents, WithProject, WithKubernetesConfig {

private final Logger LOGGER = LoggerFactory.getLogger();

@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
try {
VersionInfo version = getKubernetesClient(context).getVersion();
return ConditionEvaluationResult.enabled("Found version:" + version);
String message = "Found version:" + version;
LOGGER.info(message);
return ConditionEvaluationResult.enabled(message);
} catch (Throwable t) {
return ConditionEvaluationResult.disabled("Could not communicate with KubernetesExtension API server.");
String reason = "Could not communicate with KubernetesExtension API server.";
LOGGER.error(reason);
return ConditionEvaluationResult.disabled(reason);
}
}

@@ -90,7 +99,7 @@ public void beforeAll(ExtensionContext context) throws Exception {
list.getItems().stream()
.forEach(i -> {
client.resourceList(i).createOrReplace();
System.out.println("Created: " + i.getKind() + " name:" + i.getMetadata().getName() + ".");
LOGGER.info("Created: " + i.getKind() + " name:" + i.getMetadata().getName() + ".");
});

List<HasMetadata> waitables = list.getItems().stream().filter(i->
@@ -100,17 +109,19 @@ public void beforeAll(ExtensionContext context) throws Exception {
i instanceof ReplicaSet ||
i instanceof ReplicationController).collect(Collectors.toList());
long started = System.currentTimeMillis();
System.out.println("Waiting until ready ("+config.getReadinessTimeout()+ " ms)...");
LOGGER.info("Waiting until ready ("+config.getReadinessTimeout()+ " ms)...");
waitUntilCondition(context, waitables, i -> Readiness.isReady(i), config.getReadinessTimeout(), TimeUnit.MILLISECONDS);
long ended = System.currentTimeMillis();
System.out.println("Waited: " + (ended-started)+ " ms.");
LOGGER.info("Waited: " + (ended-started)+ " ms.");
//Display the item status
waitables.stream().map(r->client.resource(r).fromServer().get())
.forEach(i -> {
System.out.println(i.getKind() + ":" + i.getMetadata().getName() + " ready:" + Readiness.isReady(i));
if (!Readiness.isReady(i)) {
LOGGER.warning(i.getKind() + ":" + i.getMetadata().getName() + " not ready!");
getEvents(context, i).getItems().stream().forEach(e -> {
System.out.println(e.getMessage());
if (Strings.isNotNullOrEmpty(e.getMessage())) {
LOGGER.warning(e.getMessage());
}
});
}
});
@@ -130,7 +141,7 @@ public void postProcessTestInstance(Object testInstance, ExtensionContext contex
@Override
public void afterAll(ExtensionContext context) {
getKubernetesResources(context).getItems().stream().forEach(r -> {
System.out.println("Deleting: " + r.getKind() + " name:" +r.getMetadata().getName()+ " status:"+ getKubernetesClient(context).resource(r).cascading(true).delete());
LOGGER.info("Deleting: " + r.getKind() + " name:" +r.getMetadata().getName()+ " status:"+ getKubernetesClient(context).resource(r).cascading(true).delete());
});
}

@@ -16,6 +16,8 @@
package io.dekorate.testing.openshift;

import io.dekorate.DekorateException;
import io.dekorate.Logger;
import io.dekorate.LoggerFactory;
import io.dekorate.deps.kubernetes.api.model.HasMetadata;
import io.dekorate.deps.kubernetes.client.internal.readiness.Readiness;
import io.dekorate.deps.kubernetes.client.KubernetesClient;
@@ -40,6 +42,8 @@
import io.dekorate.testing.WithProject;
import io.dekorate.testing.openshift.config.OpenshiftIntegrationTestConfig;
import io.dekorate.utils.Packaging;
import io.dekorate.utils.Strings;

import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
@@ -60,17 +64,24 @@
public class OpenshiftExtension implements ExecutionCondition, BeforeAllCallback, AfterAllCallback,
WithOpenshiftIntegrationTest, WithPod, WithKubernetesClient, WithOpenshiftResources, WithProject, WithEvents, WithOpenshiftConfig {

private final Logger LOGGER = LoggerFactory.getLogger();

@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
try {
KubernetesClient client = getKubernetesClient(context);
if (!client.isAdaptable(OpenShiftClient.class)) {
return ConditionEvaluationResult.disabled("Could not detect openshift.");
String reason = "Could not detect Openshift!";
return ConditionEvaluationResult.disabled(reason);
}
VersionInfo version = getKubernetesClient(context).getVersion();
return ConditionEvaluationResult.enabled("Found version:" + version);
String reason = "Found version:" + version;
LOGGER.info(reason);
return ConditionEvaluationResult.enabled(reason);
} catch (Throwable t) {
return ConditionEvaluationResult.disabled("Could not communicate with KubernetesExtension API server.");
String reason = "Could not communicate with Openshift API server.";
LOGGER.error(reason);
return ConditionEvaluationResult.disabled(reason);
}
}

@@ -89,7 +100,7 @@ public void beforeAll(ExtensionContext context) throws Exception {
buildResources.stream()
.forEach(i -> {
client.resourceList(i).deletingExisting().createOrReplace();
System.out.println("Created: " + i.getKind() + " name:" + i.getMetadata().getName() + ".");
LOGGER.info("Created: " + i.getKind() + " name:" + i.getMetadata().getName() + ".");
});
OpenshiftUtils.waitForImageStreamTags(buildResources, config.getImageStreamTagTimeout(), TimeUnit.MILLISECONDS);
build(context, getProject());
@@ -102,7 +113,7 @@ public void beforeAll(ExtensionContext context) throws Exception {
remainingResources.stream()
.forEach(i -> {
client.resourceList(i).deletingExisting().createOrReplace();
System.out.println("Created: " + i.getKind() + " name:" + i.getMetadata().getName() + ".");
LOGGER.info("Created: " + i.getKind() + " name:" + i.getMetadata().getName() + ".");
});

OpenshiftConfig openshiftConfig = getOpenshiftConfig();
@@ -113,17 +124,19 @@ public void beforeAll(ExtensionContext context) throws Exception {
i instanceof ReplicaSet ||
i instanceof ReplicationController).collect(Collectors.toList());
long started = System.currentTimeMillis();
System.out.println("Waiting until ready ("+config.getReadinessTimeout()+ " ms)...");
LOGGER.info("Waiting until ready ("+config.getReadinessTimeout()+ " ms)...");
waitUntilCondition(context, waitables, i -> Readiness.isReady(i), config.getReadinessTimeout(), TimeUnit.MILLISECONDS);
long ended = System.currentTimeMillis();
System.out.println("Waited: " + (ended-started)+ " ms.");
LOGGER.info("Waited: " + (ended-started)+ " ms.");
//Display the item status
waitables.stream().map(r->client.resource(r).fromServer().get())
.forEach(i -> {
System.out.println(i.getKind() + ":" + i.getMetadata().getName() + " ready:" + Readiness.isReady(i));
if (!Readiness.isReady(i)) {
LOGGER.warning(i.getKind() + ":" + i.getMetadata().getName() + " not ready!");
getEvents(context, i).getItems().stream().forEach(e -> {
System.out.println(e.getMessage());
if (Strings.isNotNullOrEmpty(e.getMessage())) {
LOGGER.warning(e.getMessage());
}
});
}
});
@@ -147,7 +160,7 @@ public void afterAll(ExtensionContext context) {

getOpenshiftResources(context).getItems().stream().forEach(r -> {
try {
System.out.println("Deleting: " + r.getKind() + " name:" + r.getMetadata().getName() + " status:" + client.resource(r).delete());
LOGGER.info("Deleting: " + r.getKind() + " name:" + r.getMetadata().getName() + " status:" + client.resource(r).delete());
} catch (Exception e) {}
});

@@ -183,7 +196,7 @@ public void build(ExtensionContext context, Project project) {
* @param binaryFile The binary file.
*/
private void binaryBuild(OpenShiftClient client, BuildConfig buildConfig, File binaryFile) {
System.out.println("Running binary build:"+buildConfig.getMetadata().getName()+ " for:" +binaryFile.getAbsolutePath());
LOGGER.info("Running binary build:"+buildConfig.getMetadata().getName()+ " for:" +binaryFile.getAbsolutePath());
Build build = client.buildConfigs().withName(buildConfig.getMetadata().getName()).instantiateBinary().fromFile(binaryFile);
try (BufferedReader reader = new BufferedReader(client.builds().withName(build.getMetadata().getName()).getLogReader())) {
for (String line = reader.readLine(); line != null; line = reader.readLine()) {

0 comments on commit b3148dd

Please sign in to comment.
You can’t perform that action at this time.