Skip to content

Commit 943e987

Browse files
authored
Merge pull request #1 from stackhpc/prometheus-opensearch
Adds project setup and initial Prometheus and OpenSearch tests
2 parents 20ae7ad + 16e7ac3 commit 943e987

File tree

8 files changed

+228
-2
lines changed

8 files changed

+228
-2
lines changed

README.md

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,40 @@
1-
# stackhpc-openstack-tests
2-
Automated testing for StackHPC OpenStack
1+
# StackHPC OpenStack Tests
2+
3+
Automated testing for StackHPC OpenStack.
4+
5+
Provides test coverage of various aspects of OpenStack and related services, including:
6+
7+
* OpenSearch
8+
* Prometheus
9+
10+
Tests are written using [pytest](https://docs.pytest.org/).
11+
12+
## Installation
13+
14+
Clone this repository.
15+
16+
Create a virtual environment.
17+
18+
```sh
19+
python3 -m venv venv
20+
```
21+
22+
Install stackhpc-openstack-tests and its dependencies.
23+
24+
```sh
25+
venv/bin/pip install <path/to/repo> -r <path/to/repo>/requirements.txt
26+
```
27+
28+
## Usage
29+
30+
Run all tests provided.
31+
32+
```sh
33+
py.test --pyargs stackhpc_openstack_tests
34+
```
35+
36+
Or run tests from a specific submodule.
37+
38+
```sh
39+
py.test --pyargs stackhpc_openstack_tests.test_prometheus
40+
```

requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
opensearch-py==2.5.*
2+
prometheus-api-client==0.5.*
3+
pytest-testinfra==10.1.*

setup.cfg

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
[metadata]
2+
name = stackhpc-openstack-tests
3+
version = 0.1.0
4+
summary = Automated testing for StackHPC OpenStack
5+
description-file = README.md
6+
author = Mark Goddard
7+
author-email = [email protected]
8+
url = https://github.com/stackhpc/stackhpc-openstack-tests
9+
python-requires = >=3.6
10+
classifier =
11+
Environment :: OpenStack
12+
Intended Audience :: Information Technology
13+
Intended Audience :: System Administrators
14+
License :: OSI Approved :: Apache Software License
15+
Operating System :: POSIX :: Linux
16+
Programming Language :: Python
17+
Programming Language :: Python :: 3
18+
Programming Language :: Python :: 3.7
19+
Programming Language :: Python :: 3.8
20+
Programming Language :: Python :: 3.9
21+
Programming Language :: Python :: 3.10
22+
Programming Language :: Python :: 3.11
23+
Programming Language :: Python :: 3.12
24+
Programming Language :: Python :: 3 :: Only
25+
Programming Language :: Python :: Implementation :: CPython
26+
27+
[files]
28+
packages =
29+
stackhpc_openstack_tests
30+
31+
[options]
32+
install_requires =
33+
pytest-testinfra

setup.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Copyright (c) 2024 StackHPC Ltd.
2+
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain 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,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12+
# implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
import setuptools
17+
18+
setuptools.setup()

stackhpc_openstack_tests/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
# TODO:
16+
# * Dashboard login
17+
# * Cluster health
18+
19+
from opensearchpy import OpenSearch
20+
import os
21+
import pytest
22+
23+
from stackhpc_openstack_tests import utils
24+
25+
26+
@pytest.fixture
27+
def opensearch() -> OpenSearch:
28+
"""Pytest fixture that creates an OpenSearch API client."""
29+
# https://opensearch.org/docs/latest/clients/python-low-level/
30+
opensearch_hosts = os.environ["OPENSEARCH_HOSTS"].split(",")
31+
opensearch_port = os.environ["OPENSEARCH_PORT"]
32+
opensearch_hosts = [
33+
{"host": host, "port": opensearch_port} for host in opensearch_hosts
34+
]
35+
opensearch_tls = utils.str_to_bool(os.environ["OPENSEARCH_TLS"])
36+
if opensearch_tls:
37+
opensearch_verify_certs = utils.str_to_bool(
38+
os.environ["OPENSEARCH_VERIFY_CERTS"]
39+
)
40+
else:
41+
opensearch_verify_certs = True
42+
return OpenSearch(
43+
hosts=opensearch_hosts,
44+
http_compress=True,
45+
use_ssl=opensearch_tls,
46+
verify_certs=opensearch_verify_certs,
47+
ssl_show_warn=False,
48+
)
49+
50+
51+
def test_opensearch_has_info_logs(opensearch):
52+
"""Check that OpenSearch has some INFO level logs."""
53+
query = {
54+
"query": {
55+
"match": {
56+
"log_level": "INFO",
57+
}
58+
}
59+
}
60+
# https://opensearch-project.github.io/opensearch-py/api-ref/clients/opensearch_client.html#opensearchpy.OpenSearch.search
61+
result = opensearch.search(body=query, index="flog-*", size=1)
62+
assert len(result["hits"]["hits"]) == 1
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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

stackhpc_openstack_tests/utils.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
16+
def str_to_bool(v):
17+
"""Convert a boolean true/false string to a bool."""
18+
return v.lower() == "true"

0 commit comments

Comments
 (0)