From a6a797fdbb63c072d57deaf77fab3fb3699741b8 Mon Sep 17 00:00:00 2001 From: Jakub Jedlicka Date: Mon, 6 Jan 2025 12:05:00 +0100 Subject: [PATCH] Refactoring the infinispan-client to use DataGridInfinispanInstance This is due to reason when the rest service fail the datagrid resources were not cleaned up. --- infinispan-client/pom.xml | 5 ++ .../client/BaseOpenShiftInfinispanIT.java | 74 +++---------------- ...OperatorOpenShiftInfinispanCountersIT.java | 32 +++----- ...orOpenShiftInfinispanErrorSvcListener.java | 23 ------ .../OperatorOpenShiftInfinispanObjectsIT.java | 3 +- .../io.quarkus.test.bootstrap.ServiceListener | 1 - 6 files changed, 27 insertions(+), 111 deletions(-) delete mode 100644 infinispan-client/src/test/java/io/quarkus/ts/infinispan/client/OperatorOpenShiftInfinispanErrorSvcListener.java delete mode 100644 infinispan-client/src/test/resources/META-INF/services/io.quarkus.test.bootstrap.ServiceListener diff --git a/infinispan-client/pom.xml b/infinispan-client/pom.xml index 3ac989996..ddf8c4db2 100644 --- a/infinispan-client/pom.xml +++ b/infinispan-client/pom.xml @@ -31,6 +31,11 @@ io.quarkus quarkus-smallrye-health + + io.quarkus.qe + quarkus-test-service-infinispan + test + diff --git a/infinispan-client/src/test/java/io/quarkus/ts/infinispan/client/BaseOpenShiftInfinispanIT.java b/infinispan-client/src/test/java/io/quarkus/ts/infinispan/client/BaseOpenShiftInfinispanIT.java index a4d5fac0d..e98d17586 100644 --- a/infinispan-client/src/test/java/io/quarkus/ts/infinispan/client/BaseOpenShiftInfinispanIT.java +++ b/infinispan-client/src/test/java/io/quarkus/ts/infinispan/client/BaseOpenShiftInfinispanIT.java @@ -9,38 +9,30 @@ import jakarta.inject.Inject; -import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.Assertions; +import io.quarkus.test.bootstrap.DataGridInfinispanInstance; import io.quarkus.test.bootstrap.inject.OpenShiftClient; -import io.quarkus.test.utils.Command; +import io.quarkus.test.bootstrap.service.DataGridInfinispan; public abstract class BaseOpenShiftInfinispanIT { protected static final String TARGET_RESOURCES = "target/test-classes/"; - protected static final String CLUSTER_SECRET = "clientcert_secret.yaml"; - protected static final String CLUSTER_CONFIG = "infinispan_cluster_config.yaml"; - protected static final String CLUSTER_CONFIGMAP = "infinispan_cluster_configmap.yaml"; - protected static final String CONNECT_SECRET = "connect_secret.yaml"; - protected static final String TLS_SECRET = "tls_secret.yaml"; + protected static final String CLUSTER_SECRET = TARGET_RESOURCES + "clientcert_secret.yaml"; + protected static final String CLUSTER_CONFIG = TARGET_RESOURCES + "infinispan_cluster_config.yaml"; + protected static final String CLUSTER_CONFIGMAP = TARGET_RESOURCES + "infinispan_cluster_configmap.yaml"; + protected static final String CONNECT_SECRET = TARGET_RESOURCES + "connect_secret.yaml"; + protected static final String TLS_SECRET = TARGET_RESOURCES + "tls_secret.yaml"; protected static final String CLUSTER_NAMESPACE_NAME = "datagrid-cluster"; - protected static String NEW_CLUSTER_NAME = null; - - private static final String ORIGIN_CLUSTER_NAME = "totally-random-infinispan-cluster-name"; @Inject static OpenShiftClient ocClient; - @AfterAll - public static void deleteInfinispanCluster() { - deleteYaml(CLUSTER_CONFIGMAP); - deleteYaml(CLUSTER_CONFIG); - adjustYml(CLUSTER_CONFIG, NEW_CLUSTER_NAME, ORIGIN_CLUSTER_NAME); - adjustYml(CLUSTER_CONFIGMAP, NEW_CLUSTER_NAME, ORIGIN_CLUSTER_NAME); - } + @DataGridInfinispan(clusterSecret = CLUSTER_SECRET, clusterConfig = CLUSTER_CONFIG, clusterConfigMap = CLUSTER_CONFIGMAP, connectSecret = CONNECT_SECRET, tlsSecret = TLS_SECRET) + static DataGridInfinispanInstance dataGridInfinispan = new DataGridInfinispanInstance(); protected static void adjustYml(String yamlFile, String originString, String newString) { try { - Path yamlPath = Paths.get(TARGET_RESOURCES + yamlFile); + Path yamlPath = Paths.get(yamlFile); Charset charset = StandardCharsets.UTF_8; String yamlContent = Files.readString(yamlPath, charset); @@ -50,50 +42,4 @@ protected static void adjustYml(String yamlFile, String originString, String new Assertions.fail("Fail to adjust YAML file. Caused by: " + ex.getMessage()); } } - - /** - * Apply the YAML file. - */ - protected static void applyYaml(String yamlFile) { - try { - new Command("oc", "apply", "-f", Paths.get(TARGET_RESOURCES + yamlFile).toString()).runAndWait(); - } catch (Exception e) { - Assertions.fail("Failed to apply YAML file. Caused by: " + e.getMessage()); - } - } - - protected static void createInfinispanCluster() { - applyYaml(CLUSTER_SECRET); - applyYaml(CONNECT_SECRET); - applyYaml(TLS_SECRET); - - // there should be unique name for every created infinispan cluster to be able parallel runs - NEW_CLUSTER_NAME = ocClient.project() + "-infinispan-cluster"; - - // rename infinispan cluster and configmap - adjustYml(CLUSTER_CONFIG, ORIGIN_CLUSTER_NAME, NEW_CLUSTER_NAME); - applyYaml(CLUSTER_CONFIG); - adjustYml(CLUSTER_CONFIGMAP, ORIGIN_CLUSTER_NAME, NEW_CLUSTER_NAME); - applyYaml(CLUSTER_CONFIGMAP); - - try { - new Command("oc", "-n", CLUSTER_NAMESPACE_NAME, "wait", "--for", "condition=wellFormed", "--timeout=300s", - "infinispan/" + NEW_CLUSTER_NAME).runAndWait(); - } catch (Exception e) { - deleteInfinispanCluster(); - Assertions.fail("Fail to wait Infinispan resources to start. Caused by: " + e.getMessage()); - } - } - - /** - * - * Delete the YAML file. - */ - private static void deleteYaml(String yamlFile) { - try { - new Command("oc", "delete", "-f", Paths.get(TARGET_RESOURCES + yamlFile).toString()).runAndWait(); - } catch (Exception e) { - Assertions.fail("Failed to delete YAML file. Caused by: " + e.getMessage()); - } - } } diff --git a/infinispan-client/src/test/java/io/quarkus/ts/infinispan/client/OperatorOpenShiftInfinispanCountersIT.java b/infinispan-client/src/test/java/io/quarkus/ts/infinispan/client/OperatorOpenShiftInfinispanCountersIT.java index e5ba87bf3..018f852e4 100644 --- a/infinispan-client/src/test/java/io/quarkus/ts/infinispan/client/OperatorOpenShiftInfinispanCountersIT.java +++ b/infinispan-client/src/test/java/io/quarkus/ts/infinispan/client/OperatorOpenShiftInfinispanCountersIT.java @@ -5,8 +5,8 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import java.io.IOException; +import java.nio.file.Paths; import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicBoolean; import org.apache.http.HttpStatus; import org.hamcrest.Matchers; @@ -28,16 +28,8 @@ @TestMethodOrder(MethodOrderer.OrderAnnotation.class) public class OperatorOpenShiftInfinispanCountersIT extends BaseOpenShiftInfinispanIT { - private static final AtomicBoolean CLUSTER_CREATED = new AtomicBoolean(false); - @QuarkusApplication - static RestService one = new RestService() - .onPreStart(s -> { - // prevent attempting to create cluster on restart - if (CLUSTER_CREATED.compareAndSet(false, true)) { - createInfinispanCluster(); - } - }); + static RestService one = new RestService(); @QuarkusApplication static RestService two = new RestService(); @@ -151,8 +143,7 @@ public void testRestartInfinispanCluster() throws IOException, InterruptedExcept incrementCountersOnValue(one, "/first-counter/increment-counters", 10); - killInfinispanCluster(); - restartInfinispanCluster(); + dataGridInfinispan.restart(); // try to connect back to infinispan cluster and expect no content await().atMost(5, TimeUnit.MINUTES).untilAsserted(() -> { @@ -182,8 +173,7 @@ public void testIncrementAfterRestartInfinispanCluster() throws IOException, Int incrementCountersOnValue(one, "/first-counter/increment-counters", 10); - killInfinispanCluster(); - restartInfinispanCluster(); + dataGridInfinispan.restart(); // try to connect back to infinispan cluster and expect no content await().atMost(5, TimeUnit.MINUTES).untilAsserted(() -> { @@ -311,9 +301,7 @@ public void testMultipleClientDataAfterRestartInfinispanCluster() throws IOExcep assertEquals("Cache=1 Client=1", firstClientCounters); assertEquals("Cache=2 Client=1", secondClientCounters); - killInfinispanCluster(); - restartInfinispanCluster(); - + dataGridInfinispan.restart(); // wait for clients to be reconnected to infinispan cluster await().atMost(1, TimeUnit.MINUTES).untilAsserted(() -> { one.given() @@ -418,24 +406,26 @@ private void incrementCountersOnValue(RestService node, String url, int count) { } /** + * This is needed for `testInvokeOnFailedInfinispanCluster` instead of of restarting datagrid * Reduces the number of infinispan cluster replicas to 0 and wait for the shutdown condition. It is done by changing * the YAML file in the target/test-classes directory. */ private void killInfinispanCluster() throws IOException, InterruptedException { adjustYml(CLUSTER_CONFIG, "replicas: 1", "replicas: 0"); - applyYaml(CLUSTER_CONFIG); + ocClient.applyInProject(Paths.get(CLUSTER_CONFIG), CLUSTER_NAMESPACE_NAME); new Command("oc", "-n", CLUSTER_NAMESPACE_NAME, "wait", "--for", "condition=gracefulShutdown", "--timeout=300s", - "infinispan/" + NEW_CLUSTER_NAME).runAndWait(); + "infinispan/" + dataGridInfinispan.getDisplayName()).runAndWait(); } /** + * This is needed for `testInvokeOnFailedInfinispanCluster` instead of of restarting datagrid * The number of replicas is increased back to value 1 the same way as in "killInfinispanCluster()" method. The wait command * expects "wellFormed" condition in Infinispan cluster status. */ private void restartInfinispanCluster() throws IOException, InterruptedException { adjustYml(CLUSTER_CONFIG, "replicas: 0", "replicas: 1"); - applyYaml(CLUSTER_CONFIG); + ocClient.applyInProject(Paths.get(CLUSTER_CONFIG), CLUSTER_NAMESPACE_NAME); new Command("oc", "-n", CLUSTER_NAMESPACE_NAME, "wait", "--for", "condition=wellFormed", "--timeout=360s", - "infinispan/" + NEW_CLUSTER_NAME).runAndWait(); + "infinispan/" + dataGridInfinispan.getDisplayName()).runAndWait(); } } diff --git a/infinispan-client/src/test/java/io/quarkus/ts/infinispan/client/OperatorOpenShiftInfinispanErrorSvcListener.java b/infinispan-client/src/test/java/io/quarkus/ts/infinispan/client/OperatorOpenShiftInfinispanErrorSvcListener.java deleted file mode 100644 index f1082362b..000000000 --- a/infinispan-client/src/test/java/io/quarkus/ts/infinispan/client/OperatorOpenShiftInfinispanErrorSvcListener.java +++ /dev/null @@ -1,23 +0,0 @@ -package io.quarkus.ts.infinispan.client; - -import java.util.Set; - -import io.quarkus.test.bootstrap.ServiceContext; -import io.quarkus.test.bootstrap.ServiceListener; -import io.vertx.core.impl.ConcurrentHashSet; - -/** - * Makes sure Infinispan cluster is deleted even when there is error and {@link org.junit.jupiter.api.AfterAll} - * is not called. - */ -public class OperatorOpenShiftInfinispanErrorSvcListener implements ServiceListener { - - private static final Set TEST_CLASS_CACHE = new ConcurrentHashSet<>(); - - @Override - public void onServiceError(ServiceContext service, Throwable throwable) { - if (TEST_CLASS_CACHE.add(service.getScenarioContext().getRunningTestClassName())) { - BaseOpenShiftInfinispanIT.deleteInfinispanCluster(); - } - } -} diff --git a/infinispan-client/src/test/java/io/quarkus/ts/infinispan/client/OperatorOpenShiftInfinispanObjectsIT.java b/infinispan-client/src/test/java/io/quarkus/ts/infinispan/client/OperatorOpenShiftInfinispanObjectsIT.java index af17749ee..319291108 100644 --- a/infinispan-client/src/test/java/io/quarkus/ts/infinispan/client/OperatorOpenShiftInfinispanObjectsIT.java +++ b/infinispan-client/src/test/java/io/quarkus/ts/infinispan/client/OperatorOpenShiftInfinispanObjectsIT.java @@ -44,8 +44,7 @@ public class OperatorOpenShiftInfinispanObjectsIT extends BaseOpenShiftInfinispa new ShopItem("Item 5", 500, ShopItem.Type.MECHANICAL)); @QuarkusApplication - static RestService one = new RestService() - .onPreStart(s -> createInfinispanCluster()); + static RestService one = new RestService(); @AfterEach public void afterEach() { diff --git a/infinispan-client/src/test/resources/META-INF/services/io.quarkus.test.bootstrap.ServiceListener b/infinispan-client/src/test/resources/META-INF/services/io.quarkus.test.bootstrap.ServiceListener deleted file mode 100644 index 270cb2690..000000000 --- a/infinispan-client/src/test/resources/META-INF/services/io.quarkus.test.bootstrap.ServiceListener +++ /dev/null @@ -1 +0,0 @@ -io.quarkus.ts.infinispan.client.OperatorOpenShiftInfinispanErrorSvcListener