Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(agent): add Agent itest #759

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,7 @@
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
<maven.home>${maven.home}</maven.home>
</systemPropertyVariables>
<skipTests>${skip.ut}</skipTests>
</configuration>
</plugin>
<plugin>
Expand Down
139 changes: 139 additions & 0 deletions src/test/java/io/cryostat/resources/AgentApplicationResource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/*
* 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 java.util.concurrent.atomic.AtomicInteger;

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.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";
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
-Djava.util.logging.manager=org.jboss.logmanager.LogManager
-Dio.cryostat.agent.shaded.org.slf4j.simpleLogger.defaultLogLevel=trace
""",
"QUARKUS_HTTP_PORT",
"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(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))
.dependsOn(authProxy)
.withExposedPorts(PORT)
.withEnv(envMap)
.withNetworkAliases(ALIAS)
.waitingFor(new HostPortWaitStrategy().forPorts(PORT));
network.ifPresent(container::withNetwork);
container.addEnv(
"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", "--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
public void stop() {
if (container != null) {
container.stop();
container.close();
}
if (authProxy != null) {
authProxy.stop();
authProxy.close();
}
}

@Override
public void setIntegrationTestContext(DevServicesContext context) {
containerNetworkId = context.containerNetworkId();
cryostatPort.set(
Integer.parseInt(
context.devServicesProperties().getOrDefault("quarkus.http.port", "8081")));
}
}
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));
}
}
86 changes: 86 additions & 0 deletions src/test/java/itest/AgentDiscoveryIT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* 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 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 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;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;

@QuarkusIntegrationTest
@QuarkusTestResource(value = AgentApplicationResource.class, restrictToAnnotatedClass = true)
@EnabledIfEnvironmentVariable(named = "CI_ARCH", matches = "^$")
@EnabledIfEnvironmentVariable(named = "CI_ARCH", matches = "^amd64|AMD64$")
public class AgentDiscoveryIT extends HttpClientTest {

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

@Test
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
Loading