Skip to content

Commit

Permalink
Add execution listeners and ability to connect to kube based on token (
Browse files Browse the repository at this point in the history
  • Loading branch information
kornys authored Nov 10, 2023
1 parent 67a4a6d commit e35a01f
Show file tree
Hide file tree
Showing 11 changed files with 177 additions and 6 deletions.
29 changes: 29 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,35 @@
<artifactId>odh-e2e</artifactId>
<version>1.0-SNAPSHOT</version>

<name>OpenDataHub e2e</name>
<url>https://github.com/ExcelentProject/odh-e2e</url>
<description>Test suite for testing opendatahub</description>

<licenses>
<license>
<name>Apache License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>

<developers>
<developer>
<id>kornys</id>
<name>David Kornel</name>
<email>[email protected]</email>
</developer>
<developer>
<id>Frawless</id>
<name>Jakub Stejskal</name>
<email>[email protected]</email>
</developer>
</developers>

<scm>
<url>https://github.com/ExcelentProject/odh-e2e</url>
</scm>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
Expand Down
61 changes: 61 additions & 0 deletions src/main/java/io/odh/test/Environment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright Tealc authors.
* License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html).
*/
package io.odh.test;

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

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

/**
* Class which holds environment variables for system tests.
*/
public class Environment {

private static final Logger LOGGER = LoggerFactory.getLogger(Environment.class);
private static final Map<String, String> VALUES = new HashMap<>();

private static final String USERNAME_ENV = "KUBE_USERNAME";
private static final String PASSWORD_ENV = "KUBE_PASSWORD";
private static final String TOKEN_ENV = "KUBE_TOKEN";
private static final String URL_ENV = "KUBE_URL";

/**
* Set values
*/
public static final String RUN_USER = getOrDefault("USER", null);
public static final String KUBE_USERNAME = getOrDefault(USERNAME_ENV, null);
public static final String KUBE_PASSWORD = getOrDefault(PASSWORD_ENV, null);
public static final String KUBE_TOKEN = getOrDefault(TOKEN_ENV, null);
public static final String KUBE_URL = getOrDefault(URL_ENV, null);

private Environment() { }

static {
String debugFormat = "{}: {}";
LOGGER.info("Used environment variables:");
VALUES.entrySet().stream()
.sorted(Map.Entry.comparingByKey())
.forEach(entry -> LOGGER.info(debugFormat, entry.getKey(), entry.getValue()));
}

public static void print() { }

private static String getOrDefault(String varName, String defaultValue) {
return getOrDefault(varName, String::toString, defaultValue);
}

private static <T> T getOrDefault(String var, Function<String, T> converter, T defaultValue) {
String value = System.getenv(var);
T returnValue = defaultValue;
if (value != null) {
returnValue = converter.apply(value);
}
VALUES.put(var, String.valueOf(returnValue));
return returnValue;
}
}
4 changes: 4 additions & 0 deletions src/main/java/io/odh/test/TestConstants.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/*
* Copyright Tealc authors.
* License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html).
*/
package io.odh.test;

public class TestConstants {
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/io/odh/test/framework/ExecutionListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright Tealc authors.
* License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html).
*/
package io.odh.test.framework;

import io.odh.test.Environment;
import org.junit.platform.launcher.TestExecutionListener;
import org.junit.platform.launcher.TestPlan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ExecutionListener implements TestExecutionListener {
Logger LOGGER = LoggerFactory.getLogger(TestSeparator.class);

public void testPlanExecutionStarted(TestPlan testPlan) {
LOGGER.info("=======================================================================");
LOGGER.info("=======================================================================");
LOGGER.info(" Test run started");
LOGGER.info("=======================================================================");
LOGGER.info("=======================================================================");
Environment.print();
}

public void testPlanExecutionFinished(TestPlan testPlan) {
LOGGER.info("=======================================================================");
LOGGER.info("=======================================================================");
LOGGER.info(" Test run finished");
LOGGER.info("=======================================================================");
LOGGER.info("=======================================================================");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright Tealc authors.
* License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html).
*/
package io.odh.test.separator;
package io.odh.test.framework;

import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ParameterContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright Tealc authors.
* License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html).
*/
package io.odh.test.separator;
package io.odh.test.framework;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
Expand Down
36 changes: 34 additions & 2 deletions src/main/java/io/odh/test/platform/KubeClient.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/*
* Copyright Tealc authors.
* License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html).
*/
package io.odh.test.platform;

import io.fabric8.kubernetes.api.model.ConfigMap;
Expand All @@ -12,12 +16,14 @@
import io.fabric8.kubernetes.api.model.batch.v1.JobList;
import io.fabric8.kubernetes.api.model.batch.v1.JobStatus;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.ConfigBuilder;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientBuilder;
import io.fabric8.kubernetes.client.dsl.MixedOperation;
import io.fabric8.kubernetes.client.dsl.Resource;
import io.fabric8.kubernetes.client.dsl.RollableScalableResource;
import io.fabric8.openshift.client.OpenShiftClient;
import io.odh.test.Environment;
import io.opendatahub.datasciencecluster.v1.DataScienceCluster;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -33,7 +39,7 @@ public class KubeClient {

public KubeClient(String namespace) {
LOGGER.debug("Creating client in namespace: {}", namespace);
Config config = Config.autoConfigure(System.getenv().getOrDefault("KUBE_CONTEXT", null));
Config config = getConfig();

this.client = new KubernetesClientBuilder()
.withConfig(config)
Expand Down Expand Up @@ -74,6 +80,31 @@ public KubeClient inNamespace(String namespace) {
return this;
}

private Config getConfig() {
if (Environment.KUBE_PASSWORD != null &&
Environment.KUBE_PASSWORD != null &&
Environment.KUBE_URL != null) {
return new ConfigBuilder()
.withUsername(Environment.KUBE_USERNAME)
.withPassword(Environment.KUBE_PASSWORD)
.withMasterUrl(Environment.KUBE_URL)
.withDisableHostnameVerification(true)
.withTrustCerts(true)
.build();
} else if (Environment.KUBE_URL != null &&
Environment.KUBE_TOKEN != null) {
return new ConfigBuilder()
.withOauthToken(Environment.KUBE_TOKEN)
.withMasterUrl(Environment.KUBE_URL)
.withDisableHostnameVerification(true)
.withTrustCerts(true)
.build();
} else {
return Config.autoConfigure(System.getenv()
.getOrDefault("KUBE_CONTEXT", null));
}
}

public String getNamespace() {
return namespace;
}
Expand Down Expand Up @@ -123,6 +154,7 @@ public List<Pod> listPods(String namespaceName) {

/**
* Returns list of pods by prefix in pod name
*
* @param namespaceName Namespace name
* @param podNamePrefix prefix with which the name should begin
* @return List of pods
Expand Down Expand Up @@ -265,7 +297,7 @@ public List<Job> listJobs(String namePrefix) {
.filter(job -> job.getMetadata().getName().startsWith(namePrefix)).collect(Collectors.toList());
}

public MixedOperation<DataScienceCluster, KubernetesResourceList<DataScienceCluster>, Resource<DataScienceCluster>> dataScienceClusterClient() {
public MixedOperation<DataScienceCluster, KubernetesResourceList<DataScienceCluster>, Resource<DataScienceCluster>> dataScienceClusterClient() {
return client.resources(DataScienceCluster.class);
}

Expand Down
6 changes: 5 additions & 1 deletion src/test/java/io/odh/test/e2e/Abstract.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
/*
* Copyright Tealc authors.
* License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html).
*/
package io.odh.test.e2e;

import io.odh.test.platform.KubeClient;
import io.odh.test.TestConstants;
import io.odh.test.separator.TestSeparator;
import io.odh.test.framework.TestSeparator;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.TestInstance;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/*
* Copyright Tealc authors.
* License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html).
*/
package io.odh.test.e2e.deployed;

import io.fabric8.kubernetes.api.model.KubernetesResourceList;
Expand Down
6 changes: 5 additions & 1 deletion src/test/java/io/odh/test/unit/UnitTests.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/*
* Copyright Tealc authors.
* License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html).
*/
package io.odh.test.unit;

import io.fabric8.kubernetes.api.model.KubernetesResourceList;
Expand All @@ -6,7 +10,7 @@
import io.fabric8.kubernetes.client.dsl.Resource;
import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient;
import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer;
import io.odh.test.separator.TestSeparator;
import io.odh.test.framework.TestSeparator;
import io.opendatahub.datasciencecluster.v1.DataScienceCluster;
import io.opendatahub.datasciencecluster.v1.DataScienceClusterBuilder;
import io.opendatahub.datasciencecluster.v1.datascienceclusterspec.ComponentsBuilder;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.odh.test.framework.ExecutionListener

0 comments on commit e35a01f

Please sign in to comment.