From 9334492d457397a9b5ba06e0fb4cd413328252fc Mon Sep 17 00:00:00 2001 From: Jim Ma Date: Fri, 8 Sep 2023 09:24:25 +0800 Subject: [PATCH] [JBWS-4383]:Improve the common utility to check the WFLY readiness in cloud test --- .../test/ws/jaxws/k8s/EndpointTestCase.java | 9 +-- .../ws/cloud/test/JBossWSKubernetesTest.java | 80 +++++++++++-------- .../ws/cloud/test/JBossWSServerContainer.java | 7 -- modules/testsuite/cloud-tests/pom.xml | 3 - 4 files changed, 48 insertions(+), 51 deletions(-) delete mode 100644 modules/testsuite/cloud-tests/k8s/common/src/main/java/org/jboss/ws/cloud/test/JBossWSServerContainer.java diff --git a/modules/testsuite/cloud-tests/k8s/basic/src/test/java/org/jboss/test/ws/jaxws/k8s/EndpointTestCase.java b/modules/testsuite/cloud-tests/k8s/basic/src/test/java/org/jboss/test/ws/jaxws/k8s/EndpointTestCase.java index c5ad0dc84..fdd02afb6 100644 --- a/modules/testsuite/cloud-tests/k8s/basic/src/test/java/org/jboss/test/ws/jaxws/k8s/EndpointTestCase.java +++ b/modules/testsuite/cloud-tests/k8s/basic/src/test/java/org/jboss/test/ws/jaxws/k8s/EndpointTestCase.java @@ -43,7 +43,7 @@ ) public class EndpointTestCase extends JBossWSKubernetesTest { - private static final String APP_NAME = "jbossws-cxf-k8s-basic"; + private final String APP_NAME = "jbossws-cxf-k8s-basic"; @Test public void checkWSEndpoint(@InjectKubeClient KubernetesClient kubeClient) throws Exception { List lst = kubeClient.pods().withLabel("app.kubernetes.io/name", APP_NAME).list().getItems(); @@ -66,11 +66,4 @@ private Endpoint initPort(URL baseUrl) throws Exception { Endpoint proxy = service.getPort(Endpoint.class); return proxy; } - /** - * Get the WFLY container name, this container name will be used to check the WFLY readiness. - * @return the WFLY container name, ${project.artifactId} will be the default one. - */ - public String getContainerName() { - return APP_NAME; - } } \ No newline at end of file diff --git a/modules/testsuite/cloud-tests/k8s/common/src/main/java/org/jboss/ws/cloud/test/JBossWSKubernetesTest.java b/modules/testsuite/cloud-tests/k8s/common/src/main/java/org/jboss/ws/cloud/test/JBossWSKubernetesTest.java index 872ac8474..936d7dcd3 100644 --- a/modules/testsuite/cloud-tests/k8s/common/src/main/java/org/jboss/ws/cloud/test/JBossWSKubernetesTest.java +++ b/modules/testsuite/cloud-tests/k8s/common/src/main/java/org/jboss/ws/cloud/test/JBossWSKubernetesTest.java @@ -18,6 +18,8 @@ */ package org.jboss.ws.cloud.test; +import io.fabric8.kubernetes.api.model.ServicePort; +import io.fabric8.kubernetes.api.model.ServiceSpec; import io.fabric8.kubernetes.client.KubernetesClient; import io.fabric8.kubernetes.client.LocalPortForward; import io.restassured.RestAssured; @@ -25,52 +27,64 @@ import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; +import java.util.concurrent.atomic.AtomicBoolean; import org.junit.jupiter.api.BeforeEach; import static org.junit.jupiter.api.Assertions.assertTrue; -public class JBossWSKubernetesTest implements JBossWSServerContainer { +public class JBossWSKubernetesTest { + @InjectKubeClient private KubernetesClient jbossWSKubernetesTestKubeClient; @BeforeEach public void checkServerReady() { - waitWFLYReady(this.jbossWSKubernetesTestKubeClient, this.getContainerName(), 60000); + waitWFLYReady(this.jbossWSKubernetesTestKubeClient, 30000); } - public static boolean waitWFLYReady(KubernetesClient k8sClient, String containerName, long timeout) { - LocalPortForward p = k8sClient.services().withName(containerName).portForward(9990); - assertTrue(p.isAlive()); - URL url = null; - try { - url = new URL("http://localhost:" + p.getLocalPort() + "/health/ready"); - } catch (MalformedURLException e) { - // - } - long startTime = System.currentTimeMillis(); - while (System.currentTimeMillis() - startTime < timeout) { + public static boolean waitWFLYReady(KubernetesClient k8sClient, long timeout) { + AtomicBoolean ready = new AtomicBoolean(false); + k8sClient.services().resources().filter(serviceResource -> + serviceResource.get().getSpec().getPorts().stream().anyMatch( + servicePort -> servicePort.getTargetPort().getIntVal() == 9990) + ).forEach(serviceResource -> { + ServicePort servicePort = serviceResource.get().getSpec().getPorts().stream().filter(port -> port.getTargetPort().getIntVal() == 9990).findFirst().get(); + LocalPortForward p = serviceResource.portForward(servicePort.getPort()); + assertTrue(p.isAlive()); + URL url = null; try { - //This is a workaround for restassured print out some warning message, we try to openstream before check response with restassured - //INFO: I/O exception (org.apache.http.NoHttpResponseException) caught when processing request to {}->http://localhost:53380: - //The target server failed to respond - url.openStream(); - io.restassured.response.Response response = RestAssured.given().get(url); - if (response.getStatusCode() == 200) { - JsonPath jsonPath = response.jsonPath(); - String readyStatus = jsonPath.getString("status"); - if (readyStatus.equalsIgnoreCase("UP")) { - return true; + url = new URL("http://localhost:" + p.getLocalPort() + "/health/ready"); + } catch (MalformedURLException e) { + // + } + long startTime = System.currentTimeMillis(); + while (System.currentTimeMillis() - startTime < timeout) { + try { + //This is a workaround for restassured print out some warning message, we try to openstream before check response with restassured + //INFO: I/O exception (org.apache.http.NoHttpResponseException) caught when processing request to {}->http://localhost:53380: + //The target server failed to respond + url.openStream(); + io.restassured.response.Response response = RestAssured.given().get(url); + if (response.getStatusCode() == 200) { + JsonPath jsonPath = response.jsonPath(); + String readyStatus = jsonPath.getString("status"); + if (readyStatus.equalsIgnoreCase("UP")) { + ready.set(true); + return; + } } + } catch (IOException e) { + //the WFLY is not ready. + } + try { + Thread.sleep(timeout / 5); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + break; } - } catch (IOException e) { - //the WFLY is not ready. - } - try { - Thread.sleep(timeout/5); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - break; } - } - return false; + ready.set(false); + }); + //There is no WFLY instance running or the WFLY management port isn't exposed in service + return ready.get(); } } diff --git a/modules/testsuite/cloud-tests/k8s/common/src/main/java/org/jboss/ws/cloud/test/JBossWSServerContainer.java b/modules/testsuite/cloud-tests/k8s/common/src/main/java/org/jboss/ws/cloud/test/JBossWSServerContainer.java deleted file mode 100644 index 7a40276b7..000000000 --- a/modules/testsuite/cloud-tests/k8s/common/src/main/java/org/jboss/ws/cloud/test/JBossWSServerContainer.java +++ /dev/null @@ -1,7 +0,0 @@ -package org.jboss.ws.cloud.test; - -public interface JBossWSServerContainer { - default String getContainerName() { - return System.getProperty("project.artifactId"); - } -} diff --git a/modules/testsuite/cloud-tests/pom.xml b/modules/testsuite/cloud-tests/pom.xml index f78c12075..9357b8455 100644 --- a/modules/testsuite/cloud-tests/pom.xml +++ b/modules/testsuite/cloud-tests/pom.xml @@ -42,9 +42,6 @@ **/*Test.java **/*TestCase.java - - ${project.artifactId} -