Skip to content

Commit

Permalink
Updates TestBeanSubscription to use container IP directly similar to …
Browse files Browse the repository at this point in the history
…ReconnectContainer
  • Loading branch information
scottopell committed Jul 6, 2023
1 parent 79ce047 commit dcf963c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 23 deletions.
72 changes: 50 additions & 22 deletions src/test/java/org/datadog/jmxfetch/TestBeanSubscription.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit dcf963c

Please sign in to comment.