Skip to content

Commit

Permalink
test: adjust integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
EddeCCC committed Sep 30, 2024
1 parent 7012135 commit eec2ccd
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 37 deletions.
9 changes: 5 additions & 4 deletions inspectit-gepard-agent/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ dependencies {
// inspectit-gepard dependencies
implementation("net.bytebuddy:byte-buddy:1.14.15")
implementation("ch.qos.logback:logback-classic:1.5.6")
implementation("org.slf4j:jcl-over-slf4j:2.0.13")
implementation("org.slf4j:slf4j-api:2.0.16")
// http client for server notification
implementation("org.apache.httpcomponents.client5:httpclient5:5.3.1")
implementation("com.fasterxml.jackson.core:jackson-databind:2.17.1")
Expand Down Expand Up @@ -112,10 +112,11 @@ dependencies {

// IntegrationTesting
testImplementation("org.mock-server:mockserver-junit-jupiter:5.15.0")
testImplementation 'org.testcontainers:mockserver:1.20.0'
testImplementation 'org.testcontainers:junit-jupiter:1.20.0'
testImplementation("org.testcontainers:mockserver:1.20.0")
testImplementation("org.testcontainers:junit-jupiter:1.20.0")
testImplementation("ch.qos.logback:logback-classic:1.5.6")
testImplementation 'org.apache.logging.log4j:log4j-slf4j-impl:2.24.0'
testImplementation 'org.mock-server:mockserver-netty:5.15.0'
testImplementation("org.mock-server:mockserver-netty:5.15.0")
testImplementation("com.squareup.okhttp3:okhttp:4.12.0")
testImplementation("io.opentelemetry.proto:opentelemetry-proto:1.3.2-alpha")
testImplementation("com.google.protobuf:protobuf-java-util:3.25.4")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public IMethodHook getHook(Class<?> clazz, String methodSignature) {
}

public void updateHooksFor(Class<?> clazz, ClassInstrumentationConfiguration configuration) {
log.info("Updating hooks for {}", clazz.getName());
log.debug("Updating hooks for {}", clazz.getName());
ElementMatcher.Junction<MethodDescription> methodMatcher = configuration.methodMatcher();
TypeDescription type = TypeDescription.ForLoadedType.of(clazz);
Set<MethodDescription.InDefinedShape> matchedMethods =
Expand Down Expand Up @@ -112,6 +112,7 @@ private void updateHooks(Class<?> clazz, ClassHookConfiguration classConfigurati
}
}
});
// TODO This should be DEBUG, but should also be visible in tests
log.info("Updated {} method hooks for {}", operationCounter.get(), clazz.getName());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@
import okhttp3.Call;
import okhttp3.Request;
import org.awaitility.Awaitility;
import org.awaitility.core.ConditionTimeoutException;
import org.junit.jupiter.api.Test;

public class ScopeTest extends SpringTestBase {

private static final String configDir = "integrationtest/configurations/";

@Test
void scopeWithoutMethodInstrumentsAllMethods() throws Exception {
configurationServerMock.configServerSetup("integrationtest/configurations/simple-scope.json");
configurationServerMock.configServerSetup(configDir + "simple-scope.json");
startTarget("/opentelemetry-extensions.jar");
awaitInstrumentationUpdate();

sendRequestToTarget("/greeting");
Thread.sleep(2000);
String logs = target.getLogs();
stopTarget();

Expand All @@ -25,12 +29,11 @@ void scopeWithoutMethodInstrumentsAllMethods() throws Exception {

@Test
void scopeWithOneMethodInstrumentsOneMethod() throws Exception {
configurationServerMock.configServerSetup(
"integrationtest/configurations/scope-with-method.json");
configurationServerMock.configServerSetup(configDir + "scope-with-method.json");
startTarget("/opentelemetry-extensions.jar");
sendRequestToTarget("/greeting");
awaitInstrumentationUpdate();

Thread.sleep(2000);
sendRequestToTarget("/greeting");
String logs = target.getLogs();
stopTarget();

Expand All @@ -39,12 +42,11 @@ void scopeWithOneMethodInstrumentsOneMethod() throws Exception {

@Test
void scopeWithTwoMethodsInstrumentsTwoMethods() throws Exception {
configurationServerMock.configServerSetup(
"integrationtest/configurations/scope-with-multiple-methods.json");
configurationServerMock.configServerSetup(configDir + "scope-with-multiple-methods.json");
startTarget("/opentelemetry-extensions.jar");
sendRequestToTarget("/greeting");
awaitInstrumentationUpdate();

Thread.sleep(2000);
sendRequestToTarget("/greeting");
String logs = target.getLogs();
stopTarget();

Expand All @@ -53,12 +55,11 @@ void scopeWithTwoMethodsInstrumentsTwoMethods() throws Exception {

@Test
void emptyConfigurationDoesntInstrument() throws Exception {
configurationServerMock.configServerSetup(
"integrationtest/configurations/empty-configuration.json");
configurationServerMock.configServerSetup(configDir + "empty-configuration.json");
startTarget("/opentelemetry-extensions.jar");
sendRequestToTarget("/greeting");
awaitInstrumentationUpdate();

Thread.sleep(2000);
sendRequestToTarget("/greeting");
String logs = target.getLogs();
stopTarget();

Expand All @@ -67,9 +68,10 @@ void emptyConfigurationDoesntInstrument() throws Exception {

@Test
void multipleScopesInstrumentAllSelectedMethods() throws Exception {
configurationServerMock.configServerSetup(
"integrationtest/configurations/multiple-scopes.json");
configurationServerMock.configServerSetup(configDir + "multiple-scopes.json");
startTarget("/opentelemetry-extensions.jar");
awaitInstrumentationUpdate();

sendRequestToTarget("/greeting");
sendRequestToTarget("/front");

Expand All @@ -82,24 +84,22 @@ void multipleScopesInstrumentAllSelectedMethods() throws Exception {
@Test
void configurationUpdatesAreApplied() throws Exception {
// Set up config server to instrument multiple methods
configurationServerMock.configServerSetup(
"integrationtest/configurations/scope-with-multiple-methods.json");

configurationServerMock.configServerSetup(configDir + "scope-with-multiple-methods.json");
startTarget("/opentelemetry-extensions.jar");
sendRequestToTarget("/greeting");
awaitInstrumentationUpdate();

sendRequestToTarget("/greeting");
String logs = target.getLogs();

assertLogs(logs, 2);

// Update configuration to only instrument one method
configurationServerMock.reset();
configurationServerMock.configServerSetup(
"integrationtest/configurations/scope-with-method.json");

configurationServerMock.configServerSetup(configDir + "scope-with-method.json");
awaitConfigurationUpdate();
sendRequestToTarget("/greeting");
awaitInstrumentationUpdate();

sendRequestToTarget("/greeting");
logs = target.getLogs();
stopTarget();

Expand All @@ -110,9 +110,10 @@ void configurationUpdatesAreApplied() throws Exception {
private void sendRequestToTarget(String path) throws Exception {
String url = String.format("http://localhost:%d%s", target.getMappedPort(8080), path);
Call call = client.newCall(new Request.Builder().url(url).get().build());
// Wait for instrumentation
Thread.sleep(5000);
call.execute();

// wait for logs
Thread.sleep(1000);
}

/**
Expand Down Expand Up @@ -161,24 +162,53 @@ private int countTimes(String logs, String message) {
}

/**
* Waits until the configuration was polled one more time. This happens via checking the container
* logs. First the method counts the current amount of update messages. If the amount of update
* messages has increased, it is assumed that a new configuration has been pooled.
* Waits until the instrumentation was applied in the method hooks. The test should not fail here,
* if no further update message was found.
*/
private void awaitInstrumentationUpdate() {
String updateMessage =
"method hooks for io.opentelemetry.smoketest.springboot.controller.WebController";

try {
awaitUpdateMessage(updateMessage);
} catch (ConditionTimeoutException e) {
System.out.println("No instrumentation update occurred");
}
}

/**
* Waits until the configuration was polled one more time. The test should not fail here, if no
* further update message was found.
*/
private void awaitConfigurationUpdate() {
String updateMessage =
"Fetched configuration from configuration server and received status code 200";
try {
awaitUpdateMessage(updateMessage);
} catch (ConditionTimeoutException e) {
System.out.println("No configuration update occurred");
}
}

/**
* Waits until a certain update message was logged again. This happens via checking the container
* logs. First the method counts the current amount of update messages. If the amount of update
* messages has increased, it is assumed that a new configuration has been pooled.
*
* @param updateMessage the message, which will be waited for
*/
private void awaitUpdateMessage(String updateMessage) {
String logs = target.getLogs();
int configUpdateCount = countTimes(logs, updateMessage);
int updateCount = countTimes(logs, updateMessage);

Awaitility.await()
.pollDelay(5, TimeUnit.SECONDS)
.atMost(15, TimeUnit.SECONDS)
.until(
() -> {
String newLogs = target.getLogs();
int currentConfigUpdateCount = countTimes(newLogs, updateMessage);
return currentConfigUpdateCount > configUpdateCount;
int currentUpdateCount = countTimes(newLogs, updateMessage);
return currentUpdateCount > updateCount;
});
}
}

0 comments on commit eec2ccd

Please sign in to comment.