From 7d3fe42df5e0d303bd699dc142dfe850b0f3f0b5 Mon Sep 17 00:00:00 2001 From: Mark Goddard Date: Fri, 17 May 2024 09:08:23 +0100 Subject: [PATCH 1/3] Move monitoring tests to a monitoring module --- stackhpc_openstack_tests/monitoring/__init__.py | 13 +++++++++++++ .../{ => monitoring}/test_opensearch.py | 0 .../{ => monitoring}/test_prometheus.py | 0 3 files changed, 13 insertions(+) create mode 100644 stackhpc_openstack_tests/monitoring/__init__.py rename stackhpc_openstack_tests/{ => monitoring}/test_opensearch.py (100%) rename stackhpc_openstack_tests/{ => monitoring}/test_prometheus.py (100%) diff --git a/stackhpc_openstack_tests/monitoring/__init__.py b/stackhpc_openstack_tests/monitoring/__init__.py new file mode 100644 index 0000000..0e8b8f8 --- /dev/null +++ b/stackhpc_openstack_tests/monitoring/__init__.py @@ -0,0 +1,13 @@ +# Copyright (c) 2024 StackHPC Ltd. + +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. diff --git a/stackhpc_openstack_tests/test_opensearch.py b/stackhpc_openstack_tests/monitoring/test_opensearch.py similarity index 100% rename from stackhpc_openstack_tests/test_opensearch.py rename to stackhpc_openstack_tests/monitoring/test_opensearch.py diff --git a/stackhpc_openstack_tests/test_prometheus.py b/stackhpc_openstack_tests/monitoring/test_prometheus.py similarity index 100% rename from stackhpc_openstack_tests/test_prometheus.py rename to stackhpc_openstack_tests/monitoring/test_prometheus.py From b81d9109e547cc813d35dfbb7553d2301b4360c6 Mon Sep 17 00:00:00 2001 From: Mark Goddard Date: Wed, 15 May 2024 11:57:18 +0100 Subject: [PATCH 2/3] Add OpenSearch Dashboards test --- requirements.txt | 1 + .../monitoring/test_opensearch.py | 14 +++++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 8b58343..fbffc35 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ opensearch-py==2.5.* prometheus-api-client==0.5.* pytest-testinfra==10.1.* +requests==2.31.* diff --git a/stackhpc_openstack_tests/monitoring/test_opensearch.py b/stackhpc_openstack_tests/monitoring/test_opensearch.py index b750f88..300b7c0 100644 --- a/stackhpc_openstack_tests/monitoring/test_opensearch.py +++ b/stackhpc_openstack_tests/monitoring/test_opensearch.py @@ -13,12 +13,12 @@ # under the License. # TODO: -# * Dashboard login # * Cluster health from opensearchpy import OpenSearch import os import pytest +import requests from stackhpc_openstack_tests import utils @@ -60,3 +60,15 @@ def test_opensearch_has_info_logs(opensearch): # https://opensearch-project.github.io/opensearch-py/api-ref/clients/opensearch_client.html#opensearchpy.OpenSearch.search result = opensearch.search(body=query, index="flog-*", size=1) assert len(result["hits"]["hits"]) == 1 + + +def test_opensearch_dashboards_status(): + """Check that OpenSearch Dashboards is accessible and is in a green state.""" + dashboard_url = os.environ["OPENSEARCH_DASHBOARDS_URL"] + dashboard_username = os.environ["OPENSEARCH_DASHBOARDS_USERNAME"] + dashboard_password = os.environ["OPENSEARCH_DASHBOARDS_PASSWORD"] + dashboard_url += "/api/status" + result = requests.get(dashboard_url, auth=(dashboard_username, dashboard_password)) + assert result.ok + result = result.json() + assert result["status"]["overall"]["state"] == "green" From f5fa6aee8933c3c921ec838cfdcb0825763da689 Mon Sep 17 00:00:00 2001 From: Mark Goddard Date: Wed, 15 May 2024 12:13:54 +0100 Subject: [PATCH 3/3] Add initial Grafana tests Adds one tests that checks some basic Grafana statistics. Mostly to check that we can access the API. --- requirements.txt | 1 + .../monitoring/test_grafana.py | 40 +++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 stackhpc_openstack_tests/monitoring/test_grafana.py diff --git a/requirements.txt b/requirements.txt index fbffc35..df795ca 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ +grafana-client==4.1.* opensearch-py==2.5.* prometheus-api-client==0.5.* pytest-testinfra==10.1.* diff --git a/stackhpc_openstack_tests/monitoring/test_grafana.py b/stackhpc_openstack_tests/monitoring/test_grafana.py new file mode 100644 index 0000000..83c5b3d --- /dev/null +++ b/stackhpc_openstack_tests/monitoring/test_grafana.py @@ -0,0 +1,40 @@ +# Copyright (c) 2024 StackHPC Ltd. + +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +# TODO: Check if we can validate that dashboards load correctly. + +from grafana_client import GrafanaApi +import os +import pytest + + +@pytest.fixture +def grafana() -> GrafanaApi: + """Pytest fixture that creates a Grafana API client.""" + # https://github.com/grafana-toolbox/grafana-client + grafana_url = os.environ["GRAFANA_URL"] + grafana_username = os.environ["GRAFANA_USERNAME"] + grafana_password = os.environ["GRAFANA_PASSWORD"] + return GrafanaApi.from_url( + grafana_url, + credential=(grafana_username, grafana_password), + ) + + +def test_grafana_api_stats(grafana): + """Test that Grafana API stats are accessible.""" + # https://grafana.com/docs/grafana/latest/developers/http_api/admin/#grafana-stats + result = grafana.admin.stats() + assert result["dashboards"] > 0 + assert result["datasources"] > 0