From 4406644359c37702851605cc38c6915aa870d20c Mon Sep 17 00:00:00 2001 From: David Kornel Date: Tue, 22 Oct 2024 08:32:21 +0200 Subject: [PATCH] Allow to switch ResoruceManager between async and sequential deletion mode Signed-off-by: David Kornel --- .../annotations/ResourceManager.java | 7 +++ .../ResourceManagerCleanerExtension.java | 2 +- .../resources/KubeResourceManager.java | 48 ++++++++++++++++--- .../KubeResourceManagerCleanerIT.java | 2 + .../integration/KubeResourceManagerIT.java | 2 +- 5 files changed, 53 insertions(+), 8 deletions(-) diff --git a/test-frame-common/src/main/java/io/skodjob/testframe/annotations/ResourceManager.java b/test-frame-common/src/main/java/io/skodjob/testframe/annotations/ResourceManager.java index 352ebfb..e7ce2ec 100644 --- a/test-frame-common/src/main/java/io/skodjob/testframe/annotations/ResourceManager.java +++ b/test-frame-common/src/main/java/io/skodjob/testframe/annotations/ResourceManager.java @@ -34,4 +34,11 @@ * @return enable/disable cleaner */ boolean cleanResources() default true; + + /** + * Enables async deletion of resources + * + * @return enable/disable async cleaning + */ + boolean asyncDeletion() default true; } diff --git a/test-frame-common/src/main/java/io/skodjob/testframe/listeners/ResourceManagerCleanerExtension.java b/test-frame-common/src/main/java/io/skodjob/testframe/listeners/ResourceManagerCleanerExtension.java index 3d587e3..bc588f6 100644 --- a/test-frame-common/src/main/java/io/skodjob/testframe/listeners/ResourceManagerCleanerExtension.java +++ b/test-frame-common/src/main/java/io/skodjob/testframe/listeners/ResourceManagerCleanerExtension.java @@ -51,7 +51,7 @@ public void afterEach(ExtensionContext extensionContext) { if (annotation.isPresent() && annotation.get().cleanResources()) { KubeResourceManager.setTestContext(extensionContext); - KubeResourceManager.getInstance().deleteResources(); + KubeResourceManager.getInstance().deleteResources(annotation.get().asyncDeletion()); } } } diff --git a/test-frame-common/src/main/java/io/skodjob/testframe/resources/KubeResourceManager.java b/test-frame-common/src/main/java/io/skodjob/testframe/resources/KubeResourceManager.java index c580649..74fad20 100644 --- a/test-frame-common/src/main/java/io/skodjob/testframe/resources/KubeResourceManager.java +++ b/test-frame-common/src/main/java/io/skodjob/testframe/resources/KubeResourceManager.java @@ -400,6 +400,18 @@ private void createOrUpdateResource(boolean async, */ @SafeVarargs public final void deleteResource(T... resources) { + deleteResource(true, resources); + } + + /** + * Deletes resources. + * + * @param async Enables async deletion + * @param resources The resources to delete. + * @param The type of the resources. + */ + @SafeVarargs + public final void deleteResource(boolean async, T... resources) { List> waitExecutors = new LinkedList<>(); for (T resource : resources) { ResourceType type = findResourceType(resource); @@ -407,16 +419,26 @@ public final void deleteResource(T... resources) { try { if (type == null) { client.getClient().resource(resource).delete(); - waitExecutors.add(CompletableFuture.runAsync(() -> + CompletableFuture c = CompletableFuture.runAsync(() -> assertTrue(waitResourceCondition(resource, ResourceCondition.deletion()), String.format("Timed out deleting %s/%s in %s", resource.getKind(), - resource.getMetadata().getName(), resource.getMetadata().getNamespace())))); + resource.getMetadata().getName(), resource.getMetadata().getNamespace()))); + if (async) { + waitExecutors.add(c); + } else { + CompletableFuture.allOf(c).join(); + } } else { type.delete(resource); - waitExecutors.add(CompletableFuture.runAsync(() -> + CompletableFuture c = CompletableFuture.runAsync(() -> assertTrue(waitResourceCondition(resource, ResourceCondition.deletion()), String.format("Timed out deleting %s/%s in %s", resource.getKind(), - resource.getMetadata().getName(), resource.getMetadata().getNamespace())))); + resource.getMetadata().getName(), resource.getMetadata().getNamespace()))); + if (async) { + waitExecutors.add(c); + } else { + CompletableFuture.allOf(c).join(); + } } } catch (Exception e) { if (resource.getMetadata().getNamespace() == null) { @@ -485,6 +507,15 @@ public final boolean waitResourceCondition(T resource, R * Deletes all stored resources. */ public void deleteResources() { + deleteResources(true); + } + + /** + * Deletes all stored resources. + * + * @param async sets async or sequential deletion + */ + public void deleteResources(boolean async) { LoggerUtils.logSeparator(); if (!STORED_RESOURCES.containsKey(getTestContext().getDisplayName()) || STORED_RESOURCES.get(getTestContext().getDisplayName()).isEmpty()) { @@ -505,13 +536,18 @@ public void deleteResources() { ResourceItem resourceItem = s.pop(); try { - waitExecutors.add(CompletableFuture.runAsync(() -> { + CompletableFuture c = CompletableFuture.runAsync(() -> { try { resourceItem.throwableRunner().run(); } catch (Exception e) { throw new RuntimeException(e); } - })); + }); + if (async) { + waitExecutors.add(c); + } else { + CompletableFuture.allOf(c).join(); + } } catch (Exception e) { LOGGER.error(e.getMessage(), e); } diff --git a/test-frame-test-examples/src/test/java/io/skodjob/testframe/test/integration/KubeResourceManagerCleanerIT.java b/test-frame-test-examples/src/test/java/io/skodjob/testframe/test/integration/KubeResourceManagerCleanerIT.java index dace78a..7a537ec 100644 --- a/test-frame-test-examples/src/test/java/io/skodjob/testframe/test/integration/KubeResourceManagerCleanerIT.java +++ b/test-frame-test-examples/src/test/java/io/skodjob/testframe/test/integration/KubeResourceManagerCleanerIT.java @@ -7,6 +7,7 @@ import io.fabric8.kubernetes.api.model.HasMetadata; import io.fabric8.kubernetes.api.model.Namespace; import io.fabric8.kubernetes.api.model.NamespaceBuilder; +import io.skodjob.testframe.annotations.ResourceManager; import io.skodjob.testframe.clients.KubeClusterException; import io.skodjob.testframe.resources.KubeResourceManager; import org.junit.jupiter.api.AfterAll; @@ -25,6 +26,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; @TestInstance(TestInstance.Lifecycle.PER_CLASS) +@ResourceManager(asyncDeletion = false) public final class KubeResourceManagerCleanerIT extends AbstractIT { @BeforeAll diff --git a/test-frame-test-examples/src/test/java/io/skodjob/testframe/test/integration/KubeResourceManagerIT.java b/test-frame-test-examples/src/test/java/io/skodjob/testframe/test/integration/KubeResourceManagerIT.java index 7cea037..cefd0e2 100644 --- a/test-frame-test-examples/src/test/java/io/skodjob/testframe/test/integration/KubeResourceManagerIT.java +++ b/test-frame-test-examples/src/test/java/io/skodjob/testframe/test/integration/KubeResourceManagerIT.java @@ -38,7 +38,7 @@ void createResource() { new NamespaceBuilder().withNewMetadata().withName("test2").endMetadata().build()); assertNotNull(KubeResourceManager.getKubeClient().getClient().namespaces().withName(nsName1).get()); assertNotNull(KubeResourceManager.getKubeClient().getClient().namespaces().withName(nsName2).get()); - KubeResourceManager.getInstance().deleteResource(ns1); + KubeResourceManager.getInstance().deleteResource(false, ns1); assertTrue(isDeleteHandlerCalled.get()); } }