Skip to content

Commit

Permalink
fixup! tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewazores committed Dec 24, 2024
1 parent c06bf39 commit c473f3c
Show file tree
Hide file tree
Showing 5 changed files with 376 additions and 175 deletions.
76 changes: 60 additions & 16 deletions src/test/java/io/cryostat/resources/AgentApplicationResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,58 +23,98 @@
import io.quarkus.test.common.DevServicesContext;
import io.quarkus.test.common.QuarkusTestResourceLifecycleManager;
import org.jboss.logging.Logger;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.containers.Network;
import org.testcontainers.containers.wait.strategy.HostPortWaitStrategy;
import org.testcontainers.utility.DockerImageName;

public class AgentApplicationResource
implements QuarkusTestResourceLifecycleManager, DevServicesContext.ContextAware {

private static final String IMAGE_NAME =
"quay.io/redhat-java-monitoring/quarkus-cryostat-agent:latest";
private static final int AGENT_PORT = 9977;
public static final int PORT = 9977;
public static final String ALIAS = "quarkus-cryostat-agent";
private static final Map<String, String> envMap =
new HashMap<>(
Map.of(
"JAVA_OPTS_APPEND",
"-javaagent:/deployments/app/cryostat-agent.jar",
"""
-javaagent:/deployments/app/cryostat-agent.jar
-Djava.util.logging.manager=org.jboss.logmanager.LogManager
-Dio.cryostat.agent.shaded.org.slf4j.simpleLogger.defaultLogLevel=trace
""",
"QUARKUS_HTTP_PORT",
"101010",
"9898",
"CRYOSTAT_AGENT_APP_NAME",
"quarkus-cryostat-agent",
"CRYOSTAT_AGENT_WEBCLIENT_TLS_REQUIRED",
"false",
"CRYOSTAT_AGENT_WEBSERVER_HOST",
"0.0.0.0",
"CRYOSTAT_AGENT_WEBSERVER_PORT",
Integer.toString(AGENT_PORT),
"CRYOSTAT_AGENT_BASEURI",
"http://cryostat:8081/",
Integer.toString(PORT),
"CRYOSTAT_AGENT_BASEURI_RANGE",
"public",
"CRYOSTAT_AGENT_API_WRITES_ENABLED",
"true"));
private static final Logger logger = Logger.getLogger(AgentApplicationResource.class);
private Optional<String> containerNetworkId;
private AuthProxyContainer authProxy;
private GenericContainer<?> container;
private AtomicInteger cryostatPort = new AtomicInteger(8081);

@Override
public Map<String, String> start() {
Optional<Network> network =
containerNetworkId.map(
id ->
new Network() {
@Override
public String getId() {
return id;
}

@Override
public void close() {}

@Override
public Statement apply(
Statement base, Description description) {
throw new UnsupportedOperationException(
"Unimplemented method 'apply'");
}
});
authProxy = new AuthProxyContainer(network, cryostatPort.get());

container =
new GenericContainer<>(DockerImageName.parse(IMAGE_NAME))
.withExposedPorts(AGENT_PORT)
.dependsOn(authProxy)
.withExposedPorts(PORT)
.withEnv(envMap)
.waitingFor(Wait.forListeningPort());
containerNetworkId.ifPresent(container::withNetworkMode);
.withNetworkAliases(ALIAS)
.waitingFor(new HostPortWaitStrategy().forPorts(PORT));
network.ifPresent(container::withNetwork);
container.addEnv(
"CRYOSTAT_AGENT_BASEURI", String.format("http://cryostat:%d/", cryostatPort.get()));
container.addEnv(
"CRYOSTAT_AGENT_CALLBACK",
String.format("http://%s:%d/", container.getContainerName(), AGENT_PORT));
"CRYOSTAT_AGENT_BASEURI",
String.format("http://%s:%d/", AuthProxyContainer.ALIAS, AuthProxyContainer.PORT));
container.addEnv("CRYOSTAT_AGENT_CALLBACK", String.format("http://%s:%d/", ALIAS, PORT));

container.start();

// return Map.of("quarkus.test.arg-line", "--name=cryostat");
return Map.of();
return Map.of(
"quarkus.test.arg-line", "--network-alias=cryostat",
"cryostat.agent.tls.required", "false",
"cryostat.http.proxy.host", ALIAS,
"cryostat.http.proxy.port", Integer.toString(cryostatPort.get()),
"quarkus.http.proxy.proxy-address-forwarding", "true",
"quarkus.http.proxy.allow-x-forwarded", "true",
"quarkus.http.proxy.enable-forwarded-host", "true",
"quarkus.http.proxy.enable-forwarded-prefix", "true",
"quarkus.http.access-log.pattern", "long",
"quarkus.http.access-log.enabled", "true");
}

@Override
Expand All @@ -83,6 +123,10 @@ public void stop() {
container.stop();
container.close();
}
if (authProxy != null) {
authProxy.stop();
authProxy.close();
}
}

@Override
Expand Down
78 changes: 78 additions & 0 deletions src/test/java/io/cryostat/resources/AuthProxyContainer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* 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 org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.Network;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.images.builder.Transferable;

public class AuthProxyContainer extends GenericContainer<AuthProxyContainer> {

private static final String IMAGE_NAME = "quay.io/oauth2-proxy/oauth2-proxy:latest";
private static final String CFG_FILE_PATH = "/tmp/auth_proxy_alpha_config.yml";
static final int PORT = 8080;
static final String ALIAS = "authproxy";
private static final Map<String, String> envMap =
new HashMap<>(
Map.of(
"OAUTH2_PROXY_REDIRECT_URL",
"http://localhost:8080/oauth2/callback",
"OAUTH2_PROXY_COOKIE_SECRET",
"__24_BYTE_COOKIE_SECRET_",
"OAUTH2_PROXY_SKIP_AUTH_ROUTES",
".*",
"OAUTH2_PROXY_EMAIL_DOMAINS",
"*"));
private static final String ALPHA_CONFIG =
"""
server:
BindAddress: http://AUTHPROXY_HOST:AUTHPROXY_PORT
upstreamConfig:
proxyRawPath: true
upstreams:
- id: cryostat
path: /
uri: http://cryostat:CRYOSTAT_PORT
providers:
- id: dummy
name: Unused - Sign In Below
clientId: CLIENT_ID
clientSecret: CLIENT_SECRET
provider: google
""";

public AuthProxyContainer(Optional<Network> network, int cryostatPort) {
super(IMAGE_NAME);
network.ifPresent(this::withNetwork);
withCommand(String.format("--alpha-config=%s", CFG_FILE_PATH));
withExposedPorts(PORT);
withNetworkAliases(ALIAS);
withEnv(envMap);
withCopyToContainer(
Transferable.of(
ALPHA_CONFIG
.replaceAll("AUTHPROXY_HOST", "0.0.0.0")
.replaceAll("AUTHPROXY_PORT", Integer.toString(PORT))
.replaceAll("CRYOSTAT_PORT", Integer.toString(cryostatPort))),
CFG_FILE_PATH);
waitingFor(Wait.forLogMessage(".*OAuthProxy configured.*", 1));
}
}
58 changes: 54 additions & 4 deletions src/test/java/itest/AgentDiscoveryIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,69 @@
*/
package itest;

import java.time.Duration;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;

import io.cryostat.resources.AgentApplicationResource;
import io.cryostat.util.HttpStatusCodeIdentifier;

import io.quarkus.test.common.QuarkusTestResource;
import io.quarkus.test.junit.QuarkusIntegrationTest;
import itest.bases.StandardSelfTest;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.client.HttpResponse;
import itest.bases.HttpClientTest;
import junit.framework.AssertionFailedError;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.jboss.logging.Logger;
import org.junit.jupiter.api.Test;

@QuarkusIntegrationTest
@QuarkusTestResource(value = AgentApplicationResource.class, restrictToAnnotatedClass = true)
public class AgentDiscoveryIT extends StandardSelfTest {
public class AgentDiscoveryIT extends HttpClientTest {

static final Logger logger = Logger.getLogger(AgentDiscoveryIT.class);
static final Duration TIMEOUT = Duration.ofSeconds(60);

@Test
void shouldDiscoverTarget() {
waitForDiscovery(1);
void shouldDiscoverTarget() throws InterruptedException, TimeoutException, ExecutionException {
long last = System.nanoTime();
long elapsed = 0;
while (true) {
HttpResponse<Buffer> req =
webClient.extensions().get("/api/v4/targets", REQUEST_TIMEOUT_SECONDS);
if (HttpStatusCodeIdentifier.isSuccessCode(req.statusCode())) {
JsonArray result = req.bodyAsJsonArray();
if (result.size() == 1) {
JsonObject obj = result.getJsonObject(0);
MatcherAssert.assertThat(
obj.getString("alias"),
Matchers.equalTo(AgentApplicationResource.ALIAS));
MatcherAssert.assertThat(
obj.getString("connectUrl"),
Matchers.equalTo(
String.format(
"http://%s:%d/",
AgentApplicationResource.ALIAS,
AgentApplicationResource.PORT)));

MatcherAssert.assertThat(obj.getBoolean("agent"), Matchers.is(true));
break;
} else if (result.size() > 1) {
throw new IllegalStateException("Discovered too many targets");
}
}

long now = System.nanoTime();
elapsed += (now - last);
last = now;
if (Duration.ofNanos(elapsed).compareTo(TIMEOUT) > 0) {
throw new AssertionFailedError("Timed out");
}
Thread.sleep(5_000);
}
}
}
Loading

0 comments on commit c473f3c

Please sign in to comment.