Skip to content

Commit

Permalink
Refactored authentication tests (#967)
Browse files Browse the repository at this point in the history
Signed-off-by: dhoard <[email protected]>
  • Loading branch information
dhoard committed May 30, 2024
1 parent 9f57e0d commit d684ab1
Show file tree
Hide file tree
Showing 23 changed files with 1,836 additions and 1,606 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class MetricAssertion {
*
* @param metrics metrics
*/
public MetricAssertion(Collection<Metric> metrics) {
private MetricAssertion(Collection<Metric> metrics) {
if (metrics == null) {
throw new IllegalArgumentException("Collection<Metrics> is null");
}
Expand All @@ -60,7 +60,7 @@ public MetricAssertion(Collection<Metric> metrics) {
* @param type type
* @return this MetricAssertion
*/
public MetricAssertion type(String type) {
public MetricAssertion ofType(String type) {
if (type == null || !VALID_TYPES.contains(type)) {
throw new IllegalArgumentException(String.format("Type [%s] is null or invalid", type));
}
Expand All @@ -74,7 +74,7 @@ public MetricAssertion type(String type) {
* @param name name
* @return this MetricAssertion
*/
public MetricAssertion name(String name) {
public MetricAssertion withName(String name) {
this.name = name;
return this;
}
Expand All @@ -97,7 +97,7 @@ public MetricAssertion help(String help) {
* @param value value
* @return this MetricAssertion
*/
public MetricAssertion addLabel(String name, String value) {
public MetricAssertion withLabel(String name, String value) {
if (name == null || value == null) {
throw new IllegalArgumentException(
String.format("Label name [%s] or value [%s] is null", name, value));
Expand All @@ -115,7 +115,7 @@ public MetricAssertion addLabel(String name, String value) {
* @param value value
* @return this MetricAssertion
*/
public MetricAssertion value(Double value) {
public MetricAssertion withValue(Double value) {
this.value = value;
return this;
}
Expand All @@ -132,10 +132,10 @@ public MetricAssertion isPresent() {
/**
* Method to assert the Metric is present
*
* @param isPresent isPresent
* @param condition condition
* @return this MetricAssertion
*/
public MetricAssertion isPresent(boolean isPresent) {
public MetricAssertion isPresent(boolean condition) {
List<Metric> metrics =
this.metrics.stream()
.filter(metric -> type == null || metric.type().equals(type))
Expand All @@ -148,18 +148,28 @@ public MetricAssertion isPresent(boolean isPresent) {
.filter(metric -> value == null || metric.value() == value)
.collect(Collectors.toList());

if (isPresent && metrics.size() != 1) {
throw new AssertionFailedError(
String.format(
"Metric type [%s] help [%s] name [%s] labels [%s] value [%f] is not"
+ " present or matches multiple metrics",
type, help, name, labels, value));
} else if (!isPresent && !metrics.isEmpty()) {
throw new AssertionFailedError(
String.format(
"Metric type [%s] help [%s] name [%s] labels [%s] value [%f] is"
+ " present or matches multiple metrics",
type, help, name, labels, value));
if (condition) {
if (metrics.size() > 1) {
throw new AssertionFailedError(
String.format(
"Metric type [%s] help [%s] name [%s] labels [%s] value [%f]"
+ " matches multiple metrics",
type, help, name, labels, value));
} else if (metrics.isEmpty()) {
throw new AssertionFailedError(
String.format(
"Metric type [%s] help [%s] name [%s] labels [%s] value [%f] is not"
+ " present",
type, help, name, labels, value));
}
} else {
if (metrics.size() > 0) {
throw new AssertionFailedError(
String.format(
"Metric type [%s] help [%s] name [%s] labels [%s] value [%f] is"
+ " present",
type, help, name, labels, value));
}
}

return this;
Expand All @@ -177,10 +187,20 @@ public MetricAssertion isNotPresent() {
/**
* Method to assert the Metric is not present
*
* @param isNotPresent isNotPresent
* @param condition condition
* @return this MetricAssertion
*/
public MetricAssertion isNotPresent(boolean isNotPresent) {
return isPresent(!isNotPresent);
public MetricAssertion isNotPresent(boolean condition) {
return isPresent(!condition);
}

/**
* Method to create a MetricAssertion
*
* @param metrics the collection of metrics
* @return a MetricAssertion
*/
public static MetricAssertion assertMetric(Collection<Metric> metrics) {
return new MetricAssertion(metrics);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.prometheus.jmx.test;

import static io.prometheus.jmx.test.support.http.HttpResponseAssertions.assertHttpMetricsResponse;
import static io.prometheus.jmx.test.support.metrics.MetricAssertion.assertMetric;

import io.prometheus.jmx.test.support.http.HttpHealthyRequest;
import io.prometheus.jmx.test.support.http.HttpMetricsRequest;
Expand All @@ -26,7 +27,6 @@
import io.prometheus.jmx.test.support.http.HttpResponse;
import io.prometheus.jmx.test.support.http.HttpResponseAssertions;
import io.prometheus.jmx.test.support.metrics.Metric;
import io.prometheus.jmx.test.support.metrics.MetricAssertion;
import io.prometheus.jmx.test.support.metrics.MetricsParser;
import java.util.Collection;
import java.util.function.Consumer;
Expand Down Expand Up @@ -67,18 +67,18 @@ public void accept(HttpResponse httpResponse) {

Collection<Metric> metrics = MetricsParser.parse(httpResponse);

new MetricAssertion(metrics)
.type("UNTYPED")
.name("org_exist_management_exist_ProcessReport_RunningQueries_id")
.addLabel("key_id", "1")
.addLabel("key_path", "/db/query1.xq")
assertMetric(metrics)
.ofType("UNTYPED")
.withName("org_exist_management_exist_ProcessReport_RunningQueries_id")
.withLabel("key_id", "1")
.withLabel("key_path", "/db/query1.xq")
.isPresent();

new MetricAssertion(metrics)
.type("UNTYPED")
.name("org_exist_management_exist_ProcessReport_RunningQueries_id")
.addLabel("key_id", "2")
.addLabel("key_path", "/db/query2.xq")
assertMetric(metrics)
.ofType("UNTYPED")
.withName("org_exist_management_exist_ProcessReport_RunningQueries_id")
.withLabel("key_id", "2")
.withLabel("key_path", "/db/query2.xq")
.isPresent();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.prometheus.jmx.test;

import static io.prometheus.jmx.test.support.http.HttpResponseAssertions.assertHttpMetricsResponse;
import static io.prometheus.jmx.test.support.metrics.MetricAssertion.assertMetric;

import io.prometheus.jmx.test.support.Mode;
import io.prometheus.jmx.test.support.http.HttpHealthyRequest;
Expand All @@ -27,7 +28,6 @@
import io.prometheus.jmx.test.support.http.HttpResponse;
import io.prometheus.jmx.test.support.http.HttpResponseAssertions;
import io.prometheus.jmx.test.support.metrics.Metric;
import io.prometheus.jmx.test.support.metrics.MetricAssertion;
import io.prometheus.jmx.test.support.metrics.MetricsParser;
import java.util.Collection;
import java.util.function.Consumer;
Expand Down Expand Up @@ -73,78 +73,82 @@ public void accept(HttpResponse httpResponse) {
? "jmx_prometheus_javaagent"
: "jmx_prometheus_httpserver";

new MetricAssertion(metrics)
.type("GAUGE")
.name("jmx_exporter_build_info")
.addLabel("name", buildInfoName)
.value(1d)
assertMetric(metrics)
.ofType("GAUGE")
.withName("jmx_exporter_build_info")
.withLabel("name", buildInfoName)
.withValue(1d)
.isPresent();

new MetricAssertion(metrics).type("GAUGE").name("jmx_scrape_error").value(0d).isPresent();
assertMetric(metrics)
.ofType("GAUGE")
.withName("jmx_scrape_error")
.withValue(0d)
.isPresent();

new MetricAssertion(metrics)
.type("COUNTER")
.name("jmx_config_reload_success_total")
.value(0d)
assertMetric(metrics)
.ofType("COUNTER")
.withName("jmx_config_reload_success_total")
.withValue(0d)
.isPresent();

new MetricAssertion(metrics)
.type("GAUGE")
.name("jvm_memory_used_bytes")
.addLabel("area", "nonheap")
assertMetric(metrics)
.ofType("GAUGE")
.withName("jvm_memory_used_bytes")
.withLabel("area", "nonheap")
.isPresent(testArgument.mode() == Mode.JavaAgent);

new MetricAssertion(metrics)
.type("GAUGE")
.name("jvm_memory_used_bytes")
.addLabel("area", "heap")
assertMetric(metrics)
.ofType("GAUGE")
.withName("jvm_memory_used_bytes")
.withLabel("area", "heap")
.isPresent(testArgument.mode() == Mode.JavaAgent);

new MetricAssertion(metrics)
.type("GAUGE")
.name("jvm_memory_used_bytes")
.addLabel("area", "nonheap")
assertMetric(metrics)
.ofType("GAUGE")
.withName("jvm_memory_used_bytes")
.withLabel("area", "nonheap")
.isNotPresent(testArgument.mode() == Mode.Standalone);

new MetricAssertion(metrics)
.type("GAUGE")
.name("jvm_memory_used_bytes")
.addLabel("area", "heap")
assertMetric(metrics)
.ofType("GAUGE")
.withName("jvm_memory_used_bytes")
.withLabel("area", "heap")
.isNotPresent(testArgument.mode() == Mode.Standalone);

new MetricAssertion(metrics)
.type("UNTYPED")
.name("io_prometheus_jmx_tabularData_Server_1_Disk_Usage_Table_size")
.addLabel("source", "/dev/sda1")
.value(7.516192768E9d)
assertMetric(metrics)
.ofType("UNTYPED")
.withName("io_prometheus_jmx_tabularData_Server_1_Disk_Usage_Table_size")
.withLabel("source", "/dev/sda1")
.withValue(7.516192768E9d)
.isPresent();

new MetricAssertion(metrics)
.type("UNTYPED")
.name("io_prometheus_jmx_tabularData_Server_2_Disk_Usage_Table_pcent")
.addLabel("source", "/dev/sda2")
.value(0.8d)
assertMetric(metrics)
.ofType("UNTYPED")
.withName("io_prometheus_jmx_tabularData_Server_2_Disk_Usage_Table_pcent")
.withLabel("source", "/dev/sda2")
.withValue(0.8d)
.isPresent();

new MetricAssertion(metrics)
.type("UNTYPED")
.name(
assertMetric(metrics)
.ofType("UNTYPED")
.withName(
"io_prometheus_jmx_test_PerformanceMetricsMBean_PerformanceMetrics_ActiveSessions")
.value(2.0d)
.withValue(2.0d)
.isPresent();

new MetricAssertion(metrics)
.type("UNTYPED")
.name(
assertMetric(metrics)
.ofType("UNTYPED")
.withName(
"io_prometheus_jmx_test_PerformanceMetricsMBean_PerformanceMetrics_Bootstraps")
.value(4.0d)
.withValue(4.0d)
.isPresent();

new MetricAssertion(metrics)
.type("UNTYPED")
.name(
assertMetric(metrics)
.ofType("UNTYPED")
.withName(
"io_prometheus_jmx_test_PerformanceMetricsMBean_PerformanceMetrics_BootstrapsDeferred")
.value(6.0d)
.withValue(6.0d)
.isPresent();
}
}
Loading

0 comments on commit d684ab1

Please sign in to comment.