Merge pull request #74055 from tfc/nixos-test-port-prometheus

nixos/prometheus: Port prometheus test to python
This commit is contained in:
Robin Gloster 2019-11-24 21:59:45 +01:00 committed by GitHub
commit 3e3918d02a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -31,7 +31,7 @@ let
}; };
}; };
in import ./make-test.nix { in import ./make-test-python.nix {
name = "prometheus"; name = "prometheus";
nodes = { nodes = {
@ -173,67 +173,73 @@ in import ./make-test.nix {
testScript = { nodes, ... } : '' testScript = { nodes, ... } : ''
# Before starting the other machines we first make sure that our S3 service is online # Before starting the other machines we first make sure that our S3 service is online
# and has a bucket added for thanos: # and has a bucket added for thanos:
$s3->start; s3.start()
$s3->waitForUnit("minio.service"); s3.wait_for_unit("minio.service")
$s3->waitForOpenPort(${toString minioPort}); s3.wait_for_open_port(${toString minioPort})
$s3->succeed( s3.succeed(
"mc config host add minio " . "mc config host add minio "
"http://localhost:${toString minioPort} ${s3.accessKey} ${s3.secretKey} S3v4"); + "http://localhost:${toString minioPort} "
$s3->succeed("mc mb minio/thanos-bucket"); + "${s3.accessKey} ${s3.secretKey} S3v4",
"mc mb minio/thanos-bucket",
)
# Now that s3 has started we can start the other machines: # Now that s3 has started we can start the other machines:
$prometheus->start; for machine in prometheus, query, store:
$query->start; machine.start()
$store->start;
# Check if prometheus responds to requests: # Check if prometheus responds to requests:
$prometheus->waitForUnit("prometheus.service"); prometheus.wait_for_unit("prometheus.service")
$prometheus->waitForOpenPort(${toString queryPort}); prometheus.wait_for_open_port(${toString queryPort})
$prometheus->succeed("curl -s http://127.0.0.1:${toString queryPort}/metrics"); prometheus.succeed("curl -s http://127.0.0.1:${toString queryPort}/metrics")
# Let's test if pushing a metric to the pushgateway succeeds: # Let's test if pushing a metric to the pushgateway succeeds:
$prometheus->waitForUnit("pushgateway.service"); prometheus.wait_for_unit("pushgateway.service")
$prometheus->succeed( prometheus.succeed(
"echo 'some_metric 3.14' | " . "echo 'some_metric 3.14' | "
"curl --data-binary \@- http://127.0.0.1:${toString pushgwPort}/metrics/job/some_job"); + "curl --data-binary \@- "
+ "http://127.0.0.1:${toString pushgwPort}/metrics/job/some_job"
)
# Now check whether that metric gets ingested by prometheus. # Now check whether that metric gets ingested by prometheus.
# Since we'll check for the metric several times on different machines # Since we'll check for the metric several times on different machines
# we abstract the test using the following function: # we abstract the test using the following function:
# Function to check if the metric "some_metric" has been received and returns the correct value. # Function to check if the metric "some_metric" has been received and returns the correct value.
local *Machine::waitForMetric = sub { def wait_for_metric(machine):
my ($self) = @_; return machine.wait_until_succeeds(
$self->waitUntilSucceeds( "curl -sf 'http://127.0.0.1:${toString queryPort}/api/v1/query?query=some_metric' | "
"curl -sf 'http://127.0.0.1:${toString queryPort}/api/v1/query?query=some_metric' " . + "jq '.data.result[0].value[1]' | grep '\"3.14\"'"
"| jq '.data.result[0].value[1]' | grep '\"3.14\"'"); )
};
$prometheus->waitForMetric;
wait_for_metric(prometheus)
# Let's test if the pushgateway persists metrics to the configured location. # Let's test if the pushgateway persists metrics to the configured location.
$prometheus->waitUntilSucceeds("test -e /var/lib/prometheus-pushgateway/metrics"); prometheus.wait_until_succeeds("test -e /var/lib/prometheus-pushgateway/metrics")
# Test thanos # Test thanos
$prometheus->waitForUnit("thanos-sidecar.service"); prometheus.wait_for_unit("thanos-sidecar.service")
# Test if the Thanos query service can correctly retrieve the metric that was send above. # Test if the Thanos query service can correctly retrieve the metric that was send above.
$query->waitForUnit("thanos-query.service"); query.wait_for_unit("thanos-query.service")
$query->waitForMetric; wait_for_metric(query)
# Test if the Thanos sidecar has correctly uploaded its TSDB to S3, if the # Test if the Thanos sidecar has correctly uploaded its TSDB to S3, if the
# Thanos storage service has correctly downloaded it from S3 and if the Thanos # Thanos storage service has correctly downloaded it from S3 and if the Thanos
# query service running on $store can correctly retrieve the metric: # query service running on $store can correctly retrieve the metric:
$store->waitForUnit("thanos-store.service"); store.wait_for_unit("thanos-store.service")
$store->waitForMetric; wait_for_metric(store)
$store->waitForUnit("thanos-compact.service"); store.wait_for_unit("thanos-compact.service")
# Test if the Thanos bucket command is able to retrieve blocks from the S3 bucket # Test if the Thanos bucket command is able to retrieve blocks from the S3 bucket
# and check if the blocks have the correct labels: # and check if the blocks have the correct labels:
$store->succeed( store.succeed(
"thanos bucket ls" . "thanos bucket ls "
" --objstore.config-file=${nodes.store.config.services.thanos.store.objstore.config-file}" . + "--objstore.config-file=${nodes.store.config.services.thanos.store.objstore.config-file} "
" --output=json | jq .thanos.labels.some_label | grep 'required by thanos'"); + "--output=json | "
+ "jq .thanos.labels.some_label | "
+ "grep 'required by thanos'"
)
''; '';
} }