From eec2ccd6286b63540f92c61538bdcb35292948e3 Mon Sep 17 00:00:00 2001 From: EddeCCC Date: Mon, 30 Sep 2024 17:04:52 +0200 Subject: [PATCH] test: adjust integration test --- inspectit-gepard-agent/build.gradle | 9 +- .../hook/MethodHookManager.java | 3 +- .../integrationtest/spring/ScopeTest.java | 94 ++++++++++++------- 3 files changed, 69 insertions(+), 37 deletions(-) diff --git a/inspectit-gepard-agent/build.gradle b/inspectit-gepard-agent/build.gradle index a7d04d4..0bc6d74 100644 --- a/inspectit-gepard-agent/build.gradle +++ b/inspectit-gepard-agent/build.gradle @@ -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") @@ -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") diff --git a/inspectit-gepard-agent/src/main/java/rocks/inspectit/gepard/agent/instrumentation/hook/MethodHookManager.java b/inspectit-gepard-agent/src/main/java/rocks/inspectit/gepard/agent/instrumentation/hook/MethodHookManager.java index 55db1cd..571ad45 100644 --- a/inspectit-gepard-agent/src/main/java/rocks/inspectit/gepard/agent/instrumentation/hook/MethodHookManager.java +++ b/inspectit-gepard-agent/src/main/java/rocks/inspectit/gepard/agent/instrumentation/hook/MethodHookManager.java @@ -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 methodMatcher = configuration.methodMatcher(); TypeDescription type = TypeDescription.ForLoadedType.of(clazz); Set matchedMethods = @@ -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()); } diff --git a/inspectit-gepard-agent/src/test/java/rocks/inspectit/gepard/agent/integrationtest/spring/ScopeTest.java b/inspectit-gepard-agent/src/test/java/rocks/inspectit/gepard/agent/integrationtest/spring/ScopeTest.java index cf99082..4148736 100644 --- a/inspectit-gepard-agent/src/test/java/rocks/inspectit/gepard/agent/integrationtest/spring/ScopeTest.java +++ b/inspectit-gepard-agent/src/test/java/rocks/inspectit/gepard/agent/integrationtest/spring/ScopeTest.java @@ -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(); @@ -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(); @@ -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(); @@ -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(); @@ -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"); @@ -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(); @@ -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); } /** @@ -161,15 +162,44 @@ 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) @@ -177,8 +207,8 @@ private void awaitConfigurationUpdate() { .until( () -> { String newLogs = target.getLogs(); - int currentConfigUpdateCount = countTimes(newLogs, updateMessage); - return currentConfigUpdateCount > configUpdateCount; + int currentUpdateCount = countTimes(newLogs, updateMessage); + return currentUpdateCount > updateCount; }); } }