Skip to content

Commit

Permalink
correct behaviour in waiting for self custom target to be defined
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewazores committed Nov 7, 2023
1 parent 385bd3b commit fa47196
Showing 1 changed file with 38 additions and 26 deletions.
64 changes: 38 additions & 26 deletions src/test/java/itest/bases/StandardSelfTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
import io.vertx.ext.web.codec.BodyCodec;
import io.vertx.ext.web.handler.HttpException;
import itest.util.Utils;
import jakarta.ws.rs.NotFoundException;
import jakarta.ws.rs.core.HttpHeaders;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
Expand All @@ -64,7 +63,7 @@ public abstract class StandardSelfTest {
public static final ObjectMapper mapper = new ObjectMapper();
public static final int REQUEST_TIMEOUT_SECONDS = 30;
public static final WebClient webClient = Utils.getWebClient();
public static String selfCustomTargetLocation;
public static volatile String selfCustomTargetLocation;

@BeforeAll
public static void waitForDiscovery() {
Expand Down Expand Up @@ -92,9 +91,7 @@ public static void deleteSelfCustomTarget() {
logger.infov(
"DELETE {0} -> HTTP {1} {2}: [{3}]",
path, resp.statusCode(), resp.statusMessage(), resp.headers());
if (HttpStatusCodeIdentifier.isSuccessCode(resp.statusCode())) {
selfCustomTargetLocation = resp.headers().get(HttpHeaders.LOCATION);
}
selfCustomTargetLocation = null;
});
}

Expand Down Expand Up @@ -149,7 +146,11 @@ public static void waitForDiscovery(int otherTargetsCount) {
}

private static void tryDefineSelfCustomTarget() {
if (StringUtils.isNotBlank(selfCustomTargetLocation)) {
return;
}
logger.info("Trying to define self-referential custom target...");
CompletableFuture<String> future = new CompletableFuture<>();
try {
JsonObject self =
new JsonObject(Map.of("connectUrl", SELF_JMX_URL, "alias", SELFTEST_ALIAS));
Expand All @@ -162,57 +163,62 @@ private static void tryDefineSelfCustomTarget() {
ar -> {
if (ar.failed()) {
logger.error(ar.cause());
future.completeExceptionally(ar.cause());
return;
}
HttpResponse<Buffer> resp = ar.result();
logger.infov(
"POST /api/v2/targets -> HTTP {0} {1}: [{2}]",
resp.statusCode(), resp.statusMessage(), resp.headers());
if (HttpStatusCodeIdentifier.isSuccessCode(resp.statusCode())) {
selfCustomTargetLocation =
resp.headers().get(HttpHeaders.LOCATION);
future.complete(resp.headers().get(HttpHeaders.LOCATION));
} else {
future.completeExceptionally(
new IllegalStateException(
Integer.toString(resp.statusCode())));
}
});
selfCustomTargetLocation = future.get(REQUEST_TIMEOUT_SECONDS, TimeUnit.SECONDS);
} catch (Exception e) {
logger.warn(e);
}
}

public static String getSelfReferenceConnectUrl() {
waitForDiscovery();
tryDefineSelfCustomTarget();
CompletableFuture<JsonObject> future = new CompletableFuture<>();
WORKER.submit(
() -> {
String path = URI.create(selfCustomTargetLocation).getPath();
webClient
.get("/api/v3/targets")
.get(path)
.basicAuthentication("user", "pass")
.as(BodyCodec.jsonArray())
.as(BodyCodec.jsonObject())
.timeout(5000)
.send(
ar -> {
if (ar.failed()) {
logger.error(ar.cause());
future.completeExceptionally(ar.cause());
return;
}
HttpResponse<JsonArray> resp = ar.result();
HttpResponse<JsonObject> resp = ar.result();
JsonObject body = resp.body();
logger.infov(
"GET /api/v3/targets -> HTTP {0} {1}: [{2}]",
"GET {0} -> HTTP {1} {2}: [{3}] = {4}",
path,
resp.statusCode(),
resp.statusMessage(),
resp.headers());
JsonArray arr = resp.body();
boolean found = false;
for (int i = 0; i < arr.size(); i++) {
JsonObject obj = arr.getJsonObject(i);
if (SELFTEST_ALIAS.equals(obj.getString("alias"))) {
future.complete(obj);
found = true;
break;
}
}
if (!found) {
future.completeExceptionally(new NotFoundException());
resp.headers(),
body);
if (!HttpStatusCodeIdentifier.isSuccessCode(
resp.statusCode())) {
future.completeExceptionally(
new IllegalStateException(
Integer.toString(resp.statusCode())));
return;
}
future.complete(body);
});
});
try {
Expand Down Expand Up @@ -254,6 +260,10 @@ public static CompletableFuture<JsonObject> expectNotification(
ws.close();
}
})
// FIXME in the cryostat3 itests we DO use auth. The message below is
// copy-pasted from the old codebase, however cryostat3 does not yet
// perform authentication when websocket clients connect.

// just to initialize the connection - Cryostat expects
// clients to send a message after the connection opens
// to authenticate themselves, but in itests we don't
Expand Down Expand Up @@ -337,7 +347,9 @@ private static CompletableFuture<Path> fireDownloadRequest(
resp.statusCode(),
resp.statusMessage(),
resp.headers());
if (resp.statusCode() != 200) {
if (!(HttpStatusCodeIdentifier.isSuccessCode(resp.statusCode())
|| HttpStatusCodeIdentifier.isRedirectCode(
resp.statusCode()))) {
future.completeExceptionally(
new Exception(String.format("HTTP %d", resp.statusCode())));
return;
Expand Down

0 comments on commit fa47196

Please sign in to comment.