Skip to content

Commit

Permalink
test(itest): updated the tests and created a localstack resource (#160)
Browse files Browse the repository at this point in the history
Co-authored-by: Andrew Azores <[email protected]>
  • Loading branch information
lkonno and andrewazores authored Nov 21, 2023
1 parent c4f8511 commit 829584d
Show file tree
Hide file tree
Showing 12 changed files with 291 additions and 219 deletions.
16 changes: 4 additions & 12 deletions src/main/java/io/cryostat/recordings/Recordings.java
Original file line number Diff line number Diff line change
Expand Up @@ -305,28 +305,20 @@ public List<ArchivedRecording> agentGet(@RestPath String jvmId) {

@DELETE
@Blocking
@Path("/api/beta/recordings/{jvmId}/{filename}")
@Path("/api/beta/recordings/{connectUrl}/{filename}")
@RolesAllowed("write")
public void agentDelete(
@RestPath String jvmId,
@RestPath String connectUrl,
@RestPath String filename,
@RestForm("recording") FileUpload recording,
@RestForm("labels") JsonObject rawLabels)
throws Exception {
var metadata =
recordingHelper
.getArchivedRecordingMetadata(jvmId, filename)
.orElseGet(Metadata::empty);
var connectUrl =
Target.getTargetByJvmId(jvmId)
.map(t -> t.connectUrl)
.map(c -> c.toString())
.orElseGet(() -> metadata.labels.computeIfAbsent("connectUrl", k -> jvmId));
var target = Target.getTargetByConnectUrl(URI.create(connectUrl));
var resp =
storage.deleteObject(
DeleteObjectRequest.builder()
.bucket(archiveBucket)
.key(recordingHelper.archivedRecordingKey(jvmId, filename))
.key(recordingHelper.archivedRecordingKey(target.jvmId, filename))
.build());
if (resp.sdkHttpResponse().isSuccessful()) {
bus.publish(
Expand Down
10 changes: 3 additions & 7 deletions src/main/java/io/cryostat/reports/Reports.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,10 @@
package io.cryostat.reports;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;

import org.openjdk.jmc.flightrecorder.CouldNotLoadRecordingException;

import io.cryostat.ConfigProperties;
import io.cryostat.Producers;
import io.cryostat.core.reports.InterruptibleReportGenerator;
Expand Down Expand Up @@ -108,8 +105,7 @@ public Response getV1(@RestPath String recordingName) {
@Path("/api/v3/reports/{encodedKey}")
@Produces(MediaType.APPLICATION_JSON)
@RolesAllowed("read")
public Uni<Map<String, AnalysisResult>> get(@RestPath String encodedKey)
throws IOException, CouldNotLoadRecordingException {
public Uni<Map<String, AnalysisResult>> get(@RestPath String encodedKey) {
// TODO implement query parameter for evaluation predicate
return Uni.createFrom()
.future(
Expand All @@ -122,7 +118,7 @@ public Uni<Map<String, AnalysisResult>> get(@RestPath String encodedKey)
@Blocking
@GET
@Path("/api/v1/targets/{targetId}/reports/{recordingName}")
@Produces({MediaType.APPLICATION_JSON, MediaType.TEXT_HTML, MediaType.TEXT_PLAIN})
@Produces({MediaType.APPLICATION_JSON})
@RolesAllowed("read")
@Deprecated(since = "3.0", forRemoval = true)
public Response getActiveV1(@RestPath String targetId, @RestPath String recordingName) {
Expand All @@ -146,7 +142,7 @@ public Response getActiveV1(@RestPath String targetId, @RestPath String recordin
@CacheResult(cacheName = ACTIVE_CACHE)
@GET
@Path("/api/v3/targets/{targetId}/reports/{recordingId}")
@Produces(MediaType.APPLICATION_JSON)
@Produces({MediaType.APPLICATION_JSON})
@RolesAllowed("read")
@Deprecated(since = "3.0", forRemoval = true)
public Uni<Map<String, AnalysisResult>> getActive(
Expand Down
13 changes: 2 additions & 11 deletions src/main/resources/application-test.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,6 @@ quarkus.datasource.devservices.container-env.POSTGRESQL_DATABASE=quarkus
quarkus.datasource.devservices.username=quarkus
quarkus.datasource.devservices.password=quarkus
quarkus.datasource.devservices.db-name=quarkus
# !!!

quarkus.s3.devservices.enabled=true
quarkus.s3.devservices.buckets=archivedrecordings
# FIXME the following overrides should not be required, but currently seem to help with testcontainers reliability
quarkus.aws.devservices.localstack.image-name=localstack/localstack:2.1.0
quarkus.aws.devservices.localstack.container-properties.START_WEB=0
quarkus.aws.devservices.localstack.container-properties.SERVICES=s3
quarkus.aws.devservices.localstack.container-properties.EAGER_SERVICE_LOADING=1
quarkus.aws.devservices.localstack.container-properties.SKIP_SSL_CERT_DOWNLOAD=1
quarkus.aws.devservices.localstack.container-properties.SKIP_INFRA_DOWNLOADS=1
quarkus.aws.devservices.localstack.container-properties.DISABLE_EVENTS=1
quarkus.s3.devservices.enabled=false
# !!!
12 changes: 7 additions & 5 deletions src/test/java/io/cryostat/resources/GrafanaResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
public class GrafanaResource
implements QuarkusTestResourceLifecycleManager, DevServicesContext.ContextAware {

private static int GRAFANA_PORT = 3000;
private static String IMAGE_NAME = "quay.io/cryostat/cryostat-grafana-dashboard:latest";
private static Map<String, String> envMap =
private static final int GRAFANA_PORT = 3000;
private static final String IMAGE_NAME = "quay.io/cryostat/cryostat-grafana-dashboard:latest";
private static final Map<String, String> envMap =
Map.of(
"GF_INSTALL_PLUGINS", "grafana-simple-json-datasource",
"GF_AUTH_ANONYMOUS_ENABLED", "true",
Expand Down Expand Up @@ -59,8 +59,10 @@ public Map<String, String> start() {

@Override
public void stop() {
container.stop();
container.close();
if (container != null) {
container.stop();
container.close();
}
}

@Override
Expand Down
12 changes: 7 additions & 5 deletions src/test/java/io/cryostat/resources/JFRDatasourceResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
public class JFRDatasourceResource
implements QuarkusTestResourceLifecycleManager, DevServicesContext.ContextAware {

private static int JFR_DATASOURCE_PORT = 8080;
private static String IMAGE_NAME = "quay.io/cryostat/jfr-datasource:latest";
private static Map<String, String> envMap = Map.of();
private static final int JFR_DATASOURCE_PORT = 8080;
private static final String IMAGE_NAME = "quay.io/cryostat/jfr-datasource:latest";
private static final Map<String, String> envMap = Map.of();

private Optional<String> containerNetworkId;
private GenericContainer<?> container;
Expand All @@ -56,8 +56,10 @@ public Map<String, String> start() {

@Override
public void stop() {
container.stop();
container.close();
if (container != null) {
container.stop();
container.close();
}
}

@Override
Expand Down
93 changes: 93 additions & 0 deletions src/test/java/io/cryostat/resources/LocalStackResource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright The Cryostat Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.cryostat.resources;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import io.quarkus.test.common.DevServicesContext;
import io.quarkus.test.common.QuarkusTestResourceLifecycleManager;
import org.jboss.logging.Logger;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.utility.DockerImageName;

public class LocalStackResource
implements QuarkusTestResourceLifecycleManager, DevServicesContext.ContextAware {

private static int S3_PORT = 4566;
private static final String IMAGE_NAME = "docker.io/localstack/localstack:latest";
private static final Map<String, String> envMap =
Map.of(
"START_WEB", "0",
"SERVICES", "s3",
"EAGER_SERVICE_LOADING", "1",
"SKIP_SSL_CERT_DOWNLOAD", "1",
"DISABLE_EVENTS", "1");
private static final Logger logger = Logger.getLogger(LocalStackResource.class);
private Optional<String> containerNetworkId;
private GenericContainer<?> container;

@Override
public Map<String, String> start() {
container =
new GenericContainer<>(DockerImageName.parse(IMAGE_NAME))
.withExposedPorts(S3_PORT)
.withEnv(envMap)
.waitingFor(Wait.forHealthcheck());
containerNetworkId.ifPresent(container::withNetworkMode);

container.start();

String networkHostPort =
"http://" + container.getHost() + ":" + container.getMappedPort(S3_PORT);

Map<String, String> properties = new HashMap<String, String>();
properties.put("quarkus.s3.aws.region", "us-east-1");
properties.put("s3.url.override", networkHostPort);
properties.put("quarkus.s3.endpoint-override", properties.get("s3.url.override"));
properties.put("quarkus.s3.path-style-access", "true");
properties.put("quarkus.s3.aws.credentials.type", "static");
properties.put("quarkus.s3.aws.credentials.static-provider.access-key-id", "unused");
properties.put("quarkus.s3.aws.credentials.static-provider.secret-access-key", "unused");
properties.put(
"aws.access-key-id",
properties.get("quarkus.s3.aws.credentials.static-provider.access-key-id"));
properties.put("aws.accessKeyId", properties.get("aws.access-key-id"));
properties.put(
"aws.secret-access-key",
properties.get("quarkus.s3.aws.credentials.static-provider.secret-access-key"));
properties.put("aws.secretAccessKey", properties.get("aws.secret-access-key"));
properties.entrySet().forEach(e -> System.setProperty(e.getKey(), e.getValue()));
logger.infov("Configured properties: {0}", properties);

return properties;
}

@Override
public void stop() {
if (container != null) {
container.stop();
container.close();
}
}

@Override
public void setIntegrationTestContext(DevServicesContext context) {
containerNetworkId = context.containerNetworkId();
}
}
2 changes: 0 additions & 2 deletions src/test/java/itest/NonExistentTargetIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

@QuarkusIntegrationTest
@Disabled("TODO")
public class NonExistentTargetIT extends StandardSelfTest {

static final String BAD_TARGET_CONNECT_URL =
Expand Down
40 changes: 21 additions & 19 deletions src/test/java/itest/NoopAuthV2IT.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

@QuarkusIntegrationTest
@Disabled("TODO")
public class NoopAuthV2IT extends StandardSelfTest {

HttpRequest<Buffer> req;
Expand All @@ -44,23 +42,27 @@ void createRequest() {
@Test
public void shouldRespond200() throws Exception {
CompletableFuture<JsonObject> future = new CompletableFuture<>();
req.send(
ar -> {
if (ar.succeeded()) {
future.complete(ar.result().bodyAsJsonObject());
} else {
future.completeExceptionally(ar.cause());
}
});
JsonObject expected =
new JsonObject(
Map.of(
"meta",
Map.of(
"status", "OK",
"type", "application/json"),
"data", Map.of("result", Map.of("username", ""))));
req.basicAuthentication("user", "pass")
.send(
ar -> {
if (ar.succeeded()) {
future.complete(ar.result().bodyAsJsonObject());
} else {
future.completeExceptionally(ar.cause());
}
});

JsonObject response = future.get(REQUEST_TIMEOUT_SECONDS, TimeUnit.SECONDS);

MatcherAssert.assertThat(response.getJsonObject("meta"), Matchers.notNullValue());
MatcherAssert.assertThat(
response.getJsonObject("meta").getString("status"), Matchers.equalTo("OK"));
MatcherAssert.assertThat(
response.getJsonObject("meta").getString("type"),
Matchers.equalTo("application/json"));
MatcherAssert.assertThat(response.getJsonObject("data"), Matchers.notNullValue());
MatcherAssert.assertThat(
future.get(REQUEST_TIMEOUT_SECONDS, TimeUnit.SECONDS), Matchers.equalTo(expected));
response.getJsonObject("data").getString("result"),
Matchers.equalTo(Map.of("username", "user").toString()));
}
}
Loading

0 comments on commit 829584d

Please sign in to comment.