From 8e7061dc00982e27d5ef5fc03ac768cd06487ecb Mon Sep 17 00:00:00 2001 From: Atif Ali Date: Tue, 20 Feb 2024 20:27:16 -0500 Subject: [PATCH] test --- src/test/java/itest/CustomTargetsIT.java | 760 +++++++++++------------ 1 file changed, 369 insertions(+), 391 deletions(-) diff --git a/src/test/java/itest/CustomTargetsIT.java b/src/test/java/itest/CustomTargetsIT.java index 8da9e86775..132a9ff976 100644 --- a/src/test/java/itest/CustomTargetsIT.java +++ b/src/test/java/itest/CustomTargetsIT.java @@ -49,400 +49,378 @@ @TestMethodOrder(OrderAnnotation.class) public class CustomTargetsIT extends StandardSelfTest { - private final ExecutorService worker = ForkJoinPool.commonPool(); - static final Map NULL_RESULT = new HashMap<>(); - private String itestJvmId; - private static StoredCredential storedCredential; - - static { - NULL_RESULT.put("result", null); - } - - @BeforeEach - void setup() throws InterruptedException, ExecutionException, TimeoutException { - itestJvmId = JvmIdWebRequest.jvmIdRequest( - "service:jmx:rmi:///jndi/rmi://cryostat-itests:9091/jmxrmi"); + private final ExecutorService worker = ForkJoinPool.commonPool(); + static final Map NULL_RESULT = new HashMap<>(); + private String itestJvmId; + private static StoredCredential storedCredential; + + static { + NULL_RESULT.put("result", null); + } + + @BeforeEach + void setup() throws InterruptedException, ExecutionException, TimeoutException { + itestJvmId = + JvmIdWebRequest.jvmIdRequest( + "service:jmx:rmi:///jndi/rmi://cryostat-itests:9091/jmxrmi"); + } + + @AfterAll + static void cleanup() throws Exception { + // Delete credentials to clean up + CompletableFuture deleteResponse = new CompletableFuture<>(); + webClient + .delete("/api/v2.2/credentials/" + storedCredential.id) + .send( + ar -> { + if (assertRequestStatus(ar, deleteResponse)) { + deleteResponse.complete(ar.result().bodyAsJsonObject()); + } + }); + + JsonObject expectedDeleteResponse = + new JsonObject( + Map.of( + "meta", + Map.of("type", HttpMimeType.JSON.mime(), "status", "OK"), + "data", + NULL_RESULT)); + try { + MatcherAssert.assertThat( + deleteResponse.get(REQUEST_TIMEOUT_SECONDS, TimeUnit.SECONDS), + Matchers.equalTo(expectedDeleteResponse)); + } catch (Exception e) { + throw new ITestCleanupFailedException( + String.format("Failed to clean up credential with ID %d", storedCredential.id), + e); } - - @AfterAll - static void cleanup() throws Exception { - // Delete credentials to clean up - CompletableFuture deleteResponse = new CompletableFuture<>(); - webClient - .delete("/api/v2.2/credentials/" + storedCredential.id) - .send( - ar -> { - if (assertRequestStatus(ar, deleteResponse)) { - deleteResponse.complete(ar.result().bodyAsJsonObject()); - } - }); - - JsonObject expectedDeleteResponse = new JsonObject( + } + + @Test + @Order(1) + void shouldBeAbleToTestTargetConnection() throws InterruptedException, ExecutionException { + MultiMap form = MultiMap.caseInsensitiveMultiMap(); + form.add("connectUrl", "localhost:0"); + form.add("alias", "self"); + + CompletableFuture response = new CompletableFuture<>(); + webClient + .post("/api/v2/targets?dryrun=true") + .sendForm( + form, + ar -> { + assertRequestStatus(ar, response); + // Assert 202 since localhost:0 jvm already exists + MatcherAssert.assertThat( + ar.result().statusCode(), Matchers.equalTo(202)); + response.complete(ar.result().bodyAsJsonObject()); + }); + String expectedConnectUrl = "service:jmx:rmi:///jndi/rmi://localhost:0/jmxrmi"; + JsonObject body = response.get().getJsonObject("data").getJsonObject("result"); + MatcherAssert.assertThat( + body.getString("connectUrl"), Matchers.equalTo(expectedConnectUrl)); + MatcherAssert.assertThat(body.getString("alias"), Matchers.equalTo("self")); + } + + @Test + @Order(2) + void targetShouldNotAppearInListing() throws InterruptedException, ExecutionException { + CompletableFuture response = new CompletableFuture<>(); + webClient + .get("/api/v1/targets") + .send( + ar -> { + assertRequestStatus(ar, response); + response.complete(ar.result().bodyAsJsonArray()); + }); + JsonArray body = response.get(); + MatcherAssert.assertThat(body, Matchers.notNullValue()); + MatcherAssert.assertThat(body.size(), Matchers.equalTo(1)); + + JsonObject selfJdp = + new JsonObject( + Map.of( + "jvmId", + itestJvmId, + "alias", + "io.cryostat.Cryostat", + "connectUrl", + "service:jmx:rmi:///jndi/rmi://cryostat-itests:9091/jmxrmi", + "labels", + Map.of(), + "annotations", Map.of( - "meta", - Map.of("type", HttpMimeType.JSON.mime(), "status", "OK"), - "data", - NULL_RESULT)); - try { - MatcherAssert.assertThat( - deleteResponse.get(REQUEST_TIMEOUT_SECONDS, TimeUnit.SECONDS), - Matchers.equalTo(expectedDeleteResponse)); - } catch (Exception e) { - throw new ITestCleanupFailedException( - String.format("Failed to clean up credential with ID %d", storedCredential.id), - e); - } - } - - @Test - @Order(1) - void shouldBeAbleToTestTargetConnection() throws InterruptedException, ExecutionException { - MultiMap form = MultiMap.caseInsensitiveMultiMap(); - form.add("connectUrl", "localhost:0"); - form.add("alias", "self"); - - CompletableFuture response = new CompletableFuture<>(); - webClient - .post("/api/v2/targets?dryrun=true") - .sendForm( - form, - ar -> { - assertRequestStatus(ar, response); - // Assert 202 since localhost:0 jvm already exists - MatcherAssert.assertThat( - ar.result().statusCode(), - Matchers.equalTo(202)); - response.complete(ar.result().bodyAsJsonObject()); - }); - String expectedConnectUrl = "service:jmx:rmi:///jndi/rmi://localhost:0/jmxrmi"; - JsonObject body = response.get().getJsonObject("data").getJsonObject("result"); - MatcherAssert.assertThat( - body.getString("connectUrl"), Matchers.equalTo(expectedConnectUrl)); - MatcherAssert.assertThat(body.getString("alias"), Matchers.equalTo("self")); - } - - @Test - @Order(2) - void targetShouldNotAppearInListing() throws InterruptedException, ExecutionException { - CompletableFuture response = new CompletableFuture<>(); - webClient - .get("/api/v1/targets") - .send( - ar -> { - assertRequestStatus(ar, response); - response.complete(ar.result().bodyAsJsonArray()); - }); - JsonArray body = response.get(); - MatcherAssert.assertThat(body, Matchers.notNullValue()); - MatcherAssert.assertThat(body.size(), Matchers.equalTo(1)); - - JsonObject selfJdp = new JsonObject( - Map.of( - "jvmId", - itestJvmId, - "alias", - "io.cryostat.Cryostat", - "connectUrl", - "service:jmx:rmi:///jndi/rmi://cryostat-itests:9091/jmxrmi", - "labels", - Map.of(), - "annotations", - Map.of( - "cryostat", - Map.of( - "REALM", - "JDP", - "HOST", - "cryostat-itests", - "PORT", - "9091", - "JAVA_MAIN", - "io.cryostat.Cryostat"), - "platform", - Map.of()))); - MatcherAssert.assertThat(body, Matchers.contains(selfJdp)); - } - - @Test - @Order(3) - void shouldBeAbleToDefineTarget() - throws TimeoutException, ExecutionException, InterruptedException { - MultiMap form = MultiMap.caseInsensitiveMultiMap(); - form.add("connectUrl", "localhost:0"); - form.add("alias", "self"); - form.add("username", "username"); - form.add("password", "password"); - - CountDownLatch latch = new CountDownLatch(3); - - Future resultFuture1 = worker.submit( - () -> { - try { - return expectNotification("CredentialsStored", 15, TimeUnit.SECONDS) - .get(); - } catch (Exception e) { - throw new RuntimeException(e); - } finally { - latch.countDown(); - } - }); - - Future resultFuture2 = worker.submit( - () -> { - try { - return expectNotification( - "TargetJvmDiscovery", 15, TimeUnit.SECONDS) - .get(); - } catch (Exception e) { - throw new RuntimeException(e); - } finally { - latch.countDown(); - } - }); - - Thread.sleep(5000); // Sleep to setup notification listening before query resolves - - CompletableFuture response = new CompletableFuture<>(); - webClient - .post("/api/v2/targets?storeCredentials=true") - .sendForm( - form, - ar -> { - assertRequestStatus(ar, response); - response.complete(ar.result().bodyAsJsonObject()); - latch.countDown(); - }); - latch.await(30, TimeUnit.SECONDS); - - String expectedConnectUrl = "service:jmx:rmi:///jndi/rmi://localhost:0/jmxrmi"; - - JsonObject body = response.get().getJsonObject("data").getJsonObject("result"); - MatcherAssert.assertThat( - body.getString("connectUrl"), Matchers.equalTo(expectedConnectUrl)); - MatcherAssert.assertThat(body.getString("alias"), Matchers.equalTo("self")); - - JsonObject result1 = resultFuture1.get(); - - JsonObject message = result1.getJsonObject("message"); - - storedCredential = new StoredCredential( - message.getInteger("id"), - message.getString("matchExpression"), - message.getInteger("numMatchingTargets")); - - MatcherAssert.assertThat(storedCredential.id, Matchers.any(Integer.class)); - MatcherAssert.assertThat( - storedCredential.matchExpression, - Matchers.equalTo("target.connectUrl == \"" + expectedConnectUrl + "\"")); - MatcherAssert.assertThat( - storedCredential.numMatchingTargets, Matchers.equalTo(Integer.valueOf(1))); - - JsonObject result2 = resultFuture2.get(); - JsonObject event = result2.getJsonObject("message").getJsonObject("event"); - MatcherAssert.assertThat(event.getString("kind"), Matchers.equalTo("FOUND")); - MatcherAssert.assertThat( - event.getJsonObject("serviceRef").getString("connectUrl"), - Matchers.equalTo(expectedConnectUrl)); - MatcherAssert.assertThat( - event.getJsonObject("serviceRef").getString("alias"), Matchers.equalTo("self")); - } - - @Test - @Order(4) - void targetShouldAppearInListing() - throws ExecutionException, InterruptedException, TimeoutException { - CompletableFuture response = new CompletableFuture<>(); - webClient - .get("/api/v1/targets") - .send( - ar -> { - assertRequestStatus(ar, response); - response.complete(ar.result().bodyAsJsonArray()); - }); - JsonArray body = response.get(); - MatcherAssert.assertThat(body, Matchers.notNullValue()); - MatcherAssert.assertThat(body.size(), Matchers.equalTo(2)); - - JsonObject selfJdp = new JsonObject( + "cryostat", + Map.of( + "REALM", + "JDP", + "HOST", + "cryostat-itests", + "PORT", + "9091", + "JAVA_MAIN", + "io.cryostat.Cryostat"), + "platform", + Map.of()))); + MatcherAssert.assertThat(body, Matchers.contains(selfJdp)); + } + + @Test + @Order(3) + void shouldBeAbleToDefineTarget() + throws TimeoutException, ExecutionException, InterruptedException { + MultiMap form = MultiMap.caseInsensitiveMultiMap(); + form.add("connectUrl", "localhost:0"); + form.add("alias", "self"); + form.add("username", "username"); + form.add("password", "password"); + + CountDownLatch latch = new CountDownLatch(3); + + Future resultFuture1 = + worker.submit( + () -> { + try { + return expectNotification("CredentialsStored", 15, TimeUnit.SECONDS) + .get(); + } catch (Exception e) { + throw new RuntimeException(e); + } finally { + latch.countDown(); + } + }); + + Future resultFuture2 = + worker.submit( + () -> { + try { + return expectNotification( + "TargetJvmDiscovery", 15, TimeUnit.SECONDS) + .get(); + } catch (Exception e) { + throw new RuntimeException(e); + } finally { + latch.countDown(); + } + }); + + Thread.sleep(5000); // Sleep to setup notification listening before query resolves + + CompletableFuture response = new CompletableFuture<>(); + webClient + .post("/api/v2/targets?storeCredentials=true") + .sendForm( + form, + ar -> { + assertRequestStatus(ar, response); + response.complete(ar.result().bodyAsJsonObject()); + latch.countDown(); + }); + latch.await(30, TimeUnit.SECONDS); + + String expectedConnectUrl = "service:jmx:rmi:///jndi/rmi://localhost:0/jmxrmi"; + + JsonObject body = response.get().getJsonObject("data").getJsonObject("result"); + MatcherAssert.assertThat( + body.getString("connectUrl"), Matchers.equalTo(expectedConnectUrl)); + MatcherAssert.assertThat(body.getString("alias"), Matchers.equalTo("self")); + + JsonObject result1 = resultFuture1.get(); + + JsonObject message = result1.getJsonObject("message"); + + storedCredential = + new StoredCredential( + message.getInteger("id"), + message.getString("matchExpression"), + message.getInteger("numMatchingTargets")); + + MatcherAssert.assertThat(storedCredential.id, Matchers.any(Integer.class)); + MatcherAssert.assertThat( + storedCredential.matchExpression, + Matchers.equalTo("target.connectUrl == \"" + expectedConnectUrl + "\"")); + MatcherAssert.assertThat( + storedCredential.numMatchingTargets, Matchers.equalTo(Integer.valueOf(1))); + + JsonObject result2 = resultFuture2.get(); + JsonObject event = result2.getJsonObject("message").getJsonObject("event"); + MatcherAssert.assertThat(event.getString("kind"), Matchers.equalTo("FOUND")); + MatcherAssert.assertThat( + event.getJsonObject("serviceRef").getString("connectUrl"), + Matchers.equalTo(expectedConnectUrl)); + MatcherAssert.assertThat( + event.getJsonObject("serviceRef").getString("alias"), Matchers.equalTo("self")); + } + + @Test + @Order(4) + void targetShouldAppearInListing() + throws ExecutionException, InterruptedException, TimeoutException { + CompletableFuture response = new CompletableFuture<>(); + webClient + .get("/api/v1/targets") + .send( + ar -> { + assertRequestStatus(ar, response); + response.complete(ar.result().bodyAsJsonArray()); + }); + JsonArray body = response.get(); + MatcherAssert.assertThat(body, Matchers.notNullValue()); + MatcherAssert.assertThat(body.size(), Matchers.equalTo(2)); + + JsonObject selfJdp = + new JsonObject( + Map.of( + "jvmId", + itestJvmId, + "alias", + "io.cryostat.Cryostat", + "connectUrl", + "service:jmx:rmi:///jndi/rmi://cryostat-itests:9091/jmxrmi", + "labels", + Map.of(), + "annotations", Map.of( - "jvmId", - itestJvmId, - "alias", - "io.cryostat.Cryostat", - "connectUrl", - "service:jmx:rmi:///jndi/rmi://cryostat-itests:9091/jmxrmi", - "labels", - Map.of(), - "annotations", - Map.of( - "cryostat", - Map.of( - "REALM", - "JDP", - "HOST", - "cryostat-itests", - "PORT", - "9091", - "JAVA_MAIN", - "io.cryostat.Cryostat"), - "platform", - Map.of()))); - JsonObject selfCustom = new JsonObject( + "cryostat", + Map.of( + "REALM", + "JDP", + "HOST", + "cryostat-itests", + "PORT", + "9091", + "JAVA_MAIN", + "io.cryostat.Cryostat"), + "platform", + Map.of()))); + JsonObject selfCustom = + new JsonObject( + Map.of( + "jvmId", + itestJvmId, + "alias", + "self", + "connectUrl", + "service:jmx:rmi:///jndi/rmi://localhost:0/jmxrmi", + "labels", + Map.of(), + "annotations", Map.of( - "jvmId", - itestJvmId, - "alias", - "self", - "connectUrl", - "service:jmx:rmi:///jndi/rmi://localhost:0/jmxrmi", - "labels", - Map.of(), - "annotations", - Map.of( - "cryostat", - Map.of("REALM", "Custom Targets"), - "platform", - Map.of()))); - MatcherAssert.assertThat(body, Matchers.containsInAnyOrder(selfJdp, selfCustom)); - } - - @Test - @Order(5) - void shouldBeAbleToDeleteTarget() - throws TimeoutException, ExecutionException, InterruptedException { - CompletableFuture response3 = new CompletableFuture<>(); - webClient - .get("/api/v1/targets") - .send( - ar -> { - assertRequestStatus(ar, response3); - response3.complete(ar.result().bodyAsJsonArray()); - }); - JsonArray body3 = response3.get(); - System.out.println("+++body3 "+ body3.encodePrettily()); - System.out.println("+++ size3 "+ body3.size()); - MatcherAssert.assertThat(body3, Matchers.notNullValue()); - MatcherAssert.assertThat(body3.size(), Matchers.equalTo(2)); - - CountDownLatch latch = new CountDownLatch(2); - - worker.submit( - () -> { - try { - expectNotification("TargetJvmDiscovery", 5, TimeUnit.SECONDS) - .thenAcceptAsync( - notification -> { - JsonObject event = notification - .getJsonObject("message") - .getJsonObject("event"); - MatcherAssert.assertThat( - event.getString("kind"), - Matchers.equalTo( - "LOST")); - MatcherAssert.assertThat( - event.getJsonObject( - "serviceRef") - .getString("connectUrl"), - Matchers.equalTo( - "service:jmx:rmi:///jndi/rmi://localhost:0/jmxrmi")); - MatcherAssert.assertThat( - event.getJsonObject( - "serviceRef") - .getString("alias"), - Matchers.equalTo( - "self")); - latch.countDown(); - }) - .get(); - } catch (Exception e) { - throw new RuntimeException(e); - } - }); - CompletableFuture response1 = new CompletableFuture<>(); - webClient - .get("/api/v1/targets") - .send( - ar -> { - assertRequestStatus(ar, response1); - response1.complete(ar.result().bodyAsJsonArray()); - latch.countDown(); - }); - JsonArray body1 = response1.get(); - System.out.println("+++body1 "+ body1.encodePrettily()); - System.out.println("+++ size1 "+ body1.size()); - MatcherAssert.assertThat(body1, Matchers.notNullValue()); - MatcherAssert.assertThat(body1.size(), Matchers.equalTo(2)); - - String jmxServiceUrl = "service:jmx:rmi:///jndi/rmi://localhost:0/jmxrmi"; - String encodedUrl = URLEncoder.encode(jmxServiceUrl, StandardCharsets.UTF_8); - CompletableFuture response = new CompletableFuture<>(); - webClient - .delete("/api/v2/targets/" + encodedUrl) - .send( - ar -> { - assertRequestStatus(ar, response); - response.complete(null); - latch.countDown(); - }); - - latch.await(5, TimeUnit.SECONDS); - - CompletableFuture response2 = new CompletableFuture<>(); - webClient - .get("/api/v1/targets") - .send( - ar -> { - assertRequestStatus(ar, response2); - response1.complete(ar.result().bodyAsJsonArray()); - }); - JsonArray body2 = response2.get(); - System.out.println("+++body2 after delete "+ body2.encodePrettily()); - System.out.println("+++ size2 after delete "+ body2.size()); - MatcherAssert.assertThat(body2, Matchers.notNullValue()); - MatcherAssert.assertThat(body2.size(), Matchers.equalTo(1)); - } - - @Test - @Order(6) - void targetShouldNoLongerAppearInListing() throws ExecutionException, InterruptedException { - CompletableFuture response = new CompletableFuture<>(); - webClient - .get("/api/v1/targets") - .send( - ar -> { - assertRequestStatus(ar, response); - response.complete(ar.result().bodyAsJsonArray()); - }); - JsonArray body = response.get(); - MatcherAssert.assertThat(body, Matchers.notNullValue()); - System.out.println("+++ ouside delete size is: " + body.size()); - - MatcherAssert.assertThat(body.size(), Matchers.equalTo(1)); - - JsonObject selfJdp = new JsonObject( + "cryostat", + Map.of("REALM", "Custom Targets"), + "platform", + Map.of()))); + MatcherAssert.assertThat(body, Matchers.containsInAnyOrder(selfJdp, selfCustom)); + } + + @Test + @Order(5) + void shouldBeAbleToDeleteTarget() + throws TimeoutException, ExecutionException, InterruptedException { + CountDownLatch latch = new CountDownLatch(2); + + worker.submit( + () -> { + try { + expectNotification("TargetJvmDiscovery", 5, TimeUnit.SECONDS) + .thenAcceptAsync( + notification -> { + JsonObject event = + notification + .getJsonObject("message") + .getJsonObject("event"); + MatcherAssert.assertThat( + event.getString("kind"), + Matchers.equalTo("LOST")); + MatcherAssert.assertThat( + event.getJsonObject("serviceRef") + .getString("connectUrl"), + Matchers.equalTo( + "service:jmx:rmi:///jndi/rmi://localhost:0/jmxrmi")); + MatcherAssert.assertThat( + event.getJsonObject("serviceRef") + .getString("alias"), + Matchers.equalTo("self")); + latch.countDown(); + }) + .get(); + } catch (Exception e) { + throw new RuntimeException(e); + } + }); + + String jmxServiceUrl = "service:jmx:rmi:///jndi/rmi://localhost:0/jmxrmi"; + String encodedUrl = URLEncoder.encode(jmxServiceUrl, StandardCharsets.UTF_8); + System.out.println("+++url: " + encodedUrl); + CompletableFuture response = new CompletableFuture<>(); + webClient + .delete("/api/v2/targets/" + encodedUrl) + .send( + ar -> { + assertRequestStatus(ar, response); + response.complete(null); + latch.countDown(); + }); + + latch.await(5, TimeUnit.SECONDS); + + CompletableFuture response1 = new CompletableFuture<>(); + webClient + .get("/api/v1/targets") + .send( + ar -> { + assertRequestStatus(ar, response1); + response1.complete(ar.result().bodyAsJsonArray()); + }); + JsonArray body1 = response1.get(); + System.out.println("+++body1 after delete " + body1.encodePrettily()); + System.out.println("+++ size1 after delete " + body1.size()); + MatcherAssert.assertThat(body1, Matchers.notNullValue()); + MatcherAssert.assertThat(body1.size(), Matchers.equalTo(1)); + } + + @Test + @Order(6) + void targetShouldNoLongerAppearInListing() throws ExecutionException, InterruptedException { + CompletableFuture response = new CompletableFuture<>(); + webClient + .get("/api/v1/targets") + .send( + ar -> { + assertRequestStatus(ar, response); + response.complete(ar.result().bodyAsJsonArray()); + }); + JsonArray body = response.get(); + MatcherAssert.assertThat(body, Matchers.notNullValue()); + System.out.println("+++ outside delete size is: " + body.size()); + + MatcherAssert.assertThat(body.size(), Matchers.equalTo(1)); + + JsonObject selfJdp = + new JsonObject( + Map.of( + "jvmId", + itestJvmId, + "alias", + "io.cryostat.Cryostat", + "connectUrl", + "service:jmx:rmi:///jndi/rmi://cryostat-itests:9091/jmxrmi", + "labels", + Map.of(), + "annotations", Map.of( - "jvmId", - itestJvmId, - "alias", - "io.cryostat.Cryostat", - "connectUrl", - "service:jmx:rmi:///jndi/rmi://cryostat-itests:9091/jmxrmi", - "labels", - Map.of(), - "annotations", - Map.of( - "cryostat", - Map.of( - "REALM", - "JDP", - "HOST", - "cryostat-itests", - "PORT", - "9091", - "JAVA_MAIN", - "io.cryostat.Cryostat"), - "platform", - Map.of()))); - MatcherAssert.assertThat(body, Matchers.contains(selfJdp)); - } + "cryostat", + Map.of( + "REALM", + "JDP", + "HOST", + "cryostat-itests", + "PORT", + "9091", + "JAVA_MAIN", + "io.cryostat.Cryostat"), + "platform", + Map.of()))); + MatcherAssert.assertThat(body, Matchers.contains(selfJdp)); + } }