From 23cdbf4a455892513ba0b423d85a877ea64788a2 Mon Sep 17 00:00:00 2001 From: Carlos Roman Date: Fri, 31 May 2024 16:09:14 +0100 Subject: [PATCH] Removing src/test/java/org/datadog/jmxfetch/TestInstance.java --- .../org/datadog/jmxfetch/TestInstance.java | 240 ------------------ 1 file changed, 240 deletions(-) delete mode 100644 src/test/java/org/datadog/jmxfetch/TestInstance.java diff --git a/src/test/java/org/datadog/jmxfetch/TestInstance.java b/src/test/java/org/datadog/jmxfetch/TestInstance.java deleted file mode 100644 index 4fa8adf55..000000000 --- a/src/test/java/org/datadog/jmxfetch/TestInstance.java +++ /dev/null @@ -1,240 +0,0 @@ -package org.datadog.jmxfetch; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -import java.net.URL; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import java.util.Map; - -import org.junit.Test; - -public class TestInstance extends TestCommon { - private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger("Test Instance"); - - @Test - public void testMinCollectionInterval() throws Exception { - registerMBean(new SimpleTestJavaApp(), "org.datadog.jmxfetch.test:foo=Bar,qux=Baz"); - initApplication("jmx_min_collection_period.yml"); - - run(); - List> metrics = getMetrics(); - assertEquals(15, metrics.size()); - - run(); - metrics = getMetrics(); - assertEquals(0, metrics.size()); - - log.info("sleeping before the next collection"); - Thread.sleep(5000); - run(); - metrics = getMetrics(); - assertEquals(15, metrics.size()); - } - - // assertHostnameTags is used by testEmptyDefaultHostname - private void assertHostnameTags(List tagList) throws Exception { - // Fixed instance tag - assertTrue(tagList.contains(new String("jmx:fetch"))); - - if (tagList.contains(new String("instance:jmx_test_default_hostname"))) { - // Nominal case - assertFalse(tagList.contains(new String("host:"))); - } else if (tagList.contains(new String("instance:jmx_test_no_hostname"))) { - // empty_default_hostname case - assertTrue(tagList.contains(new String("host:"))); - } else { - fail("unexpected instance tag"); - } - } - - @Test - public void testEmptyDefaultHostname() throws Exception { - registerMBean(new SimpleTestJavaApp(), "org.datadog.jmxfetch.test:foo=Bar,qux=Baz"); - initApplication("jmx_empty_default_hostname.yaml"); - run(); - - List> metrics = getMetrics(); - assertEquals(14, metrics.size()); - for (Map metric : metrics) { - String[] tags = (String[]) metric.get("tags"); - this.assertHostnameTags(Arrays.asList(tags)); - } - - List> serviceChecks = getServiceChecks(); - assertEquals(2, serviceChecks.size()); - for (Map sc : serviceChecks) { - String[] tags = (String[]) sc.get("tags"); - this.assertHostnameTags(Arrays.asList(tags)); - } - } - - @Test - public void testBaselineDefaultHostname() throws Exception { - registerMBean(new SimpleTestJavaApp(), "org.datadog.jmxfetch.test:foo=Bar,qux=Baz"); - initApplication("jmx_baseline_default_hostname.yaml"); - run(); - - List> metrics = getMetrics(); - assertEquals(14, metrics.size()); - for (Map metric : metrics) { - String[] tags = (String[]) metric.get("tags"); - this.assertHostnameTags(Arrays.asList(tags)); - } - - List> serviceChecks = getServiceChecks(); - assertEquals(2, serviceChecks.size()); - for (Map sc : serviceChecks) { - String[] tags = (String[]) sc.get("tags"); - this.assertHostnameTags(Arrays.asList(tags)); - } - } - - // assertServiceTags is used by testServiceTagGlobal and testServiceTagInstanceOverride - private void assertServiceTag(List tagList, List services) throws Exception { - for (String service: services) { - assertTrue(tagList.contains(new String("service:" + service))); - } - } - - @Test - public void testServiceTagGlobal() throws Exception { - registerMBean(new SimpleTestJavaApp(), "org.datadog.jmxfetch.test:foo=Bar,qux=Baz"); - initApplication("jmx_service_tag_global.yaml"); - run(); - - List> metrics = getMetrics(); - assertEquals(28, metrics.size()); - for (Map metric : metrics) { - String[] tags = (String[]) metric.get("tags"); - this.assertServiceTag(Arrays.asList(tags), Arrays.asList("global")); - } - - List> serviceChecks = getServiceChecks(); - assertEquals(4, serviceChecks.size()); - for (Map sc : serviceChecks) { - String[] tags = (String[]) sc.get("tags"); - this.assertServiceTag(Arrays.asList(tags), Arrays.asList("global")); - } - } - - @Test - public void testServiceTagGlobalList() throws Exception { - registerMBean(new SimpleTestJavaApp(), "org.datadog.jmxfetch.test:foo=Bar,qux=Baz"); - initApplication("jmx_service_tag_global_list.yaml"); - run(); - - List> metrics = getMetrics(); - assertEquals(28, metrics.size()); - for (Map metric : metrics) { - String[] tags = (String[]) metric.get("tags"); - this.assertServiceTag(Arrays.asList(tags), Arrays.asList("global", "foo", "bar")); - } - - List> serviceChecks = getServiceChecks(); - assertEquals(4, serviceChecks.size()); - for (Map sc : serviceChecks) { - String[] tags = (String[]) sc.get("tags"); - this.assertServiceTag(Arrays.asList(tags), Arrays.asList("global", "foo", "bar")); - } - } - - @Test - public void testServiceTagInstanceOverride() throws Exception { - registerMBean(new SimpleTestJavaApp(), "org.datadog.jmxfetch.test:foo=Bar,qux=Baz"); - initApplication("jmx_service_tag_instance_override.yaml"); - run(); - - List> metrics = getMetrics(); - assertEquals(28, metrics.size()); - for (Map metric : metrics) { - String[] tags = (String[]) metric.get("tags"); - this.assertServiceTag(Arrays.asList(tags), Arrays.asList("override")); - } - - List> serviceChecks = getServiceChecks(); - assertEquals(4, serviceChecks.size()); - for (Map sc : serviceChecks) { - String[] tags = (String[]) sc.get("tags"); - this.assertServiceTag(Arrays.asList(tags), Arrays.asList("override")); - } - } - - @Test - public void testLoadMetricConfigFiles() throws Exception { - URL defaultConfig = Instance.class.getResource("default-jmx-metrics.yaml"); - AppConfig config = mock(AppConfig.class); - when(config.getMetricConfigFiles()).thenReturn(Collections.singletonList(defaultConfig.getPath())); - List configurationList = new ArrayList(); - Instance.loadMetricConfigFiles(config, configurationList); - - assertEquals(2, configurationList.size()); - } - - @Test - public void testLoadMetricConfigResources() throws Exception { - URL defaultConfig = Instance.class.getResource("sample-metrics.yaml"); - String configResource = defaultConfig.getPath().split("test-classes/")[1]; - AppConfig config = mock(AppConfig.class); - when(config.getMetricConfigResources()).thenReturn(Collections.singletonList(configResource)); - List configurationList = new ArrayList(); - Instance.loadMetricConfigResources(config, configurationList); - - assertEquals(2, configurationList.size()); - } - - /** Tests refresh_beans_initial and the following refresh_beans */ - @Test - public void testRefreshBeans() throws Exception { - SimpleTestJavaApp testApp = new SimpleTestJavaApp(); - initApplication("jmx_refresh_beans.yaml"); - - // We do a first collection - run(); - List> metrics = getMetrics(); - - // 13 metrics from java.lang - assertEquals(13, metrics.size()); - - // We register an additional mbean - registerMBean(testApp, "org.datadog.jmxfetch.test:iteration=one"); - log.info("sleeping before the next collection"); - Thread.sleep(1500); - - // We run a second collection. refresh_beans_initial should be due. - run(); - metrics = getMetrics(); - - // 15 = 13 metrics from java.lang + 2 iteration=one - assertEquals(15, metrics.size()); - - // We register additional mbean - registerMBean(testApp, "org.datadog.jmxfetch.test:iteration=two"); - log.info("sleeping before the next collection"); - Thread.sleep(1500); - - // We run a third collection. No change expected; refresh_beans not due. - run(); - metrics = getMetrics(); - - // 15 = 13 metrics from java.lang + 2 iteration=one - assertEquals(15, metrics.size()); - - log.info("sleeping before the next collection"); - Thread.sleep(1500); - - // We run the last collection. refresh_beans should be due. - run(); - metrics = getMetrics(); - - // 17 = 13 metrics from java.lang + 2 iteration=one + 2 iteration=two - assertEquals(17, metrics.size()); - } -}