From 69108800dab54ed7afeffdeac88bd2dbb3c939cd Mon Sep 17 00:00:00 2001 From: Mark Goddard Date: Fri, 26 Apr 2024 08:55:59 +0100 Subject: [PATCH 1/5] Initial project setup --- setup.cfg | 29 ++++++++++++++++++++++++++++ setup.py | 16 +++++++++++++++ stackhpc_openstack_tests/__init__.py | 11 +++++++++++ 3 files changed, 56 insertions(+) create mode 100644 setup.cfg create mode 100644 setup.py create mode 100644 stackhpc_openstack_tests/__init__.py diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..3cc6602 --- /dev/null +++ b/setup.cfg @@ -0,0 +1,29 @@ +[metadata] +name = stackhpc-openstack-tests +version = 0.1.0 +summary = Automated testing for StackHPC OpenStack +description-file = README.md +author = Mark Goddard +author-email = mark@stackhpc.com +url = https://github.com/stackhpc/stackhpc-openstack-tests +python-requires = >=3.6 +classifier = + Environment :: OpenStack + Intended Audience :: Information Technology + Intended Audience :: System Administrators + License :: OSI Approved :: Apache Software License + Operating System :: POSIX :: Linux + Programming Language :: Python + Programming Language :: Python :: 3 + Programming Language :: Python :: 3.7 + Programming Language :: Python :: 3.8 + Programming Language :: Python :: 3.9 + Programming Language :: Python :: 3.10 + Programming Language :: Python :: 3.11 + Programming Language :: Python :: 3.12 + Programming Language :: Python :: 3 :: Only + Programming Language :: Python :: Implementation :: CPython + +[files] +packages = + stackhpc_openstack_tests diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..afbff18 --- /dev/null +++ b/setup.py @@ -0,0 +1,16 @@ +# 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. + +import setuptools + +setuptools.setup() diff --git a/stackhpc_openstack_tests/__init__.py b/stackhpc_openstack_tests/__init__.py new file mode 100644 index 0000000..0680747 --- /dev/null +++ b/stackhpc_openstack_tests/__init__.py @@ -0,0 +1,11 @@ +# 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. From fcde1b98f7399a6fe31efe672efe960cd53afd10 Mon Sep 17 00:00:00 2001 From: Mark Goddard Date: Tue, 14 May 2024 10:03:58 +0100 Subject: [PATCH 2/5] Add pytest-testinfra dependency --- requirements.txt | 2 ++ setup.cfg | 4 ++++ setup.py | 2 ++ stackhpc_openstack_tests/__init__.py | 2 ++ 4 files changed, 10 insertions(+) create mode 100644 requirements.txt diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..2b665c2 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +prometheus-api-client==0.5.* +pytest-testinfra==10.1.* diff --git a/setup.cfg b/setup.cfg index 3cc6602..7147947 100644 --- a/setup.cfg +++ b/setup.cfg @@ -27,3 +27,7 @@ classifier = [files] packages = stackhpc_openstack_tests + +[options] +install_requires = + pytest-testinfra diff --git a/setup.py b/setup.py index afbff18..3ae4c5c 100644 --- a/setup.py +++ b/setup.py @@ -1,3 +1,5 @@ +# 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 diff --git a/stackhpc_openstack_tests/__init__.py b/stackhpc_openstack_tests/__init__.py index 0680747..0e8b8f8 100644 --- a/stackhpc_openstack_tests/__init__.py +++ b/stackhpc_openstack_tests/__init__.py @@ -1,3 +1,5 @@ +# 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 From b5c9aba7b59e529fcd7453d9e534610450a112f3 Mon Sep 17 00:00:00 2001 From: Mark Goddard Date: Wed, 15 May 2024 09:49:41 +0100 Subject: [PATCH 3/5] Add initial Prometheus tests --- stackhpc_openstack_tests/test_prometheus.py | 41 +++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 stackhpc_openstack_tests/test_prometheus.py diff --git a/stackhpc_openstack_tests/test_prometheus.py b/stackhpc_openstack_tests/test_prometheus.py new file mode 100644 index 0000000..7478ffa --- /dev/null +++ b/stackhpc_openstack_tests/test_prometheus.py @@ -0,0 +1,41 @@ +# 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. + +import os +from prometheus_api_client import PrometheusConnect +import pytest + + +@pytest.fixture +def prom() -> PrometheusConnect: + """Pytest fixture that creates a Prometheus API client.""" + # https://github.com/4n4nd/prometheus-api-client-python/ + prometheus_url = os.environ["PROMETHEUS_URL"] + kwargs = {} + if "PROMETHEUS_USERNAME" in os.environ: + prometheus_username = os.environ["PROMETHEUS_USERNAME"] + prometheus_password = os.environ["PROMETHEUS_PASSWORD"] + kwargs["auth"] = (prometheus_username, prometheus_password) + return PrometheusConnect(url=prometheus_url, disable_ssl=True, **kwargs) + + +def test_prometheus_connection(prom): + """Check that Prometheus is accessible.""" + assert prom.check_prometheus_connection() + + +def test_prometheus_node_exporter_metrics(prom): + """Check that expected node exporter metrics exist.""" + metrics = prom.all_metrics() + assert "node_cpu_seconds_total" in metrics From 86b43cbb5f8591e6177649bb58a69e9e88cd3c59 Mon Sep 17 00:00:00 2001 From: Mark Goddard Date: Tue, 14 May 2024 09:47:09 +0100 Subject: [PATCH 4/5] Add initial OpenSearch tests --- requirements.txt | 1 + stackhpc_openstack_tests/test_opensearch.py | 62 +++++++++++++++++++++ stackhpc_openstack_tests/utils.py | 18 ++++++ 3 files changed, 81 insertions(+) create mode 100644 stackhpc_openstack_tests/test_opensearch.py create mode 100644 stackhpc_openstack_tests/utils.py diff --git a/requirements.txt b/requirements.txt index 2b665c2..8b58343 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ +opensearch-py==2.5.* prometheus-api-client==0.5.* pytest-testinfra==10.1.* diff --git a/stackhpc_openstack_tests/test_opensearch.py b/stackhpc_openstack_tests/test_opensearch.py new file mode 100644 index 0000000..b750f88 --- /dev/null +++ b/stackhpc_openstack_tests/test_opensearch.py @@ -0,0 +1,62 @@ +# 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: +# * Dashboard login +# * Cluster health + +from opensearchpy import OpenSearch +import os +import pytest + +from stackhpc_openstack_tests import utils + + +@pytest.fixture +def opensearch() -> OpenSearch: + """Pytest fixture that creates an OpenSearch API client.""" + # https://opensearch.org/docs/latest/clients/python-low-level/ + opensearch_hosts = os.environ["OPENSEARCH_HOSTS"].split(",") + opensearch_port = os.environ["OPENSEARCH_PORT"] + opensearch_hosts = [ + {"host": host, "port": opensearch_port} for host in opensearch_hosts + ] + opensearch_tls = utils.str_to_bool(os.environ["OPENSEARCH_TLS"]) + if opensearch_tls: + opensearch_verify_certs = utils.str_to_bool( + os.environ["OPENSEARCH_VERIFY_CERTS"] + ) + else: + opensearch_verify_certs = True + return OpenSearch( + hosts=opensearch_hosts, + http_compress=True, + use_ssl=opensearch_tls, + verify_certs=opensearch_verify_certs, + ssl_show_warn=False, + ) + + +def test_opensearch_has_info_logs(opensearch): + """Check that OpenSearch has some INFO level logs.""" + query = { + "query": { + "match": { + "log_level": "INFO", + } + } + } + # 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 diff --git a/stackhpc_openstack_tests/utils.py b/stackhpc_openstack_tests/utils.py new file mode 100644 index 0000000..9b7b02b --- /dev/null +++ b/stackhpc_openstack_tests/utils.py @@ -0,0 +1,18 @@ +# 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. + + +def str_to_bool(v): + """Convert a boolean true/false string to a bool.""" + return v.lower() == "true" From 16e7ac38c62353eef2ca8574b277bcf0f15e8450 Mon Sep 17 00:00:00 2001 From: Mark Goddard Date: Tue, 14 May 2024 12:02:12 +0100 Subject: [PATCH 5/5] Update README --- README.md | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 8fb4c7b..f5482ad 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,40 @@ -# stackhpc-openstack-tests -Automated testing for StackHPC OpenStack +# StackHPC OpenStack Tests + +Automated testing for StackHPC OpenStack. + +Provides test coverage of various aspects of OpenStack and related services, including: + +* OpenSearch +* Prometheus + +Tests are written using [pytest](https://docs.pytest.org/). + +## Installation + +Clone this repository. + +Create a virtual environment. + +```sh +python3 -m venv venv +``` + +Install stackhpc-openstack-tests and its dependencies. + +```sh +venv/bin/pip install -r /requirements.txt +``` + +## Usage + +Run all tests provided. + +```sh +py.test --pyargs stackhpc_openstack_tests +``` + +Or run tests from a specific submodule. + +```sh +py.test --pyargs stackhpc_openstack_tests.test_prometheus +```