|
| 1 | +# Copyright (c) 2024 StackHPC Ltd. |
| 2 | + |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | +# not use this file except in compliance with the License. You may obtain |
| 5 | +# a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | +# License for the specific language governing permissions and limitations |
| 13 | +# under the License. |
| 14 | + |
| 15 | +import os |
| 16 | +from prometheus_api_client import PrometheusConnect |
| 17 | +import pytest |
| 18 | + |
| 19 | + |
| 20 | +@pytest.fixture |
| 21 | +def prom() -> PrometheusConnect: |
| 22 | + """Pytest fixture that creates a Prometheus API client.""" |
| 23 | + # https://github.com/4n4nd/prometheus-api-client-python/ |
| 24 | + prometheus_url = os.environ["PROMETHEUS_URL"] |
| 25 | + kwargs = {} |
| 26 | + if "PROMETHEUS_USERNAME" in os.environ: |
| 27 | + prometheus_username = os.environ["PROMETHEUS_USERNAME"] |
| 28 | + prometheus_password = os.environ["PROMETHEUS_PASSWORD"] |
| 29 | + kwargs["auth"] = (prometheus_username, prometheus_password) |
| 30 | + return PrometheusConnect(url=prometheus_url, disable_ssl=True, **kwargs) |
| 31 | + |
| 32 | + |
| 33 | +def test_prometheus_connection(prom): |
| 34 | + """Check that Prometheus is accessible.""" |
| 35 | + assert prom.check_prometheus_connection() |
| 36 | + |
| 37 | + |
| 38 | +def test_prometheus_node_exporter_metrics(prom): |
| 39 | + """Check that expected node exporter metrics exist.""" |
| 40 | + metrics = prom.all_metrics() |
| 41 | + assert "node_cpu_seconds_total" in metrics |
0 commit comments