|
12 | 12 | import static io.opentelemetry.contrib.jmxscraper.config.JmxScraperConfig.JMX_REGISTRY_SSL; |
13 | 13 | import static io.opentelemetry.contrib.jmxscraper.config.JmxScraperConfig.JMX_REMOTE_PROFILE; |
14 | 14 | import static io.opentelemetry.contrib.jmxscraper.config.JmxScraperConfig.JMX_SERVICE_URL; |
| 15 | +import static io.opentelemetry.contrib.jmxscraper.config.JmxScraperConfig.JMX_TARGET_SOURCE; |
15 | 16 | import static io.opentelemetry.contrib.jmxscraper.config.JmxScraperConfig.JMX_TARGET_SYSTEM; |
16 | 17 | import static io.opentelemetry.contrib.jmxscraper.config.JmxScraperConfig.JMX_USERNAME; |
17 | 18 | import static io.opentelemetry.contrib.jmxscraper.config.JmxScraperConfig.fromConfig; |
18 | 19 | import static org.assertj.core.api.Assertions.assertThat; |
19 | 20 | import static org.assertj.core.api.Assertions.assertThatThrownBy; |
20 | 21 |
|
21 | 22 | import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException; |
| 23 | +import java.io.InputStream; |
22 | 24 | import java.time.Duration; |
| 25 | +import java.util.Locale; |
23 | 26 | import java.util.Properties; |
24 | 27 | import org.junit.jupiter.api.BeforeAll; |
25 | 28 | import org.junit.jupiter.api.Test; |
26 | 29 | import org.junit.jupiter.params.ParameterizedTest; |
| 30 | +import org.junit.jupiter.params.provider.EnumSource; |
27 | 31 | import org.junit.jupiter.params.provider.ValueSource; |
28 | 32 |
|
29 | 33 | class JmxScraperConfigTest { |
@@ -138,74 +142,93 @@ void shouldFailValidation_missingConfigPathAndTargetSystem() { |
138 | 142 | .hasMessage("at least one of 'otel.jmx.target.system' or 'otel.jmx.config' must be set"); |
139 | 143 | } |
140 | 144 |
|
141 | | - @Test |
142 | | - void shouldFailValidation_invalidTargetSystem() { |
| 145 | + @ParameterizedTest |
| 146 | + @EnumSource(JmxScraperConfig.TargetSystemSource.class) |
| 147 | + void shouldFailValidation_invalidTargetSystem(JmxScraperConfig.TargetSystemSource source) { |
143 | 148 | // Given |
144 | 149 | Properties properties = (Properties) validProperties.clone(); |
145 | 150 | properties.setProperty(JMX_TARGET_SYSTEM, "hal9000"); |
| 151 | + properties.setProperty(JMX_TARGET_SOURCE, source.name().toLowerCase(Locale.ROOT)); |
146 | 152 |
|
147 | 153 | // When and Then |
148 | 154 | assertThatThrownBy(() -> fromConfig(TestUtil.configProperties(properties))) |
149 | 155 | .isInstanceOf(ConfigurationException.class) |
150 | 156 | .hasMessageStartingWith("unsupported target system"); |
151 | 157 | } |
152 | 158 |
|
153 | | - // TODO: Tests below will be reimplemented |
154 | | - |
155 | | - // @Test |
156 | | - // @SetSystemProperty(key = "otel.jmx.service.url", value = "myServiceUrl") |
157 | | - // @SetSystemProperty(key = "javax.net.ssl.keyStorePassword", value = "truth") |
158 | | - // void propertiesFileOverride() { |
159 | | - // Properties props = new Properties(); |
160 | | - // JmxMetrics.loadPropertiesFromPath( |
161 | | - // props, ClassLoader.getSystemClassLoader().getResource("all.properties").getPath()); |
162 | | - // JmxConfig config = new JmxConfig(props); |
163 | | - // |
164 | | - // // This property should retain the system property value, not the config file value |
165 | | - // assertThat(config.serviceUrl).isEqualTo("myServiceUrl"); |
166 | | - // // These properties are set from the config file |
167 | | - // assertThat(config.groovyScript).isEqualTo("/my/groovy/script"); |
168 | | - // assertThat(config.targetSystem).isEqualTo("jvm,cassandra"); |
169 | | - // assertThat(config.targetSystems).containsOnly("jvm", "cassandra"); |
170 | | - // assertThat(config.intervalMilliseconds).isEqualTo(20000); |
171 | | - // assertThat(config.metricsExporterType).isEqualTo("otlp"); |
172 | | - // assertThat(config.otlpExporterEndpoint).isEqualTo("https://myotlpendpoint"); |
173 | | - // assertThat(config.prometheusExporterHost).isEqualTo("host123.domain.com"); |
174 | | - // assertThat(config.prometheusExporterPort).isEqualTo(67890); |
175 | | - // assertThat(config.username).isEqualTo("myUser\nname"); |
176 | | - // assertThat(config.password).isEqualTo("myPassw\\ord"); |
177 | | - // assertThat(config.remoteProfile).isEqualTo("SASL/DIGEST-MD5"); |
178 | | - // assertThat(config.realm).isEqualTo("myRealm"); |
179 | | - // |
180 | | - // // This property should retain the system property value, not the config file value |
181 | | - // assertThat(System.getProperty("javax.net.ssl.keyStorePassword")).isEqualTo("truth"); |
182 | | - // // These properties are set from the config file loading into JmxConfig |
183 | | - // assertThat(System.getProperty("javax.net.ssl.keyStore")).isEqualTo("/my/key/store"); |
184 | | - // assertThat(System.getProperty("javax.net.ssl.keyStoreType")).isEqualTo("JKS"); |
185 | | - // assertThat(System.getProperty("javax.net.ssl.trustStore")).isEqualTo("/my/trust/store"); |
186 | | - // assertThat(System.getProperty("javax.net.ssl.trustStorePassword")).isEqualTo("def456"); |
187 | | - // assertThat(System.getProperty("javax.net.ssl.trustStoreType")).isEqualTo("JKS"); |
188 | | - // } |
189 | | - // |
190 | | - // @Test |
191 | | - // @SetSystemProperty(key = "otel.jmx.service.url", value = "myServiceUrl") |
192 | | - // @SetSystemProperty(key = "otel.jmx.groovy.script", value = "myGroovyScript") |
193 | | - // @SetSystemProperty(key = "otel.jmx.target.system", value = "myTargetSystem") |
194 | | - // void canSupportScriptAndTargetSystem() { |
195 | | - // JmxConfig config = new JmxConfig(); |
196 | | - // |
197 | | - // assertThat(config.serviceUrl).isEqualTo("myServiceUrl"); |
198 | | - // assertThat(config.groovyScript).isEqualTo("myGroovyScript"); |
199 | | - // assertThat(config.targetSystem).isEqualTo("mytargetsystem"); |
200 | | - // assertThat(config.targetSystems).containsOnly("mytargetsystem"); |
201 | | - // } |
202 | | - // |
203 | | - // @Test |
204 | | - // @SetSystemProperty(key = "otel.metric.export.interval", value = "123") |
205 | | - // void otelMetricExportIntervalRespected() { |
206 | | - // JmxConfig config = new JmxConfig(); |
207 | | - // assertThat(config.intervalMilliseconds).isEqualTo(10000); |
208 | | - // assertThat(config.properties.getProperty("otel.metric.export.interval")).isEqualTo("123"); |
209 | | - // } |
210 | | - // |
| 159 | + @ParameterizedTest |
| 160 | + @ValueSource(strings = {"auto", ""}) |
| 161 | + void targetSystemSource_auto(String source) { |
| 162 | + Properties properties = (Properties) validProperties.clone(); |
| 163 | + properties.setProperty(JMX_TARGET_SYSTEM, "fake-test-system1"); |
| 164 | + if (!source.isEmpty()) { |
| 165 | + properties.setProperty(JMX_TARGET_SOURCE, source); |
| 166 | + } |
| 167 | + |
| 168 | + JmxScraperConfig config = fromConfig(TestUtil.configProperties(properties)); |
| 169 | + |
| 170 | + // should resolve to instrumentation when available in both |
| 171 | + shouldResolveToInstrumentationYaml(config, "fake-test-system1"); |
| 172 | + |
| 173 | + // should resolve to legacy yaml when not available in instrumentation |
| 174 | + shouldResolveToLegacyYaml(config, "fake-test-system2"); |
| 175 | + |
| 176 | + // should resolve to instrumentation when only defined there |
| 177 | + shouldResolveToInstrumentationYaml(config, "fake-test-system3"); |
| 178 | + } |
| 179 | + |
| 180 | + @Test |
| 181 | + void targetSystemSource_legacy() { |
| 182 | + Properties properties = (Properties) validProperties.clone(); |
| 183 | + properties.setProperty(JMX_TARGET_SYSTEM, "fake-test-system1"); |
| 184 | + properties.setProperty(JMX_TARGET_SOURCE, "legacy"); |
| 185 | + |
| 186 | + JmxScraperConfig config = fromConfig(TestUtil.configProperties(properties)); |
| 187 | + |
| 188 | + shouldResolveToLegacyYaml(config, "fake-test-system1"); |
| 189 | + |
| 190 | + shouldResolveToLegacyYaml(config, "fake-test-system2"); |
| 191 | + |
| 192 | + // should not support system only defined in instrumentation |
| 193 | + shouldNotResolveYaml(config, "fake-test-system3"); |
| 194 | + } |
| 195 | + |
| 196 | + @Test |
| 197 | + void targetSystemSource_instrumentation() { |
| 198 | + Properties properties = (Properties) validProperties.clone(); |
| 199 | + properties.setProperty(JMX_TARGET_SYSTEM, "fake-test-system1"); |
| 200 | + properties.setProperty(JMX_TARGET_SOURCE, "instrumentation"); |
| 201 | + |
| 202 | + JmxScraperConfig config = fromConfig(TestUtil.configProperties(properties)); |
| 203 | + |
| 204 | + shouldResolveToInstrumentationYaml(config, "fake-test-system1"); |
| 205 | + |
| 206 | + // should not support system only defined in legacy |
| 207 | + shouldNotResolveYaml(config, "fake-test-system2"); |
| 208 | + |
| 209 | + shouldResolveToInstrumentationYaml(config, "fake-test-system3"); |
| 210 | + } |
| 211 | + |
| 212 | + private static InputStream getYaml(String path) { |
| 213 | + return JmxScraperConfigTest.class.getClassLoader().getResourceAsStream(path); |
| 214 | + } |
| 215 | + |
| 216 | + private static void shouldResolveToInstrumentationYaml(JmxScraperConfig config, String target) { |
| 217 | + assertThat(config.getTargetSystemYaml(target)) |
| 218 | + .describedAs("should resolve to instrumentation yaml") |
| 219 | + .hasSameContentAs(getYaml("jmx/rules/" + target + ".yaml")); |
| 220 | + } |
| 221 | + |
| 222 | + private static void shouldResolveToLegacyYaml(JmxScraperConfig config, String target) { |
| 223 | + assertThat(config.getTargetSystemYaml(target)) |
| 224 | + .describedAs("should resolve to legacy yaml") |
| 225 | + .hasSameContentAs(getYaml(target + ".yaml")); |
| 226 | + } |
| 227 | + |
| 228 | + private static void shouldNotResolveYaml(JmxScraperConfig config, String target) { |
| 229 | + assertThatThrownBy(() -> config.getTargetSystemYaml(target)) |
| 230 | + .describedAs("should not support system") |
| 231 | + .isInstanceOf(ConfigurationException.class) |
| 232 | + .hasMessageStartingWith("unsupported target system"); |
| 233 | + } |
211 | 234 | } |
0 commit comments