Skip to content

Commit b33f495

Browse files
authored
chore: Added integration tests (#1485)
Signed-off-by: dhoard <doug.hoard@gmail.com>
1 parent 90263af commit b33f495

515 files changed

Lines changed: 23253 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 339 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,339 @@
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+
* https://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.core;
18+
19+
import static io.prometheus.jmx.test.support.metrics.MetricAssertion.assertMetric;
20+
import static io.prometheus.jmx.test.support.metrics.MetricAssertion.assertMetricsContentType;
21+
import static org.assertj.core.api.Assertions.assertThat;
22+
import static org.paramixel.api.Context.withInstance;
23+
24+
import io.prometheus.jmx.test.support.environment.JmxExporterMode;
25+
import io.prometheus.jmx.test.support.environment.JmxExporterPath;
26+
import io.prometheus.jmx.test.support.environment.JmxExporterTestEnvironment;
27+
import io.prometheus.jmx.test.support.environment.NetworkSupport;
28+
import io.prometheus.jmx.test.support.http.HttpClient;
29+
import io.prometheus.jmx.test.support.http.HttpHeader;
30+
import io.prometheus.jmx.test.support.http.HttpRequest;
31+
import io.prometheus.jmx.test.support.http.HttpResponse;
32+
import io.prometheus.jmx.test.support.metrics.Metric;
33+
import io.prometheus.jmx.test.support.metrics.MetricsContentType;
34+
import io.prometheus.jmx.test.support.metrics.MetricsParser;
35+
import java.io.IOException;
36+
import java.util.Collection;
37+
import java.util.Map;
38+
import java.util.stream.Collectors;
39+
import org.paramixel.api.Paramixel;
40+
import org.paramixel.api.Runner;
41+
import org.paramixel.api.action.Action;
42+
import org.paramixel.api.action.Each;
43+
import org.paramixel.api.action.Instance;
44+
import org.paramixel.api.action.Scope;
45+
import org.paramixel.api.action.Sequence;
46+
import org.paramixel.api.action.Step;
47+
import org.testcontainers.containers.Network;
48+
49+
public class AllLowerCaseWithHttpAuthSSLTest {
50+
51+
private static final String BASE_URL = "https://localhost";
52+
53+
private static final String VALID_USERNAME = "Prometheus";
54+
55+
private static final String VALID_PASSWORD = "secret";
56+
57+
private static final String[] TEST_USERNAMES = new String[] {VALID_USERNAME, "prometheus", "bad", "", null};
58+
59+
private static final String[] TEST_PASSWORDS = new String[] {VALID_PASSWORD, "Secret", "bad", "", null};
60+
61+
private final JmxExporterTestEnvironment environment;
62+
63+
private Network network;
64+
65+
public static void main(String[] args) throws Throwable {
66+
Runner.defaultRunner().runAndExit(factory());
67+
}
68+
69+
@Paramixel.Factory
70+
public static Action factory() throws Throwable {
71+
var environments =
72+
JmxExporterTestEnvironment.createTestEnvironments(AllLowerCaseWithHttpAuthSSLTest.class).stream()
73+
.filter(e -> !e.getJavaDockerImage().contains("eclipse-temurin:8-alpine"))
74+
.map(e -> e.setBaseUrl(BASE_URL))
75+
.collect(Collectors.toList());
76+
77+
return Each.parallel(
78+
AllLowerCaseWithHttpAuthSSLTest.class.getName(),
79+
environments,
80+
env -> Instance.builder(env.name(), () -> new AllLowerCaseWithHttpAuthSSLTest(env))
81+
.body(Scope.builder("scenario")
82+
.before(Step.of(
83+
"setUp()",
84+
withInstance(
85+
AllLowerCaseWithHttpAuthSSLTest.class,
86+
AllLowerCaseWithHttpAuthSSLTest::setUp)))
87+
.body(Sequence.builder("tests")
88+
.child(Step.of(
89+
"testHealthy()",
90+
withInstance(
91+
AllLowerCaseWithHttpAuthSSLTest.class,
92+
AllLowerCaseWithHttpAuthSSLTest::testHealthy)))
93+
.child(Step.of(
94+
"testDefaultTextMetrics()",
95+
withInstance(
96+
AllLowerCaseWithHttpAuthSSLTest.class,
97+
AllLowerCaseWithHttpAuthSSLTest
98+
::testDefaultTextMetrics)))
99+
.child(Step.of(
100+
"testOpenMetricsTextMetrics()",
101+
withInstance(
102+
AllLowerCaseWithHttpAuthSSLTest.class,
103+
AllLowerCaseWithHttpAuthSSLTest
104+
::testOpenMetricsTextMetrics)))
105+
.child(Step.of(
106+
"testPrometheusTextMetrics()",
107+
withInstance(
108+
AllLowerCaseWithHttpAuthSSLTest.class,
109+
AllLowerCaseWithHttpAuthSSLTest
110+
::testPrometheusTextMetrics)))
111+
.child(Step.of(
112+
"testPrometheusProtobufMetrics()",
113+
withInstance(
114+
AllLowerCaseWithHttpAuthSSLTest.class,
115+
AllLowerCaseWithHttpAuthSSLTest
116+
::testPrometheusProtobufMetrics))))
117+
.after(Step.of(
118+
"tearDown()",
119+
withInstance(
120+
AllLowerCaseWithHttpAuthSSLTest.class,
121+
AllLowerCaseWithHttpAuthSSLTest::tearDown)))))
122+
.build();
123+
}
124+
125+
private AllLowerCaseWithHttpAuthSSLTest(JmxExporterTestEnvironment environment) {
126+
this.environment = environment;
127+
}
128+
129+
public void setUp() throws Throwable {
130+
network = NetworkSupport.create();
131+
environment.initialize(network);
132+
}
133+
134+
public void testHealthy() throws IOException {
135+
String url = environment.getUrl(JmxExporterPath.HEALTHY);
136+
137+
for (String username : TEST_USERNAMES) {
138+
for (String password : TEST_PASSWORDS) {
139+
int expectedStatusCode = 401;
140+
141+
if (VALID_USERNAME.equals(username) && VALID_PASSWORD.equals(password)) {
142+
expectedStatusCode = 200;
143+
}
144+
145+
HttpRequest httpRequest = HttpRequest.builder()
146+
.url(url)
147+
.basicAuthentication(username, password)
148+
.build();
149+
150+
HttpResponse httpResponse = HttpClient.sendRequest(httpRequest);
151+
152+
assertThat(httpResponse.statusCode()).isEqualTo(expectedStatusCode);
153+
}
154+
}
155+
}
156+
157+
public void testDefaultTextMetrics() throws IOException {
158+
String url = environment.getUrl(JmxExporterPath.METRICS);
159+
160+
for (String username : TEST_USERNAMES) {
161+
for (String password : TEST_PASSWORDS) {
162+
int expectedStatusCode = 401;
163+
164+
if (VALID_USERNAME.equals(username) && VALID_PASSWORD.equals(password)) {
165+
expectedStatusCode = 200;
166+
}
167+
168+
HttpRequest httpRequest = HttpRequest.builder()
169+
.url(url)
170+
.basicAuthentication(username, password)
171+
.build();
172+
173+
HttpResponse httpResponse = HttpClient.sendRequest(httpRequest);
174+
175+
if (expectedStatusCode == 401) {
176+
assertThat(httpResponse.statusCode()).isEqualTo(401);
177+
} else {
178+
assertMetricsResponse(httpResponse, MetricsContentType.DEFAULT);
179+
}
180+
}
181+
}
182+
}
183+
184+
public void testOpenMetricsTextMetrics() throws IOException {
185+
String url = environment.getUrl(JmxExporterPath.METRICS);
186+
187+
for (String username : TEST_USERNAMES) {
188+
for (String password : TEST_PASSWORDS) {
189+
int expectedStatusCode = 401;
190+
191+
if (VALID_USERNAME.equals(username) && VALID_PASSWORD.equals(password)) {
192+
expectedStatusCode = 200;
193+
}
194+
195+
HttpRequest httpRequest = HttpRequest.builder()
196+
.url(url)
197+
.basicAuthentication(username, password)
198+
.header(HttpHeader.ACCEPT, MetricsContentType.OPEN_METRICS_TEXT_METRICS.toString())
199+
.build();
200+
201+
HttpResponse httpResponse = HttpClient.sendRequest(httpRequest);
202+
203+
if (expectedStatusCode == 401) {
204+
assertThat(httpResponse.statusCode()).isEqualTo(401);
205+
} else {
206+
assertMetricsResponse(httpResponse, MetricsContentType.OPEN_METRICS_TEXT_METRICS);
207+
}
208+
}
209+
}
210+
}
211+
212+
public void testPrometheusTextMetrics() throws IOException {
213+
String url = environment.getUrl(JmxExporterPath.METRICS);
214+
215+
for (String username : TEST_USERNAMES) {
216+
for (String password : TEST_PASSWORDS) {
217+
int expectedStatusCode = 401;
218+
219+
if (VALID_USERNAME.equals(username) && VALID_PASSWORD.equals(password)) {
220+
expectedStatusCode = 200;
221+
}
222+
223+
HttpRequest httpRequest = HttpRequest.builder()
224+
.url(url)
225+
.basicAuthentication(username, password)
226+
.header(HttpHeader.ACCEPT, MetricsContentType.PROMETHEUS_TEXT_METRICS.toString())
227+
.build();
228+
229+
HttpResponse httpResponse = HttpClient.sendRequest(httpRequest);
230+
231+
if (expectedStatusCode == 401) {
232+
assertThat(httpResponse.statusCode()).isEqualTo(401);
233+
} else {
234+
assertMetricsResponse(httpResponse, MetricsContentType.PROMETHEUS_TEXT_METRICS);
235+
}
236+
}
237+
}
238+
}
239+
240+
public void testPrometheusProtobufMetrics() throws IOException {
241+
String url = environment.getUrl(JmxExporterPath.METRICS);
242+
243+
for (String username : TEST_USERNAMES) {
244+
for (String password : TEST_PASSWORDS) {
245+
int expectedStatusCode = 401;
246+
247+
if (VALID_USERNAME.equals(username) && VALID_PASSWORD.equals(password)) {
248+
expectedStatusCode = 200;
249+
}
250+
251+
HttpRequest httpRequest = HttpRequest.builder()
252+
.url(url)
253+
.basicAuthentication(username, password)
254+
.header(HttpHeader.ACCEPT, MetricsContentType.PROMETHEUS_PROTOBUF_METRICS.toString())
255+
.build();
256+
257+
HttpResponse httpResponse = HttpClient.sendRequest(httpRequest);
258+
259+
if (expectedStatusCode == 401) {
260+
assertThat(httpResponse.statusCode()).isEqualTo(401);
261+
} else {
262+
assertMetricsResponse(httpResponse, MetricsContentType.PROMETHEUS_PROTOBUF_METRICS);
263+
}
264+
}
265+
}
266+
}
267+
268+
public void tearDown() throws Throwable {
269+
environment.close();
270+
NetworkSupport.close(network);
271+
}
272+
273+
private void assertMetricsResponse(HttpResponse httpResponse, MetricsContentType metricsContentType) {
274+
assertMetricsContentType(httpResponse, metricsContentType);
275+
276+
Map<String, Collection<Metric>> metrics = MetricsParser.parseMap(httpResponse);
277+
278+
for (String metricName : metrics.keySet()) {
279+
assertThat(metricName).isEqualTo(metricName.toLowerCase());
280+
}
281+
282+
for (Collection<Metric> metricList : metrics.values()) {
283+
for (Metric metric : metricList) {
284+
for (String labelName : metric.labels().keySet()) {
285+
assertThat(labelName).isEqualTo(labelName.toLowerCase());
286+
}
287+
}
288+
}
289+
290+
boolean isJmxExporterModeJavaAgent = environment.getJmxExporterMode() == JmxExporterMode.JavaAgent;
291+
292+
String buildInfoName = environment.getJmxExporterMode().getBuildInfoName();
293+
294+
assertMetric(metrics)
295+
.ofType(Metric.Type.GAUGE)
296+
.withName("jmx_exporter_build_info")
297+
.withLabel("name", buildInfoName)
298+
.withValue(1d)
299+
.isPresent();
300+
301+
assertMetric(metrics)
302+
.ofType(Metric.Type.GAUGE)
303+
.withName("jmx_scrape_error")
304+
.withValue(0d)
305+
.isPresent();
306+
307+
assertMetric(metrics)
308+
.ofType(Metric.Type.COUNTER)
309+
.withName("jmx_config_reload_success_total")
310+
.withValue(0d)
311+
.isPresent();
312+
313+
assertMetric(metrics)
314+
.ofType(Metric.Type.GAUGE)
315+
.withName("jvm_memory_used_bytes")
316+
.withLabel("area", "nonheap")
317+
.isPresentWhen(isJmxExporterModeJavaAgent);
318+
319+
assertMetric(metrics)
320+
.ofType(Metric.Type.GAUGE)
321+
.withName("jvm_memory_used_bytes")
322+
.withLabel("area", "heap")
323+
.isPresentWhen(isJmxExporterModeJavaAgent);
324+
325+
assertMetric(metrics)
326+
.ofType(Metric.Type.UNTYPED)
327+
.withName("io_prometheus_jmx_tabulardata_server_1_disk_usage_table_size")
328+
.withLabel("source", "/dev/sda1")
329+
.withValue(7.516192768E9d)
330+
.isPresent();
331+
332+
assertMetric(metrics)
333+
.ofType(Metric.Type.UNTYPED)
334+
.withName("io_prometheus_jmx_tabulardata_server_2_disk_usage_table_pcent")
335+
.withLabel("source", "/dev/sda2")
336+
.withValue(0.8d)
337+
.isPresent();
338+
}
339+
}

0 commit comments

Comments
 (0)