Skip to content

Commit

Permalink
test(snapshot): handle deletion request redirects for cleanup (cryost…
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewazores authored Nov 27, 2023
1 parent 9eb8f62 commit 3aa393c
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 190 deletions.
292 changes: 102 additions & 190 deletions src/test/java/itest/SnapshotTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,12 @@
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.handler.HttpException;
import itest.bases.StandardSelfTest;
import itest.util.ITestCleanupFailedException;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;

@QuarkusTest
@TestMethodOrder(OrderAnnotation.class)
public class SnapshotTest extends StandardSelfTest {

static final String TEST_RECORDING_NAME = "someRecording";
Expand All @@ -54,7 +49,6 @@ String v2RequestUrl() {
}

@Test
@Order(1)
void testPostV1ShouldHandleEmptySnapshot() throws Exception {
// precondition, there should be no recordings before we start
CompletableFuture<JsonArray> preListRespFuture = new CompletableFuture<>();
Expand Down Expand Up @@ -101,7 +95,6 @@ void testPostV1ShouldHandleEmptySnapshot() throws Exception {
}

@Test
@Order(2)
void testPostV2ShouldHandleEmptySnapshot() throws Exception {
// precondition, there should be no recordings before we start
CompletableFuture<JsonArray> preListRespFuture = new CompletableFuture<>();
Expand Down Expand Up @@ -148,95 +141,56 @@ void testPostV2ShouldHandleEmptySnapshot() throws Exception {
}

@Test
@Order(3)
void testPostV1ShouldCreateSnapshot() throws Exception {
CompletableFuture<String> snapshotName = new CompletableFuture<>();

try {
// Create a recording
MultiMap form = MultiMap.caseInsensitiveMultiMap();
form.add("recordingName", TEST_RECORDING_NAME);
form.add("duration", "5");
form.add("events", "template=ALL");
webClient
.extensions()
.post(String.format("%s/recordings", v1RequestUrl()), true, form, 5);
// Create a recording
MultiMap form = MultiMap.caseInsensitiveMultiMap();
form.add("recordingName", TEST_RECORDING_NAME);
form.add("duration", "5");
form.add("events", "template=ALL");
webClient.extensions().post(String.format("%s/recordings", v1RequestUrl()), true, form, 5);

// Create a snapshot recording of all events at that time
webClient
.post(String.format("%s/snapshot", v1RequestUrl()))
.basicAuthentication("user", "pass")
.send(
ar -> {
if (assertRequestStatus(ar, snapshotName)) {
MatcherAssert.assertThat(
ar.result().statusCode(), Matchers.equalTo(200));
MatcherAssert.assertThat(
ar.result()
.getHeader(HttpHeaders.CONTENT_TYPE.toString()),
Matchers.equalTo("text/plain;charset=UTF-8"));
snapshotName.complete(ar.result().bodyAsString());
}
});

MatcherAssert.assertThat(
snapshotName.get(REQUEST_TIMEOUT_SECONDS, TimeUnit.SECONDS),
Matchers.matchesPattern(SNAPSHOT_NAME_PATTERN));

} finally {
// Clean up recording and snapshot
CompletableFuture<JsonObject> deleteRecordingResponse = new CompletableFuture<>();
webClient
.delete(String.format("%s/recordings/%s", v1RequestUrl(), TEST_RECORDING_NAME))
.basicAuthentication("user", "pass")
.send(
ar -> {
if (assertRequestStatus(ar, deleteRecordingResponse)) {
deleteRecordingResponse.complete(
ar.result().bodyAsJsonObject());
}
});

try {
deleteRecordingResponse.get(REQUEST_TIMEOUT_SECONDS, TimeUnit.SECONDS);
} catch (InterruptedException | ExecutionException e) {
throw new ITestCleanupFailedException(
String.format("Failed to delete target recording %s", TEST_RECORDING_NAME),
e);
}

CompletableFuture<JsonObject> deleteSnapshotResponse = new CompletableFuture<>();
// Create a snapshot recording of all events at that time
webClient
.post(String.format("%s/snapshot", v1RequestUrl()))
.basicAuthentication("user", "pass")
.send(
ar -> {
if (assertRequestStatus(ar, snapshotName)) {
MatcherAssert.assertThat(
ar.result().statusCode(), Matchers.equalTo(200));
MatcherAssert.assertThat(
ar.result().getHeader(HttpHeaders.CONTENT_TYPE.toString()),
Matchers.equalTo("text/plain;charset=UTF-8"));
snapshotName.complete(ar.result().bodyAsString());
}
});

webClient
.delete(
String.format(
"%s/recordings/%s",
v1RequestUrl(),
snapshotName.get(REQUEST_TIMEOUT_SECONDS, TimeUnit.SECONDS)))
.basicAuthentication("user", "pass")
.send(
ar -> {
if (assertRequestStatus(ar, deleteSnapshotResponse)) {
deleteSnapshotResponse.complete(ar.result().bodyAsJsonObject());
}
});
MatcherAssert.assertThat(
snapshotName.get(REQUEST_TIMEOUT_SECONDS, TimeUnit.SECONDS),
Matchers.matchesPattern(SNAPSHOT_NAME_PATTERN));

try {
deleteSnapshotResponse.get(REQUEST_TIMEOUT_SECONDS, TimeUnit.SECONDS);
} catch (InterruptedException | ExecutionException e) {
throw new ITestCleanupFailedException(
// Clean up recording and snapshot
webClient
.extensions()
.delete(
String.format("%s/recordings/%s", v1RequestUrl(), TEST_RECORDING_NAME),
true,
5);
webClient
.extensions()
.delete(
String.format(
"Failed to delete snapshot %s",
"%s/recordings/%s",
v1RequestUrl(),
snapshotName.get(REQUEST_TIMEOUT_SECONDS, TimeUnit.SECONDS)),
e);
}
}
true,
5);
}

@Test
@Order(4)
void testPostV1SnapshotThrowsWithNonExistentTarget() throws Exception {

CompletableFuture<String> snapshotResponse = new CompletableFuture<>();
webClient
.post("/api/v1/targets/notFound%2F9000/snapshot")
Expand All @@ -255,126 +209,84 @@ void testPostV1SnapshotThrowsWithNonExistentTarget() throws Exception {
}

@Test
@Order(5)
void testPostV2ShouldCreateSnapshot() throws Exception {
CompletableFuture<String> snapshotName = new CompletableFuture<>();

try {
// Create a recording
MultiMap form = MultiMap.caseInsensitiveMultiMap();
form.add("recordingName", TEST_RECORDING_NAME);
form.add("duration", "5");
form.add("events", "template=ALL");
webClient
.extensions()
.post(String.format("%s/recordings", v1RequestUrl()), true, form, 5);

// Create a snapshot recording of all events at that time
CompletableFuture<JsonObject> createResponse = new CompletableFuture<>();
webClient
.post(String.format("%s/snapshot", v2RequestUrl()))
.basicAuthentication("user", "pass")
.send(
ar -> {
if (assertRequestStatus(ar, createResponse)) {
MatcherAssert.assertThat(
ar.result().statusCode(), Matchers.equalTo(201));
MatcherAssert.assertThat(
ar.result()
.getHeader(HttpHeaders.CONTENT_TYPE.toString()),
Matchers.equalTo("application/json;charset=UTF-8"));
createResponse.complete(ar.result().bodyAsJsonObject());
}
});

snapshotName.complete(
createResponse
.get(REQUEST_TIMEOUT_SECONDS, TimeUnit.SECONDS)
.getJsonObject("data")
.getJsonObject("result")
.getString("name"));
// Create a recording
MultiMap form = MultiMap.caseInsensitiveMultiMap();
form.add("recordingName", TEST_RECORDING_NAME);
form.add("duration", "5");
form.add("events", "template=ALL");
webClient.extensions().post(String.format("%s/recordings", v1RequestUrl()), true, form, 5);

JsonObject json = createResponse.get(REQUEST_TIMEOUT_SECONDS, TimeUnit.SECONDS);

MatcherAssert.assertThat(
json.getJsonObject("meta"),
Matchers.equalTo(
new JsonObject(
Map.of("type", "application/json", "status", "Created"))));
MatcherAssert.assertThat(json.getMap(), Matchers.hasKey("data"));
MatcherAssert.assertThat(
json.getJsonObject("data").getMap(), Matchers.hasKey("result"));
JsonObject result = json.getJsonObject("data").getJsonObject("result");
MatcherAssert.assertThat(result.getString("state"), Matchers.equalTo("STOPPED"));
MatcherAssert.assertThat(
result.getLong("startTime"),
Matchers.lessThanOrEqualTo(Instant.now().toEpochMilli()));
MatcherAssert.assertThat(
result.getString("name"),
Matchers.equalTo(snapshotName.get(REQUEST_TIMEOUT_SECONDS, TimeUnit.SECONDS)));
MatcherAssert.assertThat(result.getLong("id"), Matchers.greaterThan(0L));
MatcherAssert.assertThat(
result.getString("downloadUrl"),
Matchers.equalTo("/api/v3/activedownload/" + result.getLong("id")));
MatcherAssert.assertThat(
result.getString("reportUrl"),
Matchers.equalTo("/api/v3/targets/1/reports/" + result.getLong("remoteId")));
MatcherAssert.assertThat(result.getLong("expiry"), Matchers.nullValue());

} finally {
// Clean up recording and snapshot
CompletableFuture<JsonObject> deleteRecordingResponse = new CompletableFuture<>();
webClient
.delete(String.format("%s/recordings/%s", v1RequestUrl(), TEST_RECORDING_NAME))
.basicAuthentication("user", "pass")
.send(
ar -> {
if (assertRequestStatus(ar, deleteRecordingResponse)) {
deleteRecordingResponse.complete(
ar.result().bodyAsJsonObject());
}
});
// Create a snapshot recording of all events at that time
CompletableFuture<JsonObject> createResponse = new CompletableFuture<>();
webClient
.post(String.format("%s/snapshot", v2RequestUrl()))
.basicAuthentication("user", "pass")
.send(
ar -> {
if (assertRequestStatus(ar, createResponse)) {
MatcherAssert.assertThat(
ar.result().statusCode(), Matchers.equalTo(201));
MatcherAssert.assertThat(
ar.result().getHeader(HttpHeaders.CONTENT_TYPE.toString()),
Matchers.equalTo("application/json;charset=UTF-8"));
createResponse.complete(ar.result().bodyAsJsonObject());
}
});

try {
deleteRecordingResponse.get(REQUEST_TIMEOUT_SECONDS, TimeUnit.SECONDS);
} catch (InterruptedException | ExecutionException e) {
throw new ITestCleanupFailedException(
String.format("Failed to delete target recording %s", TEST_RECORDING_NAME),
e);
}
snapshotName.complete(
createResponse
.get(REQUEST_TIMEOUT_SECONDS, TimeUnit.SECONDS)
.getJsonObject("data")
.getJsonObject("result")
.getString("name"));

CompletableFuture<JsonObject> deleteSnapshotResponse = new CompletableFuture<>();
JsonObject json = createResponse.get(REQUEST_TIMEOUT_SECONDS, TimeUnit.SECONDS);

webClient
.delete(
String.format(
"%s/recordings/%s",
v1RequestUrl(),
snapshotName.get(REQUEST_TIMEOUT_SECONDS, TimeUnit.SECONDS)))
.basicAuthentication("user", "pass")
.send(
ar -> {
if (assertRequestStatus(ar, deleteSnapshotResponse)) {
deleteSnapshotResponse.complete(ar.result().bodyAsJsonObject());
}
});
MatcherAssert.assertThat(
json.getJsonObject("meta"),
Matchers.equalTo(
new JsonObject(Map.of("type", "application/json", "status", "Created"))));
MatcherAssert.assertThat(json.getMap(), Matchers.hasKey("data"));
MatcherAssert.assertThat(json.getJsonObject("data").getMap(), Matchers.hasKey("result"));
JsonObject result = json.getJsonObject("data").getJsonObject("result");
MatcherAssert.assertThat(result.getString("state"), Matchers.equalTo("STOPPED"));
MatcherAssert.assertThat(
result.getLong("startTime"),
Matchers.lessThanOrEqualTo(Instant.now().toEpochMilli()));
MatcherAssert.assertThat(
result.getString("name"),
Matchers.equalTo(snapshotName.get(REQUEST_TIMEOUT_SECONDS, TimeUnit.SECONDS)));
MatcherAssert.assertThat(result.getLong("id"), Matchers.greaterThan(0L));
MatcherAssert.assertThat(
result.getString("downloadUrl"),
Matchers.equalTo("/api/v3/activedownload/" + result.getLong("id")));
MatcherAssert.assertThat(
result.getString("reportUrl"),
Matchers.equalTo("/api/v3/targets/1/reports/" + result.getLong("remoteId")));
MatcherAssert.assertThat(result.getLong("expiry"), Matchers.nullValue());

try {
deleteSnapshotResponse.get(REQUEST_TIMEOUT_SECONDS, TimeUnit.SECONDS);
} catch (InterruptedException | ExecutionException e) {
throw new ITestCleanupFailedException(
webClient
.extensions()
.delete(
String.format("%s/recordings/%s", v1RequestUrl(), TEST_RECORDING_NAME),
true,
5);
webClient
.extensions()
.delete(
String.format(
"Failed to delete snapshot %s",
"%s/recordings/%s",
v1RequestUrl(),
snapshotName.get(REQUEST_TIMEOUT_SECONDS, TimeUnit.SECONDS)),
e);
}
}
true,
5);
}

@Test
@Order(6)
void testPostV2SnapshotThrowsWithNonExistentTarget() throws Exception {

CompletableFuture<String> snapshotName = new CompletableFuture<>();
webClient
.post("/api/v2/targets/notFound:9000/snapshot")
Expand Down
Loading

0 comments on commit 3aa393c

Please sign in to comment.