From dcf963cebfc55496be08d6e9ae654b6e7c4c681a Mon Sep 17 00:00:00 2001 From: Scott Opell Date: Thu, 6 Jul 2023 20:48:23 +0000 Subject: [PATCH] Updates TestBeanSubscription to use container IP directly similar to ReconnectContainer --- .../jmxfetch/TestBeanSubscription.java | 72 +++++++++++++------ .../jmxfetch/TestReconnectContainer.java | 1 - 2 files changed, 50 insertions(+), 23 deletions(-) diff --git a/src/test/java/org/datadog/jmxfetch/TestBeanSubscription.java b/src/test/java/org/datadog/jmxfetch/TestBeanSubscription.java index 4500cfa2a..c30994578 100644 --- a/src/test/java/org/datadog/jmxfetch/TestBeanSubscription.java +++ b/src/test/java/org/datadog/jmxfetch/TestBeanSubscription.java @@ -11,9 +11,11 @@ import lombok.extern.slf4j.Slf4j; -import org.junit.Before; import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TestRule; +import org.junit.runners.model.Statement; +import org.junit.runner.Description; import org.testcontainers.containers.GenericContainer; import org.testcontainers.containers.output.Slf4jLogConsumer; import org.testcontainers.containers.wait.strategy.Wait; @@ -25,35 +27,56 @@ public class TestBeanSubscription extends TestCommon { private static final int rmiPort = 9090; private static final int controlPort = 9091; + private static final int supervisorPort = 9092; private JMXServerControlClient controlClient; + private JMXServerSupervisorClient supervisorClient; private static Slf4jLogConsumer logConsumer = new Slf4jLogConsumer(log); private static ImageFromDockerfile img = new ImageFromDockerfile() .withFileFromPath(".", Paths.get("./tools/misbehaving-jmx-server/")); - @Before - public void setup() { - this.controlClient = new JMXServerControlClient(cont.getHost(), cont.getMappedPort(controlPort)); - } - - @Rule + @Rule(order = 0) public GenericContainer cont = new GenericContainer<>(img) - .withExposedPorts(rmiPort, controlPort) .withEnv(Collections.singletonMap("RMI_PORT", "" + rmiPort)) .withEnv(Collections.singletonMap("CONTROL_PORT", "" + controlPort)) - .waitingFor(Wait.forLogMessage(".*IAMREADY.*", 1)); + .withEnv(Collections.singletonMap("SUPERVISOR_PORT", "" + supervisorPort)) + .waitingFor(Wait.forLogMessage(".*Supervisor HTTP Server Started. Waiting for initialization payload POST to /init.*", 1)); + + @Rule(order = 1) + public TestRule setupRule = new TestRule() { + @Override + public Statement apply(final Statement base, Description description) { + return new Statement() { + @Override + public void evaluate() throws Throwable { + String ipAddress = cont.getContainerInfo().getNetworkSettings().getIpAddress(); + controlClient = new JMXServerControlClient(ipAddress, controlPort); + supervisorClient = new JMXServerSupervisorClient(ipAddress, supervisorPort); + cont.followOutput(logConsumer); + try { + log.info("Initializing JMX Server with RMI hostname {}", ipAddress); + supervisorClient.initializeJMXServer(ipAddress); + } catch (IOException e) { + log.warn("Supervisor call to set rmi hostname failed, tests may fail in some environments, e: ", e); + } + base.evaluate(); + } + }; + } + }; @Test public void testJMXFetchBasic() throws IOException, InterruptedException { String testDomain = "test-domain"; + String ipAddress = cont.getContainerInfo().getNetworkSettings().getIpAddress(); this.initApplicationWithYamlLines( "init_config:", " is_jmx: true", "", "instances:", " - name: jmxint_container", - " host: " + cont.getHost(), - " port: " + cont.getMappedPort(rmiPort), + " host: " + ipAddress, + " port: " + rmiPort, " min_collection_interval: null", // allow collections at arbitrary intervals since we trigger them manually in the tests " enable_bean_subscription: true", " refresh_beans: 5000", // effectively disable bean refresh @@ -85,19 +108,20 @@ public void testJMXFetchBasic() throws IOException, InterruptedException { metrics = ((ConsoleReporter) this.appConfig.getReporter()).getMetrics(); assertEquals(numBeans * numAttributesPerBean, metrics.size()); } - + @Test public void testJMXFetchManyBeans() throws IOException, InterruptedException { cont.followOutput(logConsumer); String testDomain = "test-domain"; + String ipAddress = cont.getContainerInfo().getNetworkSettings().getIpAddress(); this.initApplicationWithYamlLines( "init_config:", " is_jmx: true", "", "instances:", " - name: jmxint_container", - " host: " + cont.getHost(), - " port: " + cont.getMappedPort(rmiPort), + " host: " + ipAddress, + " port: " + rmiPort, " min_collection_interval: null", " enable_bean_subscription: true", " refresh_beans: 5000", // effectively disable bean refresh @@ -134,14 +158,15 @@ public void testJMXFetchManyBeans() throws IOException, InterruptedException { public void testConcurrentCollectionWithSubscriptionUpdates() throws IOException, InterruptedException { String testDomain = "test-domain"; cont.followOutput(logConsumer); + String ipAddress = cont.getContainerInfo().getNetworkSettings().getIpAddress(); this.initApplicationWithYamlLines( "init_config:", " is_jmx: true", "", "instances:", " - name: jmxint_container", - " host: " + cont.getHost(), - " port: " + cont.getMappedPort(rmiPort), + " host: " + ipAddress, + " port: " + rmiPort, " min_collection_interval: null", " enable_bean_subscription: true", " refresh_beans: 5000", // effectively disable bean refresh @@ -196,14 +221,15 @@ public void testConcurrentCollectionWithSubscriptionUpdates() throws IOException public void testBeanRemoval() throws IOException, InterruptedException { String testDomain = "test-domain"; cont.followOutput(logConsumer); + String ipAddress = cont.getContainerInfo().getNetworkSettings().getIpAddress(); this.initApplicationWithYamlLines( "init_config:", " is_jmx: true", "", "instances:", " - name: jmxint_container", - " host: " + cont.getHost(), - " port: " + cont.getMappedPort(rmiPort), + " host: " + ipAddress, + " port: " + rmiPort, " min_collection_interval: null", " enable_bean_subscription: true", " refresh_beans: 5000", // effectively disable bean refresh @@ -255,14 +281,15 @@ public void testBeanRemoval() throws IOException, InterruptedException { public void testNetworkFailure() throws IOException, InterruptedException { String testDomain = "test-domain-nwkfail"; cont.followOutput(logConsumer); + String ipAddress = cont.getContainerInfo().getNetworkSettings().getIpAddress(); this.initApplicationWithYamlLines( "init_config:", " is_jmx: true", "", "instances:", " - name: jmxint_container", - " host: " + cont.getHost(), - " port: " + cont.getMappedPort(rmiPort), + " host: " + ipAddress, + " port: " + rmiPort, " min_collection_interval: null", " enable_bean_subscription: true", " refresh_beans: 5000", // effectively disable bean refresh @@ -310,14 +337,15 @@ public void testNetworkFailure() throws IOException, InterruptedException { public void testDisconnectDuringBeanCreation() throws IOException, InterruptedException { String testDomain = "test-domain-dsc-bn-creat"; cont.followOutput(logConsumer); + String ipAddress = cont.getContainerInfo().getNetworkSettings().getIpAddress(); this.initApplicationWithYamlLines( "init_config:", " is_jmx: true", "", "instances:", " - name: jmxint_container", - " host: " + cont.getHost(), - " port: " + cont.getMappedPort(rmiPort), + " host: " + ipAddress, + " port: " + rmiPort, " min_collection_interval: null", " enable_bean_subscription: true", " refresh_beans: 5000", // effectively disable bean refresh diff --git a/src/test/java/org/datadog/jmxfetch/TestReconnectContainer.java b/src/test/java/org/datadog/jmxfetch/TestReconnectContainer.java index 10e886cb2..e761c5ca1 100644 --- a/src/test/java/org/datadog/jmxfetch/TestReconnectContainer.java +++ b/src/test/java/org/datadog/jmxfetch/TestReconnectContainer.java @@ -12,7 +12,6 @@ import javax.management.remote.JMXConnectorFactory; import javax.management.remote.JMXServiceURL; -import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TestRule;