From a1ecc75a2a50d9c7c161c33060b16d7b603bfd49 Mon Sep 17 00:00:00 2001 From: EddeCCC Date: Thu, 12 Sep 2024 13:49:15 +0200 Subject: [PATCH] add test --- .../configuration/ConfigurationManager.java | 1 - .../cache/process/BatchInstrumenterTest.java | 6 +- .../ConfigurationServerMock.java | 1 - .../integrationtest/IntegrationTestBase.java | 1 - .../integrationtest/spring/ScopeTest.java | 91 +++++++++++++++++-- .../util/ConfigurationMapperTest.java | 2 +- .../resolver/ConfigurationResolverTest.java | 9 +- 7 files changed, 95 insertions(+), 16 deletions(-) diff --git a/src/main/java/rocks/inspectit/gepard/agent/configuration/ConfigurationManager.java b/src/main/java/rocks/inspectit/gepard/agent/configuration/ConfigurationManager.java index cf32096..6f5aad8 100644 --- a/src/main/java/rocks/inspectit/gepard/agent/configuration/ConfigurationManager.java +++ b/src/main/java/rocks/inspectit/gepard/agent/configuration/ConfigurationManager.java @@ -44,7 +44,6 @@ public void loadConfiguration() { * configuration server url was set up. */ private void startHttpPolling(String serverUrl, ConfigurationPersistence persistence) { - log.info("Starting configuration polling from configuration server with url: {}", serverUrl); InspectitScheduler scheduler = InspectitScheduler.getInstance(); HttpConfigurationPoller poller = new HttpConfigurationPoller(serverUrl, persistence); Duration pollingInterval = PropertiesResolver.getPollingInterval(); diff --git a/src/test/java/rocks/inspectit/gepard/agent/instrumentation/cache/process/BatchInstrumenterTest.java b/src/test/java/rocks/inspectit/gepard/agent/instrumentation/cache/process/BatchInstrumenterTest.java index fcd3919..fed30de 100644 --- a/src/test/java/rocks/inspectit/gepard/agent/instrumentation/cache/process/BatchInstrumenterTest.java +++ b/src/test/java/rocks/inspectit/gepard/agent/instrumentation/cache/process/BatchInstrumenterTest.java @@ -39,7 +39,8 @@ void classIsRemovedFromCacheAndNotAddedToBatch() { Set> classes = new HashSet<>(); classes.add(TEST_CLASS); when(cache.getKeyIterator()).thenReturn(classes.iterator()); - when(configurationResolver.getClassInstrumentationConfiguration(TEST_CLASS)).thenReturn(configuration); + when(configurationResolver.getClassInstrumentationConfiguration(TEST_CLASS)) + .thenReturn(configuration); when(instrumentationState.shouldRetransform(TEST_CLASS, configuration)).thenReturn(false); BatchInstrumenter instrumenter = @@ -55,7 +56,8 @@ void classIsRemovedFromCacheAndAddedToBatch() { Set> classes = new HashSet<>(); classes.add(TEST_CLASS); when(cache.getKeyIterator()).thenReturn(classes.iterator()); - when(configurationResolver.getClassInstrumentationConfiguration(TEST_CLASS)).thenReturn(configuration); + when(configurationResolver.getClassInstrumentationConfiguration(TEST_CLASS)) + .thenReturn(configuration); when(instrumentationState.shouldRetransform(TEST_CLASS, configuration)).thenReturn(true); BatchInstrumenter instrumenter = diff --git a/src/test/java/rocks/inspectit/gepard/agent/integrationtest/ConfigurationServerMock.java b/src/test/java/rocks/inspectit/gepard/agent/integrationtest/ConfigurationServerMock.java index 1f91d52..48ec153 100644 --- a/src/test/java/rocks/inspectit/gepard/agent/integrationtest/ConfigurationServerMock.java +++ b/src/test/java/rocks/inspectit/gepard/agent/integrationtest/ConfigurationServerMock.java @@ -50,7 +50,6 @@ public void stop() { } public void configServerSetup(String config_path) throws IOException { - ClassLoader loader = getClass().getClassLoader(); File file = new File(loader.getResource(config_path).getFile()); String body = FileUtils.readFileToString(file, "UTF-8"); diff --git a/src/test/java/rocks/inspectit/gepard/agent/integrationtest/IntegrationTestBase.java b/src/test/java/rocks/inspectit/gepard/agent/integrationtest/IntegrationTestBase.java index cf8804a..5cf6d23 100644 --- a/src/test/java/rocks/inspectit/gepard/agent/integrationtest/IntegrationTestBase.java +++ b/src/test/java/rocks/inspectit/gepard/agent/integrationtest/IntegrationTestBase.java @@ -126,7 +126,6 @@ void reset() throws IOException { } protected void stopTarget() { - target.stop(); } diff --git a/src/test/java/rocks/inspectit/gepard/agent/integrationtest/spring/ScopeTest.java b/src/test/java/rocks/inspectit/gepard/agent/integrationtest/spring/ScopeTest.java index fd7d128..a16a704 100644 --- a/src/test/java/rocks/inspectit/gepard/agent/integrationtest/spring/ScopeTest.java +++ b/src/test/java/rocks/inspectit/gepard/agent/integrationtest/spring/ScopeTest.java @@ -2,9 +2,11 @@ import static org.junit.jupiter.api.Assertions.assertTrue; +import java.util.concurrent.TimeUnit; import okhttp3.Call; import okhttp3.Request; import org.junit.jupiter.api.Test; +import org.awaitility.Awaitility; public class ScopeTest extends SpringTestBase { @@ -76,6 +78,34 @@ void multipleScopesInstrumentAllSelectedMethods() throws Exception { assertLogs(logs, 4); } + @Test + void configurationUpdatesAreApplied() throws Exception { + // Set up config server to instrument multiple methods + configurationServerMock.configServerSetup( + "integrationtest/configurations/scope-with-multiple-methods.json"); + + startTarget("/opentelemetry-extensions.jar"); + 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"); + + awaitConfigurationUpdate(); + sendRequestToTarget("/greeting"); + + logs = target.getLogs(); + stopTarget(); + + // 2 logs before update + 1 log after update + assertLogs(logs, 3); + } + 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()); @@ -84,7 +114,38 @@ private void sendRequestToTarget(String path) throws Exception { call.execute(); } + /** + * Checks, if the logs contain "HELLO GEPARD" and "BYE GEPARD" for a specific number of times + * + * @param logs the logs + * @param times the amount of times "HELLO GEPARD" and "BYE GEPARD" should be present in the logs + */ + private void assertLogs(String logs, int times) { + boolean loggedHelloGepardTwice = containsTimes(logs, "HELLO GEPARD", times); + boolean loggedByeGepardTwice = containsTimes(logs, "BYE GEPARD", times); + + assertTrue(loggedHelloGepardTwice); + assertTrue(loggedByeGepardTwice); + } + + /** + * Checks, if a specific message can be found for a specific amount of times inside the provided logs. + * + * @return true, if the message appears the expected amount of times in the logs + */ private boolean containsTimes(String logs, String message, int times) { + int count = countTimes(logs, message); + return count == times; + } + + /** + * Counts how many times a specific message can be found inside the provided logs + * + * @param logs the logs + * @param message the message to look for + * @return the amount of times the message appears in the logs + */ + private int countTimes(String logs, String message) { int count = 0; int index = 0; while (index != -1) { @@ -94,14 +155,30 @@ private boolean containsTimes(String logs, String message, int times) { index += message.length(); } } - return count == times; + return count; } - private void assertLogs(String logs, int i) { - boolean loggedHelloGepardTwice = containsTimes(logs, "HELLO GEPARD", i); - boolean loggedByeGepardTwice = containsTimes(logs, "BYE GEPARD", i); - - assertTrue(loggedHelloGepardTwice); - assertTrue(loggedByeGepardTwice); + /** + * 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. + */ + private void awaitConfigurationUpdate() throws InterruptedException { + String updateMessage = + "Fetched configuration from configuration server and received status code 200"; + String logs = target.getLogs(); + int configUpdateCount = countTimes(logs, updateMessage); + + Awaitility.await() + .atMost(15, TimeUnit.SECONDS) + .until( + () -> { + String newLogs = target.getLogs(); + int currentConfigUpdateCount = countTimes(newLogs, updateMessage); + return currentConfigUpdateCount > configUpdateCount; + }); + + // Wait an additional second as puffer + Thread.sleep(1000); } } diff --git a/src/test/java/rocks/inspectit/gepard/agent/internal/configuration/util/ConfigurationMapperTest.java b/src/test/java/rocks/inspectit/gepard/agent/internal/configuration/util/ConfigurationMapperTest.java index 6e481b3..7a0cf51 100644 --- a/src/test/java/rocks/inspectit/gepard/agent/internal/configuration/util/ConfigurationMapperTest.java +++ b/src/test/java/rocks/inspectit/gepard/agent/internal/configuration/util/ConfigurationMapperTest.java @@ -62,7 +62,7 @@ private static String expectedString() { } private static InspectitConfiguration expectedConfig() { - Scope scope = new Scope(true,"com.example.Application", Collections.emptyList()); + Scope scope = new Scope(true, "com.example.Application", Collections.emptyList()); InstrumentationConfiguration instrumentationConfiguration = new InstrumentationConfiguration(List.of(scope)); return new InspectitConfiguration(instrumentationConfiguration); diff --git a/src/test/java/rocks/inspectit/gepard/agent/resolver/ConfigurationResolverTest.java b/src/test/java/rocks/inspectit/gepard/agent/resolver/ConfigurationResolverTest.java index d9f6968..1fdd6d9 100644 --- a/src/test/java/rocks/inspectit/gepard/agent/resolver/ConfigurationResolverTest.java +++ b/src/test/java/rocks/inspectit/gepard/agent/resolver/ConfigurationResolverTest.java @@ -40,7 +40,8 @@ void typeShouldNotBeInstrumented() { InspectitConfiguration configuration = new InspectitConfiguration(); when(holder.getConfiguration()).thenReturn(configuration); - ClassInstrumentationConfiguration config = resolver.getClassInstrumentationConfiguration(TEST_TYPE); + ClassInstrumentationConfiguration config = + resolver.getClassInstrumentationConfiguration(TEST_TYPE); boolean isActive = config.isActive(); assertFalse(isActive); @@ -52,7 +53,8 @@ void typeShouldBeInstrumented() { InspectitConfiguration configuration = createConfiguration(List.of(scope)); when(holder.getConfiguration()).thenReturn(configuration); - ClassInstrumentationConfiguration config = resolver.getClassInstrumentationConfiguration(TEST_TYPE); + ClassInstrumentationConfiguration config = + resolver.getClassInstrumentationConfiguration(TEST_TYPE); boolean isActive = config.isActive(); assertTrue(isActive); @@ -64,7 +66,8 @@ void typeShouldBeDeinstrumented() { InspectitConfiguration configuration = createConfiguration(List.of(scope)); when(holder.getConfiguration()).thenReturn(configuration); - ClassInstrumentationConfiguration config = resolver.getClassInstrumentationConfiguration(TEST_TYPE); + ClassInstrumentationConfiguration config = + resolver.getClassInstrumentationConfiguration(TEST_TYPE); boolean isActive = config.isActive(); assertFalse(isActive);