Skip to content

Commit

Permalink
Use two resoruce stack for class wide and test resources (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
kornys authored Dec 5, 2023
1 parent 3978997 commit 7fb74eb
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 62 deletions.
54 changes: 54 additions & 0 deletions src/main/java/io/odh/test/framework/TestCallbackListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* 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;

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 = "#";

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

@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
Expand Up @@ -10,8 +10,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

static {
Environment.print();
Expand Down
34 changes: 0 additions & 34 deletions src/main/java/io/odh/test/framework/TestSeparator.java

This file was deleted.

39 changes: 28 additions & 11 deletions src/main/java/io/odh/test/framework/manager/ResourceManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ public class ResourceManager {
private static ResourceManager instance;
private static KubeClient client;

public static final Stack<ResourceItem> RESOURCE_STACK = new Stack<>();
static final Stack<ResourceItem> CLASS_RESOURCE_STACK = new Stack<>();
static final Stack<ResourceItem> METHOD_RESOURCE_STACK = new Stack<>();

static Stack<ResourceItem> resourceStackPointer = CLASS_RESOURCE_STACK;

public static synchronized ResourceManager getInstance() {
if (instance == null) {
Expand All @@ -50,6 +53,18 @@ public static KubeClient getClient() {
new OperatorGroupResource(),
};

public final void switchToTestResourceStack() {
resourceStackPointer = METHOD_RESOURCE_STACK;
}

public final void switchToClassResourceStack() {
resourceStackPointer = CLASS_RESOURCE_STACK;
}

public final void pushToStack(ResourceItem item) {
resourceStackPointer.push(item);
}

@SafeVarargs
public final <T extends HasMetadata> void createResourceWithoutWait(T... resources) {
createResource(false, resources);
Expand Down Expand Up @@ -95,7 +110,7 @@ private <T extends HasMetadata> void createResource(boolean waitReady, T... reso
}

synchronized (this) {
RESOURCE_STACK.push(
resourceStackPointer.push(
new ResourceItem<T>(
() -> deleteResource(resource),
resource
Expand Down Expand Up @@ -178,17 +193,19 @@ public final <T extends HasMetadata> boolean waitResourceCondition(T resource, R
}

public void deleteResources() {
LOGGER.info(String.join("", Collections.nCopies(76, "#")));

while (!RESOURCE_STACK.empty()) {
try {
ResourceItem resourceItem = RESOURCE_STACK.pop();
resourceItem.getThrowableRunner().run();
} catch (Exception e) {
e.printStackTrace();
if (!resourceStackPointer.isEmpty()) {
LOGGER.info(String.join("", Collections.nCopies(76, "#")));

while (!resourceStackPointer.empty()) {
try {
ResourceItem resourceItem = resourceStackPointer.pop();
resourceItem.getThrowableRunner().run();
} catch (Exception e) {
e.printStackTrace();
}
}
LOGGER.info(String.join("", Collections.nCopies(76, "#")));
}
LOGGER.info(String.join("", Collections.nCopies(76, "#")));
}

private <T extends HasMetadata> ResourceType<T> findResourceType(T resource) {
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/io/odh/test/install/OlmInstall.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class OlmInstall {

public void create() {
createOperatorGroup();
ResourceManager.RESOURCE_STACK.push(new ResourceItem(this::deleteCSV));
ResourceManager.getInstance().pushToStack(new ResourceItem(this::deleteCSV));
createAndModifySubscription();

// Wait for operator creation
Expand Down Expand Up @@ -68,8 +68,6 @@ private void createAndModifySubscription() {
Subscription subscription = prepareSubscription();

ResourceManager.getInstance().createResourceWithWait(subscription);
// ResourceManager.RESOURCE_STACK.push(new ResourceItem(this::deleteCSV));

}
public void updateSubscription() {
Subscription subscription = prepareSubscription();
Expand Down
12 changes: 5 additions & 7 deletions src/test/java/io/odh/test/e2e/Abstract.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,24 @@
*/
package io.odh.test.e2e;

import io.odh.test.framework.ExtensionContextParameterResolver;
import io.odh.test.framework.TestCallbackListener;
import io.odh.test.framework.manager.ResourceManager;
import io.odh.test.framework.TestExceptionCallbackListener;
import io.odh.test.framework.TestSeparator;
import org.junit.jupiter.api.AfterAll;
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)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class Abstract implements TestSeparator {
public class Abstract {

static {
ResourceManager.getInstance();
}

@AfterAll
void teardownEnvironment() {
ResourceManager.getInstance().deleteResources();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import io.fabric8.kubernetes.client.dsl.MixedOperation;
import io.fabric8.kubernetes.client.dsl.Resource;
import io.odh.test.e2e.Abstract;
import io.odh.test.framework.TestSeparator;
import io.odh.test.framework.manager.ResourceManager;
import io.odh.test.platform.KubeUtils;
import org.junit.jupiter.api.BeforeAll;
Expand All @@ -27,7 +26,7 @@

@Tag("continuous")
public class DataScienceProjectIT extends Abstract {
static final Logger LOGGER = LoggerFactory.getLogger(TestSeparator.class);
static final Logger LOGGER = LoggerFactory.getLogger(DataScienceProjectIT.class);

MixedOperation<Notebook, KubernetesResourceList<Notebook>, Resource<Notebook>> notebookCli;

Expand Down
33 changes: 31 additions & 2 deletions src/test/java/io/odh/test/unit/UnitTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,56 @@
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.framework.TestSeparator;
import io.odh.test.framework.ExtensionContextParameterResolver;
import io.opendatahub.datasciencecluster.v1.DataScienceCluster;
import io.opendatahub.datasciencecluster.v1.DataScienceClusterBuilder;
import io.opendatahub.datasciencecluster.v1.datascienceclusterspec.ComponentsBuilder;
import io.opendatahub.datasciencecluster.v1.datascienceclusterspec.components.Codeflare;
import io.opendatahub.datasciencecluster.v1.datascienceclusterspec.components.CodeflareBuilder;
import io.opendatahub.v1alpha.OdhDashboardConfig;
import io.opendatahub.v1alpha.OdhDashboardConfigBuilder;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
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;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;

@Tag("unit")
@ExtendWith(ExtensionContextParameterResolver.class)
@EnableKubernetesMockClient(crud = true)
public class UnitTests implements TestSeparator {
public class UnitTests {

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

static final String SEPARATOR_CHAR = "#";

private KubernetesClient kubernetesClient;

private KubernetesMockServer server;

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

@AfterEach
void after(ExtensionContext extensionContext) {
LOGGER.info(String.format("%s.%s-FINISHED", extensionContext.getRequiredTestClass().getName(),
extensionContext.getDisplayName().replace("()", "")));
LOGGER.info(String.join("", Collections.nCopies(76, SEPARATOR_CHAR)));
}

@Test
void testCreateDeleteDataScienceCluster() {
MixedOperation<DataScienceCluster, KubernetesResourceList<DataScienceCluster>, Resource<DataScienceCluster>> dsClient =
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
io.odh.test.framework.ExecutionListener
io.odh.test.framework.TestPlanExecutionListener

0 comments on commit 7fb74eb

Please sign in to comment.