Skip to content

Commit 634ae07

Browse files
authored
cicd: rewrite bash tests to python (#213)
* add python tests * add new postgres versions in runner.py * fix key_and_queries in statements.py with adding info_view
1 parent e6f7890 commit 634ae07

39 files changed

+1547
-8
lines changed

mamonsu/lib/runner.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,7 @@ def is_any_equal(array):
224224
# extract pg version from input
225225
def define_pg_version(version_args):
226226
if len(version_args) < 4:
227-
if version_args == "15" or version_args == "14" or version_args == "11" or version_args == "12" or version_args == "13" or version_args == "10" \
228-
or version_args == "9.6" or version_args == "9.5":
227+
if version_args in ["9.5", "9.6", "10", "11", "12", "13", "14", "15", "16", "17"]:
229228
version_number = version_args[0].split('.')
230229
for num in version_number:
231230
if not num.isdigit():

mamonsu/plugins/pgsql/statements.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -288,13 +288,16 @@ def keys_and_queries(self, template_zabbix):
288288
i + 1))
289289

290290
if Pooler.server_version_greater("14"):
291+
info_view = 'pgpro_stats_info'
291292
if self.extension == "pg_stat_statements":
292-
for i, item in enumerate(self.Items_pg_14):
293-
keys = item[0].split("[")
294-
result.append(
295-
"{0}[*],$2 $1 -c \"{1}\" | awk -F '|' '{{print ${2}}}'".format(
296-
"{0}{1}.{2}".format(self.key, keys[0], keys[1][:-1]),
297-
self.query_info.format(metrics=(item[1]), extension_schema=extension_schema),
293+
info_view = 'pg_stat_statements_info'
294+
for i, item in enumerate(self.Items_pg_14):
295+
keys = item[0].split("[")
296+
result.append(
297+
"{0}[*],$2 $1 -c \"{1}\" | awk -F '|' '{{print ${2}}}'".format(
298+
"{0}{1}.{2}".format(self.key, keys[0], keys[1][:-1]),
299+
self.query_info.format(metrics=(item[1]), extension_schema=extension_schema,
300+
info_view_name=info_view),
298301
i + 1))
299302
return template_zabbix.key_and_query(result)
300303
else:

tests/.env

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
WAIT_MAMONSU_TIMEOUT=180
2+
DEFAULT_HOSTGROUP="Zabbix servers"
3+
DEFAULT_TEMPLATE="Mamonsu PostgreSQL Linux"
4+
POSTGRES_VERSION=15
5+
6+
# creds
7+
POSTGRES_USER=postgres
8+
POSTGRES_PASSWORD=postgres
9+
POSTGRES_DB=mamonsu_test_db
10+
11+
ZABBIX_ADMIN_USER=Admin
12+
ZABBIX_ADMIN_PASS=zabbix
13+
14+
# hosts
15+
ZABBIX_EXT_URL=127.0.0.1:1337
16+
ZABBIX_INT_URL=zabbix-web:8080
17+
POSTGRES_EXT_HOST=127.0.0.1
18+
19+
# external ports
20+
POSTGRES_EXT_PORT=15432
21+
MAMONSU_AGENT_EXT_PORT=11050
22+
ZABBIX_SERVER_EXT_PORT=11051
23+
ZABBIX_WEB_EXT_PORT=1337
24+
25+
# internal ports
26+
POSTGRES_PORT=5432
27+
MAMONSU_AGENT_PORT=10050
28+
ZABBIX_SERVER_PORT=10051
29+
ZABBIX_WEB_PORT=8080

tests/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
# Mamonsu autotests
3+
4+
Mamonsu testing with different Postgres version, different operation systems(not supported yet). Uses docker-compose to run all services.
5+
6+
7+
## Installation
8+
9+
10+
```bash
11+
pip3 install -e requirement.txt
12+
```
13+
14+
## Usage/Examples
15+
16+
You can simly run tests with only pytest mark "bash" and it will be ran with Postgres version from env variable POSTGRES_VERSION which is specified in .env file
17+
18+
```bash
19+
pytest -m bash
20+
```
21+
22+
You can run tests with different Postgres versions with POSTGRES_VERSIONS variable
23+
24+
```bash
25+
POSTGRES_VERSIONS=12,13 pytest -m bash
26+
```
27+
28+
To run specific test you have to use -k flag with function name
29+
30+
```bash
31+
POSTGRES_VERSIONS=12,13 pytest -k test_export_zabbix_params
32+
```

tests/config/__init__.py

Whitespace-only changes.

tests/config/config.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import os
2+
from pathlib import Path
3+
from typing import Any
4+
5+
from dotenv import load_dotenv
6+
7+
8+
class Config:
9+
def __init__(self, env_path: Path | None = None):
10+
self._root_path = Path(__file__).parent.parent
11+
load_dotenv(env_path or self._root_path / ".env")
12+
13+
def __getattr__(self, name: str) -> Any:
14+
value = os.getenv(name)
15+
if value is None:
16+
return None
17+
return self._convert_value(value)
18+
19+
@staticmethod
20+
def _convert_value(value: str) -> Any:
21+
if value.lower() in ("true", "false"):
22+
return value.lower() == "true"
23+
try:
24+
return int(value)
25+
except ValueError:
26+
try:
27+
return float(value)
28+
except ValueError:
29+
return value

tests/config/constants/__init__.py

Whitespace-only changes.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from enum import StrEnum
2+
3+
4+
class ContainersEnum(StrEnum):
5+
POSTGRES = "mamonsu-pg"
6+
MAMONSU = "mamonsu-pg"
7+
ZABBIX_WEB = "zabbix-web"
8+
ZABBIX_SERVER = "zabbix-server"

tests/debian.Dockerfile

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Стейдж нужен для того, что бы потом скопировать из него энтрипоинт нужной версии, т.к. в --from= нельзя использовать env
2+
ARG POSTGRES_VERSION=15
3+
FROM postgres:${POSTGRES_VERSION} AS postgres_base
4+
5+
FROM debian:bookworm-slim AS builder
6+
RUN apt-get update && \
7+
apt-get install -y --no-install-recommends \
8+
curl \
9+
software-properties-common \
10+
make \
11+
dpkg-dev \
12+
debhelper \
13+
build-essential \
14+
python3-dev \
15+
python3-setuptools && \
16+
rm -rf /var/lib/apt/lists/*
17+
18+
WORKDIR /app
19+
COPY . /app
20+
RUN make deb
21+
22+
FROM postgres:${POSTGRES_VERSION}
23+
24+
COPY --from=builder /app/mamonsu*.deb /tmp/
25+
26+
RUN apt-get update && \
27+
apt-get install -y --no-install-recommends \
28+
python3 \
29+
python3-setuptools \
30+
sudo \
31+
&& rm -rf /var/lib/apt/lists/*
32+
RUN dpkg -i /tmp/mamonsu*.deb || apt-get install -f -y && \
33+
rm /tmp/mamonsu*.deb
34+
RUN mkdir -p /var/log/mamonsu && \
35+
chown postgres:postgres /var/log/mamonsu && \
36+
chmod 755 /var/log/mamonsu
37+
38+
COPY --from=postgres_base /usr/local/bin/docker-entrypoint.sh /usr/local/bin/
39+
COPY ./tests/service-scripts/mamonsu-pg/mamonsu.conf /etc/mamonsu/agent.conf
40+
COPY ./tests/service-scripts/mamonsu-pg/entrypoint.sh ./tests/service-scripts/mamonsu-pg/init_mamonsu_in_zbx.sh /app/
41+
42+
RUN chmod +x /app/entrypoint.sh /app/init_mamonsu_in_zbx.sh
43+
44+
ENTRYPOINT ["/app/entrypoint.sh"]

tests/docker-compose.yaml

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
services:
2+
mamonsu-pg:
3+
build:
4+
context: .
5+
dockerfile: tests/debian.Dockerfile
6+
args:
7+
POSTGRES_VERSION: ${POSTGRES_VERSION}
8+
container_name: mamonsu-pg
9+
hostname: mamonsu-pg
10+
image: mamonsu-pg
11+
ports:
12+
- "${MAMONSU_AGENT_EXT_PORT}:${MAMONSU_AGENT_PORT}"
13+
- "${POSTGRES_EXT_PORT}:${POSTGRES_PORT}"
14+
environment:
15+
POSTGRES_VERSION: ${POSTGRES_VERSION}
16+
MAMONSU_AGENT_PORT: ${MAMONSU_AGENT_PORT}
17+
POSTGRES_USER: ${POSTGRES_USER}
18+
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
19+
POSTGRES_DB: postgres
20+
POSTGRES_HOST_AUTH_METHOD: md5
21+
ZABBIX_USER: ${ZABBIX_ADMIN_USER}
22+
ZABBIX_PASSWD: ${ZABBIX_ADMIN_PASS}
23+
ZABBIX_URL: http://${ZABBIX_INT_URL}/
24+
restart: no
25+
26+
zabbix:
27+
image: zabbix/zabbix-server-pgsql:6.4.13-ubuntu
28+
container_name: zabbix
29+
hostname: zabbix
30+
environment:
31+
- DB_SERVER_HOST=mamonsu-pg
32+
- POSTGRES_USER=${POSTGRES_USER}
33+
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
34+
- POSTGRES_DB=zabbix
35+
- PGPASSWORD=${POSTGRES_PASSWORD}
36+
ports:
37+
- "${ZABBIX_SERVER_EXT_PORT}:${ZABBIX_SERVER_PORT}"
38+
depends_on:
39+
- mamonsu-pg
40+
41+
zabbix-web:
42+
image: zabbix/zabbix-web-nginx-pgsql:6.4.13-ubuntu
43+
container_name: zabbix-web
44+
hostname: zabbix-web
45+
environment:
46+
- DB_SERVER_HOST=mamonsu-pg
47+
- POSTGRES_USER=${POSTGRES_USER}
48+
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
49+
- POSTGRES_DB=zabbix
50+
- ZBX_SERVER_HOST=zabbix-server
51+
- ZBX_SERVER_PORT=${ZABBIX_SERVER_PORT}
52+
- ZABBIX_ADMIN_USER=Admin
53+
- ZABBIX_ADMIN_PASS=zabbix
54+
ports:
55+
- "${ZABBIX_WEB_EXT_PORT}:${ZABBIX_WEB_PORT}"
56+
depends_on:
57+
- zabbix
58+
healthcheck:
59+
test: |
60+
curl -fsS "http://localhost:${ZABBIX_WEB_PORT}/api_jsonrpc.php" \
61+
-X POST \
62+
-H "Content-Type: application/json-rpc" \
63+
-d '{"jsonrpc":"2.0","method":"apiinfo.version","id":1,"auth":null,"params":{}}' \
64+
| grep -q '"result"' || exit 1
65+
interval: 5s
66+
timeout: 5s
67+
retries: 15
68+
start_period: 30s

0 commit comments

Comments
 (0)