Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
robsunday committed Sep 18, 2024
1 parent 8dfe174 commit 350f8a9
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
public class JmxScraperConfigFactory {
private static final String PREFIX = "otel.";
static final String SERVICE_URL = PREFIX + "jmx.service.url";
static final String CUSTOM_JMX_SCRAPING_CONFIG = PREFIX + "jmx.custom.jmx.scraping.config";
static final String CUSTOM_JMX_SCRAPING_CONFIG = PREFIX + "jmx.custom.scraping.config";
static final String TARGET_SYSTEM = PREFIX + "jmx.target.system";
static final String INTERVAL_MILLISECONDS = PREFIX + "jmx.interval.milliseconds";
static final String METRICS_EXPORTER_TYPE = PREFIX + "metrics.exporter";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@

package io.opentelemetry.contrib.jmxscraper;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.mock;

import io.opentelemetry.contrib.jmxscraper.config.ConfigurationException;
import io.opentelemetry.contrib.jmxscraper.config.JmxScraperConfig;
import io.opentelemetry.contrib.jmxscraper.config.JmxScraperConfigFactory;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
Expand All @@ -18,7 +23,7 @@ class JmxScraperTest {
@Test
void shouldThrowExceptionWhenInvalidCommandLineArgsProvided() {
// Given
List<String> emptyArgs = Collections.singletonList("-inexistingOption");
List<String> emptyArgs = Collections.singletonList("-nonExistentOption");
JmxScraperConfigFactory configFactoryMock = mock(JmxScraperConfigFactory.class);

// When and Then
Expand All @@ -29,11 +34,46 @@ void shouldThrowExceptionWhenInvalidCommandLineArgsProvided() {
@Test
void shouldThrowExceptionWhenTooManyCommandLineArgsProvided() {
// Given
List<String> emptyArgs = Arrays.asList("-config", "path", "-inexistingOption");
List<String> args = Arrays.asList("-config", "path", "-nonExistentOption");
JmxScraperConfigFactory configFactoryMock = mock(JmxScraperConfigFactory.class);

// When and Then
assertThatThrownBy(() -> JmxScraper.createConfigFromArgs(emptyArgs, configFactoryMock))
assertThatThrownBy(() -> JmxScraper.createConfigFromArgs(args, configFactoryMock))
.isInstanceOf(ArgumentsParsingException.class);
}

@Test
void shouldCreateConfig_propertiesLoadedFromFile()
throws ConfigurationException, ArgumentsParsingException {
// Given
String filePath = ClassLoader.getSystemClassLoader().getResource("validConfig.properties").getPath();
List<String> args = Arrays.asList("-config", filePath);
JmxScraperConfigFactory configFactory = new JmxScraperConfigFactory();

// When
JmxScraperConfig config = JmxScraper.createConfigFromArgs(args, configFactory);

// Then
assertThat(config).isNotNull();
}

@Test
void shouldCreateConfig_propertiesLoadedFromStdIn()
throws ConfigurationException, ArgumentsParsingException, IOException {
InputStream originalIn = System.in;
try(InputStream stream = ClassLoader.getSystemClassLoader().getResourceAsStream("validConfig.properties")) {
// Given
System.setIn(stream);
List<String> args = Arrays.asList("-config", "-");
JmxScraperConfigFactory configFactory = new JmxScraperConfigFactory();

// When
JmxScraperConfig config = JmxScraper.createConfigFromArgs(args, configFactory);

// Then
assertThat(config).isNotNull();
} finally {
System.setIn(originalIn);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ void shouldFailValidation_missingConfigPathAndTargetSystem() {
assertThatThrownBy(() -> configFactory.createConfig(properties))
.isInstanceOf(ConfigurationException.class)
.hasMessage(
"otel.jmx.custom.jmx.scraping.config or otel.jmx.target.system must be specified.");
"otel.jmx.custom.scraping.config or otel.jmx.target.system must be specified.");
}

@Test
Expand Down Expand Up @@ -180,6 +180,36 @@ void shouldFailValidation_missingOtlpEndpoint() {
.hasMessage("otel.exporter.otlp.endpoint must be specified for otlp format.");
}

@Test
void shouldPassValidation_noMetricsExporterType() throws ConfigurationException {
// Given
JmxScraperConfigFactory configFactory = new JmxScraperConfigFactory();
Properties properties = (Properties) validProperties.clone();
properties.remove(JmxScraperConfigFactory.OTLP_ENDPOINT);
properties.remove(JmxScraperConfigFactory.METRICS_EXPORTER_TYPE);

// When
JmxScraperConfig config = configFactory.createConfig(properties);

// Then
assertThat(config).isNotNull();
}

@Test
void shouldPassValidation_nonOtlpMetricsExporterType() throws ConfigurationException {
// Given
JmxScraperConfigFactory configFactory = new JmxScraperConfigFactory();
Properties properties = (Properties) validProperties.clone();
properties.remove(JmxScraperConfigFactory.OTLP_ENDPOINT);
properties.setProperty(JmxScraperConfigFactory.METRICS_EXPORTER_TYPE, "logging");

// When
JmxScraperConfig config = configFactory.createConfig(properties);

// Then
assertThat(config).isNotNull();
}

@Test
void shouldFailValidation_negativeInterval() {
// Given
Expand Down
20 changes: 20 additions & 0 deletions jmx-scraper/src/test/resources/validConfig.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
otel.jmx.service.url=service:jmx:rmi:///jndi/rmi://myhost:12345/jmxrmi
otel.jmx.custom.scraping.config=/my/scraping-config.yaml
otel.jmx.target.system=jvm,cassandra
otel.jmx.interval.milliseconds=20000
otel.metrics.exporter=otlp
otel.metric.export.interval=1000
otel.exporter.otlp.endpoint=https://myotlpendpoint
otel.jmx.username=myUser\n\
name
otel.jmx.password=myPassw\\ord
otel.jmx.remote.profile=SASL/DIG\EST-MD5
otel.jmx.realm=myRealm
otel.resource.attributes=one=two,three=four
javax.net.ssl.keyStore=/my/key/store
javax.net.ssl.keyStorePassword=abc123
javax.net.ssl.keyStoreType=JKS
javax.net.ssl.trustStore=/my/trust/store
javax.net.ssl.trustStorePassword=def456
javax.net.ssl.trustStoreType=JKS
otel.jmx.aggregate.across.mbeans=true

0 comments on commit 350f8a9

Please sign in to comment.