Skip to content

Commit

Permalink
Add some fixes, proper cleaning, etc
Browse files Browse the repository at this point in the history
Signed-off-by: Jakub Stejskal <[email protected]>
  • Loading branch information
Frawless committed Dec 6, 2023
1 parent 82a5240 commit 2801510
Show file tree
Hide file tree
Showing 12 changed files with 134 additions and 90 deletions.
2 changes: 1 addition & 1 deletion src/main/java/io/odh/test/Environment.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public class Environment {
public static final String OLM_OPERATOR_CHANNEL = getOrDefault(OLM_OPERATOR_CHANNEL_ENV, OLM_OPERATOR_CHANNEL_DEFAULT);
public static final String OLM_OPERATOR_VERSION = getOrDefault(OLM_OPERATOR_VERSION_ENV, OLM_OPERATOR_VERSION_DEFAULT);

public static final String OPERATOR_INSTALL_TYPE = getOrDefault(OPERATOR_INSTALL_TYPE_ENV, InstallTypes.OLM.toString());
public static final String OPERATOR_INSTALL_TYPE = getOrDefault(OPERATOR_INSTALL_TYPE_ENV, InstallTypes.BUNDLE.toString());

private Environment() { }

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/io/odh/test/framework/TestCallbackListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package io.odh.test.framework;

import io.odh.test.framework.manager.ResourceManager;
import io.odh.test.platform.KubeUtils;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
Expand Down Expand Up @@ -41,6 +42,7 @@ public void beforeEach(ExtensionContext extensionContext) throws Exception {
public void afterAll(ExtensionContext extensionContext) throws Exception {
ResourceManager.getInstance().switchToClassResourceStack();
ResourceManager.getInstance().deleteResources();
KubeUtils.clearOdhCRDs();
}

@Override
Expand Down
42 changes: 31 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 @@ -7,6 +7,7 @@
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.admissionregistration.v1.ValidatingWebhookConfiguration;
import io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinition;
import io.fabric8.kubernetes.api.model.apps.Deployment;
import io.fabric8.kubernetes.api.model.rbac.ClusterRole;
import io.fabric8.kubernetes.api.model.rbac.ClusterRoleBinding;
import io.odh.test.TestConstants;
Expand All @@ -16,6 +17,10 @@
import io.odh.test.framework.manager.resources.OperatorGroupResource;
import io.odh.test.framework.manager.resources.SubscriptionResource;
import io.odh.test.platform.KubeClient;
import io.odh.test.platform.cmdClient.KubeCmdClient;
import io.odh.test.platform.cmdClient.Oc;
import io.odh.test.utils.DeploymentUtils;
import io.opendatahub.datasciencecluster.v1.DataScienceCluster;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -30,6 +35,7 @@ public class ResourceManager {

private static ResourceManager instance;
private static KubeClient client;
private static KubeCmdClient kubeCmdClient;

static final Stack<ResourceItem> CLASS_RESOURCE_STACK = new Stack<>();
static final Stack<ResourceItem> METHOD_RESOURCE_STACK = new Stack<>();
Expand All @@ -40,6 +46,7 @@ public static synchronized ResourceManager getInstance() {
if (instance == null) {
instance = new ResourceManager();
client = new KubeClient(TestConstants.DEFAULT_NAMESPACE);
kubeCmdClient = new Oc();
}
return instance;
}
Expand All @@ -48,6 +55,10 @@ public static KubeClient getClient() {
return client;
}

public static KubeCmdClient getKubeCmdClient() {
return kubeCmdClient;
}

private final ResourceType<?>[] resourceTypes = new ResourceType[]{
new SubscriptionResource(),
new OperatorGroupResource(),
Expand Down Expand Up @@ -82,6 +93,14 @@ private <T extends HasMetadata> void createResource(boolean waitReady, T... reso
for (T resource : resources) {
ResourceType<T> type = findResourceType(resource);

synchronized (this) {
resourceStackPointer.push(
new ResourceItem<T>(
() -> deleteResource(resource),
resource
));
}

if (resource.getMetadata().getNamespace() == null) {
LOGGER.info("Creating/Updating {} {}",
resource.getKind(), resource.getMetadata().getName());
Expand All @@ -93,12 +112,21 @@ private <T extends HasMetadata> void createResource(boolean waitReady, T... reso
if (type == null) {
if (resource instanceof Deployment) {
Deployment deployment = (Deployment) resource;
client.getClient().apps().deployments().resource(deployment).create();
if (client.getClient().apps().deployments().resource(deployment).get() != null) {
client.getClient().apps().deployments().resource(deployment).update();
} else {
client.getClient().apps().deployments().resource(deployment).create();
}
if (waitReady) {
DeploymentUtils.waitForDeploymentReady(resource.getMetadata().getNamespace(), resource.getMetadata().getName());
}
} else {
client.getClient().resource(resource).create();
if (client.getClient().resource(resource).get() != null) {
client.getClient().resource(resource).update();
} else {
client.getClient().resource(resource).create();
}

}
} else {
type.create(resource);
Expand All @@ -107,14 +135,6 @@ private <T extends HasMetadata> void createResource(boolean waitReady, T... reso
String.format("Timed out waiting for %s %s/%s to be ready", resource.getKind(), resource.getMetadata().getNamespace(), resource.getMetadata().getName()));
}
}

synchronized (this) {
resourceStackPointer.push(
new ResourceItem<T>(
() -> deleteResource(resource),
resource
));
}
}
}

Expand Down Expand Up @@ -174,7 +194,7 @@ public final <T extends HasMetadata> boolean waitResourceCondition(T resource, R
assertNotNull(resource.getMetadata().getName());

// cluster role binding and custom resource definition does not need namespace...
if (!(resource instanceof ClusterRoleBinding || resource instanceof CustomResourceDefinition || resource instanceof ClusterRole || resource instanceof ValidatingWebhookConfiguration)) {
if (!(resource instanceof ClusterRoleBinding || resource instanceof CustomResourceDefinition || resource instanceof ClusterRole || resource instanceof ValidatingWebhookConfiguration || resource instanceof DataScienceCluster)) {
assertNotNull(resource.getMetadata().getNamespace());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,62 @@
import io.fabric8.kubernetes.api.model.KubernetesResourceList;
import io.fabric8.kubernetes.client.dsl.MixedOperation;
import io.fabric8.kubernetes.client.dsl.Resource;
import io.odh.test.TestConstants;
import io.odh.test.TestUtils;
import io.odh.test.framework.manager.ResourceManager;
import io.odh.test.framework.manager.ResourceType;
import io.odh.test.platform.KubeUtils;
import io.opendatahub.datasciencecluster.v1.DataScienceCluster;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DataScienceClusterResource implements ResourceType<DataScienceCluster> {

private static final Logger LOGGER = LoggerFactory.getLogger(DataScienceClusterResource.class);
@Override
public String getKind() {
return "DataScienceCluster";
}

@Override
public DataScienceCluster get(String namespace, String name) {
return dataScienceCLusterClient().inNamespace(namespace).withName(name).get();
return dataScienceCLusterClient().withName(name).get();
}

@Override
public void create(DataScienceCluster resource) {
dataScienceCLusterClient().inNamespace(resource.getMetadata().getNamespace()).resource(resource).create();
dataScienceCLusterClient().resource(resource).create();
}

@Override
public void delete(DataScienceCluster resource) {
dataScienceCLusterClient().inNamespace(resource.getMetadata().getNamespace()).withName(resource.getMetadata().getName()).delete();
dataScienceCLusterClient().withName(resource.getMetadata().getName()).delete();
}

@Override
public void update(DataScienceCluster resource) {
dataScienceCLusterClient().inNamespace(resource.getMetadata().getNamespace()).resource(resource).update();
dataScienceCLusterClient().resource(resource).update();
}

@Override
public boolean waitForReadiness(DataScienceCluster resource) {
return resource != null;
String message = String.format("DataScienceCluster %s readiness", resource.getMetadata().getName());
TestUtils.waitFor(message, TestConstants.GLOBAL_POLL_INTERVAL_SHORT, TestConstants.GLOBAL_TIMEOUT, () -> {
boolean dscReady;

DataScienceCluster dsc = dataScienceCLusterClient().withName(resource.getMetadata().getName()).get();

String dashboardStatus = KubeUtils.getDscConditionByType(dsc.getStatus().getConditions(), "dashboardReady").getStatus();
LOGGER.debug("DataScienceCluster {} dashboard status: {}", resource.getMetadata().getName(), dashboardStatus);
dscReady = dashboardStatus.equals("True");

String workbenchesStatus = KubeUtils.getDscConditionByType(dsc.getStatus().getConditions(), "workbenchesReady").getStatus();
LOGGER.debug("DataScienceCluster {} workbenches status: {}", resource.getMetadata().getName(), workbenchesStatus);
dscReady = dscReady && workbenchesStatus.equals("True");

return dscReady;
}, () -> { });
return true;
}

public static MixedOperation<DataScienceCluster, KubernetesResourceList<DataScienceCluster>, Resource<DataScienceCluster>> dataScienceCLusterClient() {
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/io/odh/test/install/BundleInstall.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
import io.odh.test.Environment;
import io.odh.test.TestConstants;
import io.odh.test.TestUtils;
import io.odh.test.framework.manager.ResourceItem;
import io.odh.test.framework.manager.ResourceManager;
import io.odh.test.platform.KubeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -55,7 +57,8 @@ public void printResources() {
});
}

public void installBundle() {
public void create() {
ResourceManager.getInstance().createResourceWithWait(resources.toArray(new HasMetadata[0]));
ResourceManager.getInstance().pushToStack(new ResourceItem(KubeUtils::deleteDefaultDSCI, null));
}
}
2 changes: 2 additions & 0 deletions src/main/java/io/odh/test/install/OlmInstall.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import io.odh.test.framework.manager.ResourceItem;
import io.odh.test.framework.manager.ResourceManager;
import io.odh.test.framework.manager.resources.OperatorGroupResource;
import io.odh.test.platform.KubeUtils;
import io.odh.test.utils.DeploymentUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -68,6 +69,7 @@ private void createAndModifySubscription() {
Subscription subscription = prepareSubscription();

ResourceManager.getInstance().createResourceWithWait(subscription);
ResourceManager.getInstance().pushToStack(new ResourceItem(KubeUtils::deleteDefaultDSCI, null));
}
public void updateSubscription() {
Subscription subscription = prepareSubscription();
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/io/odh/test/platform/KubeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
package io.odh.test.platform;

import io.odh.test.framework.manager.ResourceManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -21,6 +22,23 @@ public static org.kubeflow.v1.notebookstatus.Conditions getNotebookConditionByTy
return conditions.stream().filter(c -> c.getType().equals(type)).findFirst().orElseGet(null);
}

public static void clearOdhCRDs() {
ResourceManager.getClient().getClient().apiextensions().v1().customResourceDefinitions().list().getItems()
.stream().filter(crd -> crd.getMetadata().getName().contains("opendatahub.io")).toList()
.forEach(crd -> {
LOGGER.info("Deleting CRD {}", crd.getMetadata().getName());
ResourceManager.getClient().getClient().resource(crd).delete();
});
}

/**
* TODO - this should be removed when https://github.com/opendatahub-io/opendatahub-operator/issues/765 will be resolved
*/
public static void deleteDefaultDSCI() {
LOGGER.info("Clearing DSCI ...");
ResourceManager.getKubeCmdClient().exec("delete", "dsci", "--all");
}

private KubeUtils() {
}
}
27 changes: 0 additions & 27 deletions src/test/java/io/odh/test/e2e/Abstract.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,13 @@

import io.odh.test.framework.ExtensionContextParameterResolver;
import io.odh.test.framework.TestCallbackListener;
import io.odh.test.Environment;
import io.odh.test.framework.manager.ResourceManager;
import io.odh.test.framework.TestExceptionCallbackListener;
import io.odh.test.install.InstallTypes;
import io.odh.test.install.OlmInstall;
import io.odh.test.framework.TestSeparator;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
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;

import static org.junit.jupiter.api.Assertions.assertTrue;

@DisplayNameGeneration(DisplayNameGenerator.IndicativeSentences.class)
@ExtendWith(TestExceptionCallbackListener.class)
@ExtendWith(TestCallbackListener.class)
Expand All @@ -31,23 +23,4 @@ public class Abstract {
static {
ResourceManager.getInstance();
}

@BeforeAll
void setupEnvironment() {
if (Environment.OPERATOR_INSTALL_TYPE.equals(InstallTypes.OLM.toString())) {
OlmInstall olmInstall = new OlmInstall();
olmInstall.create();
} else if (Environment.OPERATOR_INSTALL_TYPE.equals(InstallTypes.BUNDLE.toString())) {
LOGGER.error("Bundle install is not implemented yet!");
assertTrue(false);
} else {
LOGGER.error("Unknown install type {}! You should implement it at first!", Environment.OPERATOR_INSTALL_TYPE);
assertTrue(false);
}
}

@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.api.model.NamespaceBuilder;
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.manager.ResourceManager;
import io.opendatahub.datasciencecluster.v1.DataScienceCluster;
import io.opendatahub.datasciencecluster.v1.DataScienceClusterBuilder;
Expand All @@ -31,7 +30,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;

@Tag("standard")
public class DataScienceClusterIT extends Abstract {
public class DataScienceClusterIT extends StandardAbstract {

private static final String DS_PROJECT_NAME = "test-dsp";
private static final String DS_PROJECT_NAMESPACE = "test-ns-ds";
Expand Down
8 changes: 3 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 @@ -12,7 +12,6 @@
import io.fabric8.kubernetes.api.model.PersistentVolumeClaimBuilder;
import io.fabric8.kubernetes.api.model.Quantity;
import io.odh.test.OdhAnnotationsLabels;
import io.odh.test.e2e.Abstract;
import io.odh.test.framework.manager.ResourceManager;
import io.odh.test.framework.manager.resources.NotebookResource;
import io.odh.test.utils.PodUtils;
Expand All @@ -35,8 +34,9 @@
import org.kubeflow.v1.NotebookBuilder;

import java.io.IOException;
import java.util.Map;

public class NotebookST extends Abstract {
public class NotebookST extends StandardAbstract {

private static final String DS_PROJECT_NAME = "test-notebooks";
private static final String DS_PROJECT_NAMESPACE = "test-notebooks";
Expand Down Expand Up @@ -75,7 +75,7 @@ void testCreateSimpleNotebook() throws IOException {
ResourceManager.getInstance().createResourceWithoutWait(notebook);

LabelSelector lblSelector = new LabelSelectorBuilder()
.withMatchLabels(ResourceManager.getClient().listPods(NTB_NAMESPACE).get(0).getMetadata().getLabels())
.withMatchLabels(Map.of("app", NTB_NAME))
.build();

PodUtils.waitForPodsReady(NTB_NAMESPACE, lblSelector, 1, true, () -> { });
Expand All @@ -92,7 +92,6 @@ void deployDataScienceCluster() {
DataScienceCluster dsc = new DataScienceClusterBuilder()
.withNewMetadata()
.withName(DS_PROJECT_NAME)
.withNamespace(DS_PROJECT_NAMESPACE)
.endMetadata()
.withNewSpec()
.withComponents(
Expand All @@ -117,6 +116,5 @@ void deployDataScienceCluster() {
.build();
// Deploy DSC
ResourceManager.getInstance().createResourceWithWait(dsc);
// TODO - tady musi byt wait na DSC pody
}
}
Loading

0 comments on commit 2801510

Please sign in to comment.