Skip to content

Commit a197007

Browse files
authored
Added integration test using standalone exporter and environment variables (#1333)
Signed-off-by: dhoard <doug.hoard@gmail.com>
1 parent c4d8b3a commit a197007

File tree

7 files changed

+299
-0
lines changed

7 files changed

+299
-0
lines changed
Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
/*
2+
* Copyright (C) The Prometheus jmx_exporter Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.prometheus.jmx.test.rmi.ssl;
18+
19+
import static io.prometheus.jmx.test.support.http.HttpResponse.assertHealthyResponse;
20+
import static io.prometheus.jmx.test.support.metrics.MetricAssertion.assertMetric;
21+
import static io.prometheus.jmx.test.support.metrics.MetricAssertion.assertMetricsContentType;
22+
23+
import io.prometheus.jmx.test.support.environment.JmxExporterMode;
24+
import io.prometheus.jmx.test.support.environment.JmxExporterPath;
25+
import io.prometheus.jmx.test.support.environment.JmxExporterTestEnvironment;
26+
import io.prometheus.jmx.test.support.http.HttpClient;
27+
import io.prometheus.jmx.test.support.http.HttpHeader;
28+
import io.prometheus.jmx.test.support.http.HttpResponse;
29+
import io.prometheus.jmx.test.support.metrics.Metric;
30+
import io.prometheus.jmx.test.support.metrics.MetricsContentType;
31+
import io.prometheus.jmx.test.support.metrics.MetricsParser;
32+
import io.prometheus.jmx.test.support.util.TestSupport;
33+
import java.io.IOException;
34+
import java.util.ArrayList;
35+
import java.util.Collection;
36+
import java.util.List;
37+
import java.util.stream.Stream;
38+
import org.testcontainers.containers.Network;
39+
import org.verifyica.api.ArgumentContext;
40+
import org.verifyica.api.Trap;
41+
import org.verifyica.api.Verifyica;
42+
43+
public class EnvironmentVariableRMISSLTest {
44+
45+
@Verifyica.ArgumentSupplier(parallelism = Integer.MAX_VALUE)
46+
public static Stream<JmxExporterTestEnvironment> arguments() {
47+
// Filter the arguments..
48+
//
49+
// 1. only run the Standalone exporter
50+
// 2. filter out the GraalVM 1.8 JVM - exception is that SunJSSE is not found
51+
// 3. filter out all ibmjava* JVMs - exception is that SunJSSE is not found
52+
//
53+
return JmxExporterTestEnvironment.createEnvironments()
54+
.filter(
55+
exporterTestEnvironment ->
56+
exporterTestEnvironment.getJmxExporterMode()
57+
== JmxExporterMode.Standalone)
58+
.filter(
59+
exporterTestEnvironment ->
60+
!exporterTestEnvironment
61+
.getJavaDockerImage()
62+
.contains("graalvm/jdk:java8"))
63+
.filter(
64+
exporterTestEnvironment ->
65+
!exporterTestEnvironment.getJavaDockerImage().contains("ibmjava"));
66+
}
67+
68+
@Verifyica.BeforeAll
69+
public void beforeAll(ArgumentContext argumentContext) {
70+
Class<?> testClass = argumentContext.classContext().testClass();
71+
Network network = TestSupport.getOrCreateNetwork(argumentContext);
72+
TestSupport.initializeExporterTestEnvironment(argumentContext, network, testClass);
73+
}
74+
75+
@Verifyica.Test
76+
@Verifyica.Order(1)
77+
public void testHealthy(JmxExporterTestEnvironment jmxExporterTestEnvironment)
78+
throws IOException {
79+
String url = jmxExporterTestEnvironment.getUrl(JmxExporterPath.HEALTHY);
80+
81+
HttpResponse httpResponse = HttpClient.sendRequest(url);
82+
83+
assertHealthyResponse(httpResponse);
84+
}
85+
86+
@Verifyica.Test
87+
public void testDefaultTextMetrics(JmxExporterTestEnvironment jmxExporterTestEnvironment)
88+
throws IOException {
89+
String url = jmxExporterTestEnvironment.getUrl(JmxExporterPath.METRICS);
90+
91+
HttpResponse httpResponse = HttpClient.sendRequest(url);
92+
93+
assertMetricsResponse(jmxExporterTestEnvironment, httpResponse, MetricsContentType.DEFAULT);
94+
}
95+
96+
@Verifyica.Test
97+
public void testOpenMetricsTextMetrics(JmxExporterTestEnvironment jmxExporterTestEnvironment)
98+
throws IOException {
99+
String url = jmxExporterTestEnvironment.getUrl(JmxExporterPath.METRICS);
100+
101+
HttpResponse httpResponse =
102+
HttpClient.sendRequest(
103+
url,
104+
HttpHeader.ACCEPT,
105+
MetricsContentType.OPEN_METRICS_TEXT_METRICS.toString());
106+
107+
assertMetricsResponse(
108+
jmxExporterTestEnvironment,
109+
httpResponse,
110+
MetricsContentType.OPEN_METRICS_TEXT_METRICS);
111+
}
112+
113+
@Verifyica.Test
114+
public void testPrometheusTextMetrics(JmxExporterTestEnvironment jmxExporterTestEnvironment)
115+
throws IOException {
116+
String url = jmxExporterTestEnvironment.getUrl(JmxExporterPath.METRICS);
117+
118+
HttpResponse httpResponse =
119+
HttpClient.sendRequest(
120+
url,
121+
HttpHeader.ACCEPT,
122+
MetricsContentType.PROMETHEUS_TEXT_METRICS.toString());
123+
124+
assertMetricsResponse(
125+
jmxExporterTestEnvironment,
126+
httpResponse,
127+
MetricsContentType.PROMETHEUS_TEXT_METRICS);
128+
}
129+
130+
@Verifyica.Test
131+
public void testPrometheusProtobufMetrics(JmxExporterTestEnvironment jmxExporterTestEnvironment)
132+
throws IOException {
133+
String url = jmxExporterTestEnvironment.getUrl(JmxExporterPath.METRICS);
134+
135+
HttpResponse httpResponse =
136+
HttpClient.sendRequest(
137+
url,
138+
HttpHeader.ACCEPT,
139+
MetricsContentType.PROMETHEUS_PROTOBUF_METRICS.toString());
140+
141+
assertMetricsResponse(
142+
jmxExporterTestEnvironment,
143+
httpResponse,
144+
MetricsContentType.PROMETHEUS_PROTOBUF_METRICS);
145+
}
146+
147+
@Verifyica.AfterAll
148+
public void afterAll(ArgumentContext argumentContext) throws Throwable {
149+
List<Trap> traps = new ArrayList<>();
150+
151+
traps.add(new Trap(() -> TestSupport.destroyExporterTestEnvironment(argumentContext)));
152+
traps.add(new Trap(() -> TestSupport.destroyNetwork(argumentContext)));
153+
154+
Trap.assertEmpty(traps);
155+
}
156+
157+
private void assertMetricsResponse(
158+
JmxExporterTestEnvironment jmxExporterTestEnvironment,
159+
HttpResponse httpResponse,
160+
MetricsContentType metricsContentType) {
161+
assertMetricsContentType(httpResponse, metricsContentType);
162+
163+
Collection<Metric> metrics = MetricsParser.parseCollection(httpResponse);
164+
165+
boolean isJmxExporterModeJavaAgent =
166+
jmxExporterTestEnvironment.getJmxExporterMode() == JmxExporterMode.JavaAgent;
167+
168+
String buildInfoName =
169+
TestSupport.getBuildInfoName(jmxExporterTestEnvironment.getJmxExporterMode());
170+
171+
assertMetric(metrics)
172+
.ofType(Metric.Type.GAUGE)
173+
.withName("jmx_exporter_build_info")
174+
.withLabel("name", buildInfoName)
175+
.withValue(1d)
176+
.isPresent();
177+
178+
assertMetric(metrics)
179+
.ofType(Metric.Type.GAUGE)
180+
.withName("jmx_scrape_error")
181+
.withValue(0d)
182+
.isPresent();
183+
184+
assertMetric(metrics)
185+
.ofType(Metric.Type.COUNTER)
186+
.withName("jmx_config_reload_success_total")
187+
.withValue(0d)
188+
.isPresent();
189+
190+
assertMetric(metrics)
191+
.ofType(Metric.Type.GAUGE)
192+
.withName("jvm_memory_used_bytes")
193+
.withLabel("area", "nonheap")
194+
.isPresentWhen(isJmxExporterModeJavaAgent);
195+
196+
assertMetric(metrics)
197+
.ofType(Metric.Type.GAUGE)
198+
.withName("jvm_memory_used_bytes")
199+
.withLabel("area", "heap")
200+
.isPresentWhen(isJmxExporterModeJavaAgent);
201+
202+
assertMetric(metrics)
203+
.ofType(Metric.Type.GAUGE)
204+
.withName("jvm_memory_used_bytes")
205+
.withLabel("area", "nonheap")
206+
.isPresentWhen(isJmxExporterModeJavaAgent);
207+
208+
assertMetric(metrics)
209+
.ofType(Metric.Type.GAUGE)
210+
.withName("jvm_memory_used_bytes")
211+
.withLabel("area", "heap")
212+
.isPresentWhen(isJmxExporterModeJavaAgent);
213+
214+
assertMetric(metrics)
215+
.ofType(Metric.Type.UNTYPED)
216+
.withName("io_prometheus_jmx_tabularData_Server_1_Disk_Usage_Table_size")
217+
.withLabel("source", "/dev/sda1")
218+
.withValue(7.516192768E9d)
219+
.isPresent();
220+
221+
assertMetric(metrics)
222+
.ofType(Metric.Type.UNTYPED)
223+
.withName("io_prometheus_jmx_tabularData_Server_2_Disk_Usage_Table_pcent")
224+
.withLabel("source", "/dev/sda2")
225+
.withValue(0.8d)
226+
.isPresent();
227+
}
228+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/bin/bash
2+
3+
#
4+
# Code to run the test with RedHat UBI images
5+
#
6+
# When running on RedHat UBI images, testcontainers maps
7+
# the files as the current user, but the application runs
8+
# as "jboss" on UBI8 images and "default" on UBI9 images
9+
# preventing the chmod commands to change permissions on
10+
# the jmxremote.access and jmxremote.password files.
11+
#
12+
# The code copies the files to /tmp as the current user
13+
# then performs a chmod to change permissions.
14+
#
15+
16+
JMXREMOTE_ACCESS=jmxremote.access
17+
JMXREMOTE_PASSWORD=jmxremote.password
18+
19+
WHOAMI=$(whoami)
20+
if [ "${WHOAMI}" = "jboss" ] || [ "${WHOAMI}" = "default" ];
21+
then
22+
cp ${JMXREMOTE_ACCESS} /tmp/jmxremote.access
23+
cp ${JMXREMOTE_PASSWORD} /tmp/jmxremote.password
24+
chmod go-rwx /tmp/jmxremote.access
25+
chmod go-rwx /tmp/jmxremote.password
26+
JMXREMOTE_ACCESS=/tmp/jmxremote.access
27+
JMXREMOTE_PASSWORD=/tmp/jmxremote.password
28+
else
29+
chmod go-rwx jmxremote.access
30+
chmod go-rwx jmxremote.password
31+
fi
32+
33+
java \
34+
-Xmx512M \
35+
-Dcom.sun.management.jmxremote=true \
36+
-Dcom.sun.management.jmxremote.authenticate=true \
37+
-Dcom.sun.management.jmxremote.password.file=${JMXREMOTE_PASSWORD} \
38+
-Dcom.sun.management.jmxremote.port=9999 \
39+
-Dcom.sun.management.jmxremote.registry.ssl=true \
40+
-Dcom.sun.management.jmxremote.access.file=${JMXREMOTE_ACCESS} \
41+
-Dcom.sun.management.jmxremote.rmi.port=9999 \
42+
-Dcom.sun.management.jmxremote.ssl=true \
43+
-Djavax.net.ssl.keyStore=localhost.pkcs12 \
44+
-Djavax.net.ssl.keyStorePassword=changeit \
45+
-Djavax.net.ssl.keyStoreType=pkcs12 \
46+
-Djavax.net.ssl.trustStore=localhost.pkcs12 \
47+
-Djavax.net.ssl.trustStorePassword=changeit \
48+
-Djavax.net.ssl.trustStoreType=pkcs12 \
49+
-jar jmx_example_application.jar
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
3+
export USERNAME=Prometheus
4+
export PASSWORD=secret
5+
6+
java \
7+
-Xmx512M \
8+
-Djavax.net.ssl.keyStore=localhost.pkcs12 \
9+
-Djavax.net.ssl.keyStorePassword=changeit \
10+
-Djavax.net.ssl.keyStoreType=pkcs12 \
11+
-Djavax.net.ssl.trustStore=localhost.pkcs12 \
12+
-Djavax.net.ssl.trustStorePassword=changeit \
13+
-Djavax.net.ssl.trustStoreType=pkcs12 \
14+
-jar jmx_prometheus_standalone.jar 8888 exporter.yaml
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
hostPort: application:9999
2+
ssl: true
3+
username: ${USERNAME}
4+
password: ${PASSWORD}
5+
rules:
6+
- pattern: ".*"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Prometheus readonly
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Prometheus secret

0 commit comments

Comments
 (0)