Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable ResourceManager clean resoruce by annotation property #46

Merged
merged 3 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,17 @@ class Test {
}
//...
```
3. Work with `KubeResourceManager` and clients
3. To disable `KubeResourceManager` clean resources functionality
```java
//...
@ResourceManager(cleanResources = false)
@TestVisualSeparator
class Test {
//...
}
//...
```
4. Work with `KubeResourceManager` and clients
```java
//...
@ResourceManager
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,17 @@
* This annotation is used to manage resources in JUnit tests.
* It is applied at the class level.
* <p>
* It uses the {@link ResourceManagerExtension} and {@link ResourceManagerCleanerExtension}
* It uses the {@link ResourceManagerExtension}
* to set up and clean up resources before and after each test.
*/
@Target(ElementType.TYPE)
@Retention(RUNTIME)
@ExtendWith(ResourceManagerExtension.class)
@ExtendWith(ResourceManagerCleanerExtension.class)
public @interface ResourceManager {
/**
* Enables cleaner extension for resource manager
* @return enable/disable cleaner
*/
boolean cleanResources() default true;
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,42 @@
*/
package io.skodjob.testframe.listeners;

import io.skodjob.testframe.annotations.ResourceManager;
import io.skodjob.testframe.resources.KubeResourceManager;
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
* Enables cleaner extension based on cleanResources value
*/
public class ResourceManagerCleanerExtension
implements AfterAllCallback, AfterEachCallback {
public class ResourceManagerCleanerExtension implements AfterAllCallback, AfterEachCallback {

/**
* Enables ResourceManagerCleanerExtension for afterAll callback
* @param extensionContext context
*/
@Override
public void afterAll(ExtensionContext extensionContext) {
KubeResourceManager.setTestContext(extensionContext);
KubeResourceManager.getInstance().deleteResources();
Class<?> testClass = extensionContext.getRequiredTestClass();
ResourceManager annotation = testClass.getAnnotation(ResourceManager.class);
if (annotation != null && annotation.cleanResources()) {
KubeResourceManager.setTestContext(extensionContext);
KubeResourceManager.getInstance().deleteResources();
}
}

/**
* Enables ResourceManagerCleanerExtension for afterEach callback
* @param extensionContext context
*/
@Override
public void afterEach(ExtensionContext extensionContext) {
KubeResourceManager.setTestContext(extensionContext);
KubeResourceManager.getInstance().deleteResources();
Class<?> testClass = extensionContext.getRequiredTestClass();
ResourceManager annotation = testClass.getAnnotation(ResourceManager.class);
if (annotation != null && annotation.cleanResources()) {
KubeResourceManager.setTestContext(extensionContext);
KubeResourceManager.getInstance().deleteResources();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright Skodjob authors.
* License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html).
*/
package io.skodjob.testframe.test.integration;

import io.fabric8.kubernetes.api.model.NamespaceBuilder;
import io.skodjob.testframe.annotations.ResourceManager;
import io.skodjob.testframe.annotations.TestVisualSeparator;
import io.skodjob.testframe.clients.KubeClusterException;
import io.skodjob.testframe.resources.KubeResourceManager;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

@ResourceManager
@TestVisualSeparator
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class KubeResourceManagerCleanerIT {

@BeforeAll
void setupAll() {
KubeResourceManager.getInstance().createResourceWithWait(
new NamespaceBuilder().withNewMetadata().withName("test").endMetadata().build());
}

@BeforeEach
void setupEach() {
KubeResourceManager.getInstance().createResourceWithWait(
new NamespaceBuilder().withNewMetadata().withName("test2").endMetadata().build());
}

@AfterAll
void afterAll() {
assertNull(KubeResourceManager.getKubeClient().getClient().namespaces().withName("test2").get());
assertNull(KubeResourceManager.getKubeClient().getClient().namespaces().withName("test3").get());
}

@Test
void createResource() {
KubeResourceManager.getInstance().createResourceWithWait(
new NamespaceBuilder().withNewMetadata().withName("test3").endMetadata().build());
}

@Test
void testKubeClientNamespacesExists() {
assertNotNull(KubeResourceManager.getKubeClient().getClient().namespaces().withName("test").get());
assertNotNull(KubeResourceManager.getKubeClient().getClient().namespaces().withName("test2").get());
assertNull(KubeResourceManager.getKubeClient().getClient().namespaces().withName("test3").get());
}

@Test
void testKubeCmdClientNamespacesExists() {
assertNotNull(KubeResourceManager.getKubeCmdClient().get("namespace", "test"));
assertNotNull(KubeResourceManager.getKubeCmdClient().get("namespace", "test2"));
assertThrows(KubeClusterException.class, () -> KubeResourceManager.getKubeCmdClient().get("namespace", "test3"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,58 +7,37 @@
import io.fabric8.kubernetes.api.model.NamespaceBuilder;
import io.skodjob.testframe.annotations.ResourceManager;
import io.skodjob.testframe.annotations.TestVisualSeparator;
import io.skodjob.testframe.clients.KubeClusterException;
import io.skodjob.testframe.resources.KubeResourceManager;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

@ResourceManager
@ResourceManager(cleanResources = false)
@TestVisualSeparator
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class KubeResourceManagerIT {

@BeforeAll
void setupAll() {
KubeResourceManager.getInstance().createResourceWithWait(
new NamespaceBuilder().withNewMetadata().withName("test").endMetadata().build());
}

@BeforeEach
void setupEach() {
KubeResourceManager.getInstance().createResourceWithWait(
new NamespaceBuilder().withNewMetadata().withName("test2").endMetadata().build());
new NamespaceBuilder().withNewMetadata().withName("test").endMetadata().build());
}

@AfterAll
void afterAll() {
assertNull(KubeResourceManager.getKubeClient().getClient().namespaces().withName("test2").get());
assertNull(KubeResourceManager.getKubeClient().getClient().namespaces().withName("test3").get());
@AfterEach
void afterEach() {
assertNotNull(KubeResourceManager.getKubeClient().getClient().namespaces().withName("test").get());
assertNotNull(KubeResourceManager.getKubeClient().getClient().namespaces().withName("test2").get());
KubeResourceManager.getInstance().deleteResources();
kornys marked this conversation as resolved.
Show resolved Hide resolved
}

@Test
void createResource() {
KubeResourceManager.getInstance().createResourceWithWait(
new NamespaceBuilder().withNewMetadata().withName("test3").endMetadata().build());
}

@Test
void testKubeClientNamespacesExists() {
new NamespaceBuilder().withNewMetadata().withName("test2").endMetadata().build());
assertNotNull(KubeResourceManager.getKubeClient().getClient().namespaces().withName("test").get());
assertNotNull(KubeResourceManager.getKubeClient().getClient().namespaces().withName("test2").get());
assertNull(KubeResourceManager.getKubeClient().getClient().namespaces().withName("test3").get());
}

@Test
void testKubeCmdClientNamespacesExists() {
assertNotNull(KubeResourceManager.getKubeCmdClient().get("namespace", "test"));
assertNotNull(KubeResourceManager.getKubeCmdClient().get("namespace", "test2"));
assertThrows(KubeClusterException.class, () -> KubeResourceManager.getKubeCmdClient().get("namespace", "test3"));
}
}
Loading