Skip to content

Commit

Permalink
Add basic test for upgrade ODH using bundle (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
kornys authored Dec 7, 2023
1 parent 0b65b8e commit 2cd80f9
Show file tree
Hide file tree
Showing 17 changed files with 267 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,14 @@
* Copyright Skodjob authors.
* License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html).
*/
package io.odh.test.framework;
package io.odh.test.framework.listeners;

import io.odh.test.platform.KubeUtils;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class OdhResourceCleaner implements AfterAllCallback {

static final Logger LOGGER = LoggerFactory.getLogger(OdhResourceCleaner.class);

static final String SEPARATOR_CHAR = "#";

@Override
public void afterAll(ExtensionContext extensionContext) {
KubeUtils.clearOdhCRDs();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,19 @@
* Copyright Skodjob authors.
* License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html).
*/
package io.odh.test.framework;
package io.odh.test.framework.listeners;

import io.odh.test.framework.manager.ResourceManager;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;

/**
* jUnit5 specific class which listening on test callbacks
*/
public class TestCallbackListener implements BeforeAllCallback, BeforeEachCallback, AfterAllCallback, AfterEachCallback {

static final Logger LOGGER = LoggerFactory.getLogger(TestCallbackListener.class);

static final String SEPARATOR_CHAR = "#";
public class ResourceManagerContextHandler implements BeforeAllCallback, BeforeEachCallback, AfterAllCallback, AfterEachCallback {

@Override
public void beforeAll(ExtensionContext extensionContext) throws Exception {
Expand All @@ -31,24 +23,16 @@ public void beforeAll(ExtensionContext extensionContext) throws Exception {

@Override
public void beforeEach(ExtensionContext extensionContext) throws Exception {
LOGGER.info(String.join("", Collections.nCopies(76, SEPARATOR_CHAR)));
LOGGER.info(String.format("%s.%s-STARTED", extensionContext.getRequiredTestClass().getName(),
extensionContext.getDisplayName().replace("()", "")));
ResourceManager.getInstance().switchToTestResourceStack();
}

@Override
public void afterAll(ExtensionContext extensionContext) throws Exception {
ResourceManager.getInstance().switchToClassResourceStack();
ResourceManager.getInstance().deleteResources();
}

@Override
public void afterEach(ExtensionContext extensionContext) throws Exception {
LOGGER.info(String.format("%s.%s-FINISHED", extensionContext.getRequiredTestClass().getName(),
extensionContext.getDisplayName().replace("()", "")));
LOGGER.info(String.join("", Collections.nCopies(76, SEPARATOR_CHAR)));
ResourceManager.getInstance().switchToTestResourceStack();
ResourceManager.getInstance().deleteResources();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright Skodjob authors.
* License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html).
*/
package io.odh.test.framework.listeners;

import io.odh.test.framework.manager.ResourceManager;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;

/**
* jUnit5 specific class which listening on test callbacks
*/
public class ResourceManagerDeleteHandler implements AfterAllCallback, AfterEachCallback {
@Override
public void afterAll(ExtensionContext extensionContext) throws Exception {
ResourceManager.getInstance().switchToClassResourceStack();
ResourceManager.getInstance().deleteResources();
}

@Override
public void afterEach(ExtensionContext extensionContext) throws Exception {
ResourceManager.getInstance().switchToTestResourceStack();
ResourceManager.getInstance().deleteResources();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright Skodjob authors.
* License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html).
*/
package io.odh.test.framework;
package io.odh.test.framework.listeners;

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

import io.odh.test.Environment;
import org.junit.platform.launcher.TestExecutionListener;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright Skodjob authors.
* License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html).
*/
package io.odh.test.framework.listeners;

import io.odh.test.framework.ExtensionContextParameterResolver;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;

@ExtendWith(ExtensionContextParameterResolver.class)
public interface TestVisualSeparator {
Logger LOGGER = LoggerFactory.getLogger(TestVisualSeparator.class);
String SEPARATOR_CHAR = "#";

@BeforeEach
default void beforeEachTest(ExtensionContext testContext) {
LOGGER.info(String.join("", Collections.nCopies(76, SEPARATOR_CHAR)));
LOGGER.info(String.format("%s.%s-STARTED", testContext.getRequiredTestClass().getName(), testContext.getDisplayName()));
}

@AfterEach
default void afterEachTest(ExtensionContext testContext) {
LOGGER.info(String.format("%s.%s-FINISHED", testContext.getRequiredTestClass().getName(), testContext.getDisplayName()));
LOGGER.info(String.join("", Collections.nCopies(76, SEPARATOR_CHAR)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ public Namespace get(String namespace, String name) {

@Override
public void create(Namespace resource) {
ResourceManager.getClient().getClient().resource(resource).create();
if (get("", resource.getMetadata().getName()) != null) {
ResourceManager.getClient().getClient().resource(resource).update();
} else {
ResourceManager.getClient().getClient().resource(resource).create();
}
}

@Override
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/io/odh/test/install/BundleInstall.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -26,13 +27,16 @@ public class BundleInstall {
private static final Logger LOGGER = LoggerFactory.getLogger(BundleInstall.class);

List<HasMetadata> resources;
File installFile;

public BundleInstall(String installFilePath) throws IOException {
InputStream is;
if (installFilePath.equals(TestConstants.LATEST_BUNDLE_DEPLOY_FILE)
|| installFilePath.equals(TestConstants.RELEASED_BUNDLE_DEPLOY_FILE)) {
installFile = new File("src/main/resources/" + installFilePath);
is = TestUtils.getFileFromResourceAsStream(installFilePath);
} else {
installFile = new File(installFilePath);
is = new FileInputStream(installFilePath);
}
resources = ResourceManager.getClient().readResourcesFromYaml(is);
Expand All @@ -51,6 +55,12 @@ public String getDeploymentName() {
return resources.stream().filter(r -> r instanceof Deployment).findFirst().get().getMetadata().getName();
}

public String getDeploymentImage() {
return ((Deployment) resources.stream().filter(r -> r instanceof Deployment).findFirst().get())
.getSpec().getTemplate().getSpec().getContainers()
.stream().filter(c -> c.getName().equals("manager")).findFirst().get().getImage();
}

public void printResources() {
resources.forEach(r -> {
LOGGER.info("Kind: {}, Name: {}", r.getKind(), r.getMetadata().getName());
Expand All @@ -61,4 +71,13 @@ public void create() {
ResourceManager.getInstance().createResourceWithWait(resources.toArray(new HasMetadata[0]));
ResourceManager.getInstance().pushToStack(new ResourceItem(KubeUtils::deleteDefaultDSCI, null));
}

public void createWithoutResourceManager() throws IOException {
ResourceManager.getKubeCmdClient().namespace(getNamespace()).apply(installFile.getAbsolutePath());
}

public void deleteWithoutResourceManager() throws IOException {
KubeUtils.deleteDefaultDSCI();
ResourceManager.getKubeCmdClient().namespace(getNamespace()).delete(installFile.getAbsolutePath());
}
}
2 changes: 1 addition & 1 deletion src/main/java/io/odh/test/platform/KubeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static void clearOdhCRDs() {
*/
public static void deleteDefaultDSCI() {
LOGGER.info("Clearing DSCI ...");
ResourceManager.getKubeCmdClient().exec("delete", "dsci", "--all");
ResourceManager.getKubeCmdClient().exec(false, "delete", "dsci", "--all");
}

private KubeUtils() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public K replace(File... files) {

@Override
@SuppressWarnings("unchecked")
public K applyContent(String yamlContent) {
public K applyContentInNamespace(String yamlContent) {
try (Context context = defaultContext()) {
Exec.exec(yamlContent, namespacedCommand(APPLY, "-f", "-"));
return (K) this;
Expand All @@ -178,13 +178,31 @@ public K applyContent(String yamlContent) {

@Override
@SuppressWarnings("unchecked")
public K deleteContent(String yamlContent) {
public K deleteContentInNamespace(String yamlContent) {
try (Context context = defaultContext()) {
Exec.exec(yamlContent, namespacedCommand(DELETE, "-f", "-"), 0, true, false);
return (K) this;
}
}

@Override
@SuppressWarnings("unchecked")
public K applyContent(String yamlContent) {
try (Context context = defaultContext()) {
Exec.exec(yamlContent, command(Arrays.asList(APPLY, "-f", "-"), false), 0, true, true);
return (K) this;
}
}

@Override
@SuppressWarnings("unchecked")
public K deleteContent(String yamlContent) {
try (Context context = defaultContext()) {
Exec.exec(yamlContent, command(Arrays.asList(DELETE, "-f", "-"), false), 0, true, false);
return (K) this;
}
}

@Override
@SuppressWarnings("unchecked")
public K createNamespace(String name) {
Expand Down Expand Up @@ -297,7 +315,7 @@ public void createResourceAndApply(String template, Map<String, String> params)
}

String yaml = Exec.exec(cmd).out();
applyContent(yaml);
applyContentInNamespace(yaml);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ default K delete(String... files) {
*/
K replace(File... files);

K applyContentInNamespace(String yamlContent);

K deleteContentInNamespace(String yamlContent);

K applyContent(String yamlContent);

K deleteContent(String yamlContent);
Expand Down
11 changes: 5 additions & 6 deletions src/test/java/io/odh/test/e2e/Abstract.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,20 @@
*/
package io.odh.test.e2e;

import io.odh.test.framework.ExtensionContextParameterResolver;
import io.odh.test.framework.TestCallbackListener;
import io.odh.test.framework.listeners.ResourceManagerContextHandler;
import io.odh.test.framework.listeners.TestVisualSeparator;
import io.odh.test.framework.manager.ResourceManager;
import io.odh.test.framework.TestExceptionCallbackListener;
import io.odh.test.framework.listeners.TestExceptionCallbackListener;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.extension.ExtendWith;

@DisplayNameGeneration(DisplayNameGenerator.IndicativeSentences.class)
@ExtendWith(TestExceptionCallbackListener.class)
@ExtendWith(TestCallbackListener.class)
@ExtendWith(ExtensionContextParameterResolver.class)
@ExtendWith(ResourceManagerContextHandler.class)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class Abstract {
public class Abstract implements TestVisualSeparator {

static {
ResourceManager.getInstance();
Expand Down
5 changes: 0 additions & 5 deletions src/test/java/io/odh/test/e2e/standard/NotebookST.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
public class NotebookST extends StandardAbstract {

private static final String DS_PROJECT_NAME = "test-notebooks";
private static final String DS_PROJECT_NAMESPACE = "test-notebooks";

private static final String NTB_NAME = "test-odh-notebook";
private static final String NTB_NAMESPACE = "test-odh-notebook";
Expand Down Expand Up @@ -84,10 +83,6 @@ void testCreateSimpleNotebook() throws IOException {

@BeforeAll
void deployDataScienceCluster() {
// Create namespace
Namespace ns = new NamespaceBuilder().withNewMetadata().withName(DS_PROJECT_NAMESPACE).endMetadata().build();
ResourceManager.getInstance().createResourceWithoutWait(ns);

// Create DSC
DataScienceCluster dsc = new DataScienceClusterBuilder()
.withNewMetadata()
Expand Down
4 changes: 3 additions & 1 deletion src/test/java/io/odh/test/e2e/standard/StandardAbstract.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

import io.odh.test.Environment;
import io.odh.test.e2e.Abstract;
import io.odh.test.framework.OdhResourceCleaner;
import io.odh.test.framework.listeners.OdhResourceCleaner;
import io.odh.test.framework.listeners.ResourceManagerDeleteHandler;
import io.odh.test.framework.manager.ResourceManager;
import io.odh.test.install.BundleInstall;
import io.odh.test.install.InstallTypes;
Expand All @@ -22,6 +23,7 @@
import static org.junit.jupiter.api.Assertions.fail;

@ExtendWith(OdhResourceCleaner.class)
@ExtendWith(ResourceManagerDeleteHandler.class)
public class StandardAbstract extends Abstract {

private static final Logger LOGGER = LoggerFactory.getLogger(Abstract.class);
Expand Down
11 changes: 0 additions & 11 deletions src/test/java/io/odh/test/e2e/standard/UpgradeST.java

This file was deleted.

Loading

0 comments on commit 2cd80f9

Please sign in to comment.