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..a9ba33875 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 @@ -5,95 +5,39 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.Paths; import jakarta.inject.Inject; -import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.Assertions; +import io.quarkus.test.bootstrap.DefaultService; import io.quarkus.test.bootstrap.inject.OpenShiftClient; -import io.quarkus.test.utils.Command; +import io.quarkus.test.bootstrap.service.OperatorOpenShiftInfinispan; 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 CLIENT_CERT_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); - } + @OperatorOpenShiftInfinispan(clientCertSecret = CLIENT_CERT_SECRET, clusterConfig = CLUSTER_CONFIG, clusterConfigMap = CLUSTER_CONFIGMAP, connectSecret = CONNECT_SECRET, tlsSecret = TLS_SECRET) + static DefaultService dataGridInfinispan = new DefaultService(); - protected static void adjustYml(String yamlFile, String originString, String newString) { + protected static void adjustYml(Path yamlFile, String originString, String newString) { try { - Path yamlPath = Paths.get(TARGET_RESOURCES + yamlFile); Charset charset = StandardCharsets.UTF_8; - String yamlContent = Files.readString(yamlPath, charset); + String yamlContent = Files.readString(yamlFile, charset); yamlContent = yamlContent.replace(originString, newString); - Files.writeString(yamlPath, yamlContent, charset); + Files.writeString(yamlFile, yamlContent, charset); } catch (IOException ex) { 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..877738e8f 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,9 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import java.io.IOException; +import java.nio.file.Path; +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 +29,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 +144,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 +174,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 +302,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 +407,30 @@ 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); + Path clusterConfigPath = Path.of(System.getProperty("java.io.tmpdir"), dataGridInfinispan.getDisplayName(), + Paths.get(CLUSTER_CONFIG).getFileName().toString()); + adjustYml(clusterConfigPath, "replicas: 1", "replicas: 0"); + ocClient.applyInProject(clusterConfigPath, 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); + Path clusterConfigPath = Path.of(System.getProperty("java.io.tmpdir"), dataGridInfinispan.getDisplayName(), + Paths.get(CLUSTER_CONFIG).getFileName().toString()); + adjustYml(clusterConfigPath, "replicas: 0", "replicas: 1"); + ocClient.applyInProject(clusterConfigPath, 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