Skip to content

Commit

Permalink
flesh out tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewazores committed Feb 26, 2025
1 parent 9995e66 commit 4a4cb1d
Show file tree
Hide file tree
Showing 5 changed files with 380 additions and 469 deletions.
97 changes: 97 additions & 0 deletions src/test/java/io/cryostat/AbstractTestBase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* 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;

import static io.restassured.RestAssured.given;

import java.time.Duration;

import io.cryostat.util.HttpStatusCodeIdentifier;

import io.restassured.http.ContentType;
import jakarta.inject.Inject;
import org.apache.http.client.utils.URLEncodedUtils;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.jboss.logging.Logger;
import org.junit.jupiter.api.BeforeEach;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.HeadBucketRequest;

public abstract class AbstractTestBase {

public static final String SELF_JMX_URL = "service:jmx:rmi:///jndi/rmi://localhost:0/jmxrmi";
public static String SELF_JMX_URL_ENCODED =
URLEncodedUtils.formatSegments(SELF_JMX_URL).substring(1);
public static final String SELFTEST_ALIAS = "selftest";

@ConfigProperty(name = "storage.buckets.archives.name")
String archivesBucket;

@ConfigProperty(name = "test.storage.timeout", defaultValue = "5m")
Duration storageTimeout;

@ConfigProperty(name = "test.storage.retry", defaultValue = "5s")
Duration storageRetry;

@Inject Logger logger;
@Inject S3Client storage;

@BeforeEach
void waitForStorage() throws InterruptedException {
long totalTime = 0;
while (!bucketExists(archivesBucket)) {
long start = System.nanoTime();
Thread.sleep(storageRetry.toMillis());
long elapsed = System.nanoTime() - start;
totalTime += elapsed;
if (Duration.ofNanos(totalTime).compareTo(storageTimeout) > 0) {
throw new IllegalStateException("Storage took too long to become ready");
}
}
}

private boolean bucketExists(String bucket) {
boolean exists = false;
try {
exists =
HttpStatusCodeIdentifier.isSuccessCode(
storage.headBucket(HeadBucketRequest.builder().bucket(bucket).build())
.sdkHttpResponse()
.statusCode());
logger.debugv("Storage bucket \"{0}\" exists? {1}", bucket, exists);
} catch (Exception e) {
logger.warn(e);
}
return exists;
}

protected int defineSelfCustomTarget() {
return given().basePath("/")
.log()
.all()
.contentType(ContentType.URLENC)
.formParam("connectUrl", SELF_JMX_URL)
.formParam("alias", SELFTEST_ALIAS)
.when()
.post("/api/v4/targets")
.then()
.log()
.all()
.extract()
.jsonPath()
.getInt("id");
}
}
76 changes: 1 addition & 75 deletions src/test/java/io/cryostat/AbstractTransactionalTestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,72 +15,15 @@
*/
package io.cryostat;

import static io.restassured.RestAssured.given;

import java.time.Duration;

import io.cryostat.util.HttpStatusCodeIdentifier;

import io.restassured.http.ContentType;
import jakarta.inject.Inject;
import org.apache.http.client.utils.URLEncodedUtils;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.flywaydb.core.Flyway;
import org.jboss.logging.Logger;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.HeadBucketRequest;

public abstract class AbstractTransactionalTestBase {

public static final String SELF_JMX_URL = "service:jmx:rmi:///jndi/rmi://localhost:0/jmxrmi";
public static String SELF_JMX_URL_ENCODED =
URLEncodedUtils.formatSegments(SELF_JMX_URL).substring(1);
public static final String SELFTEST_ALIAS = "selftest";

@ConfigProperty(name = "storage.buckets.archives.name")
String archivesBucket;

@ConfigProperty(name = "test.storage.timeout", defaultValue = "5m")
Duration storageTimeout;

@ConfigProperty(name = "test.storage.retry", defaultValue = "5s")
Duration storageRetry;
public abstract class AbstractTransactionalTestBase extends AbstractTestBase {

@Inject Logger logger;
@Inject S3Client storage;
@Inject Flyway flyway;

@BeforeEach
void waitForStorage() throws InterruptedException {
long totalTime = 0;
while (!bucketExists(archivesBucket)) {
long start = System.nanoTime();
Thread.sleep(storageRetry.toMillis());
long elapsed = System.nanoTime() - start;
totalTime += elapsed;
if (Duration.ofNanos(totalTime).compareTo(storageTimeout) > 0) {
throw new IllegalStateException("Storage took too long to become ready");
}
}
}

private boolean bucketExists(String bucket) {
boolean exists = false;
try {
exists =
HttpStatusCodeIdentifier.isSuccessCode(
storage.headBucket(HeadBucketRequest.builder().bucket(bucket).build())
.sdkHttpResponse()
.statusCode());
logger.debugv("Storage bucket \"{0}\" exists? {1}", bucket, exists);
} catch (Exception e) {
logger.warn(e);
}
return exists;
}

@BeforeEach
void migrate() {
flyway.migrate();
Expand All @@ -91,21 +34,4 @@ void cleanup() {
flyway.clean();
flyway.migrate();
}

protected int defineSelfCustomTarget() {
return given().basePath("/")
.log()
.all()
.contentType(ContentType.URLENC)
.formParam("connectUrl", SELF_JMX_URL)
.formParam("alias", SELFTEST_ALIAS)
.when()
.post("/api/v4/targets")
.then()
.log()
.all()
.extract()
.jsonPath()
.getInt("id");
}
}
Loading

0 comments on commit 4a4cb1d

Please sign in to comment.