8000 commit python tests · postgrespro/mamonsu@d8697f2 · GitHub
[go: up one dir, main page]

Skip to content

Commit d8697f2

Browse files
committed
commit python tests
1 parent 10ee3fd commit d8697f2

37 files changed

+1515
-0
lines changed

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
docker~=7.1.0
2+
pytest~=8.3.5

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/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.

tests/config/constants/containers.py

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

tests/pytest.ini

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
[pytest]
3+
log_cli=true
4+
log_level=INFO
5+
log_format = %(asctime)s %(levelname)s %(message)s
6+
log_date_format = %Y-%m-%d %H:%M:%S
7+
markers =
8+
bash

tests/requirements.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pytest==8.3.5
2+
docker==7.1.0
3+
zabbix==1.3.1
4+
python-dotenv==1.1.0
5+
psycopg2==2.9.10
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#!/bin/bash
2+
set -e
3+
4+
RECOVERY_FILE="standby.signal"
5+
6+
DATA_DIR=/var/lib/postgresql/data
7+
DATA_SLAVE_PHYSICAL_DIR=/var/lib/postgresql/data_slave_physical
8+
WAL_DIR=/var/lib/postgresql/wals
9+
DATA_SLAVE_LOGICAL_DIR=/var/lib/postgresql/data_slave_logical
10+
11+
su postgres -c '/usr/local/bin/docker-entrypoint.sh postgres "$@" &'
12+
sleep 5
13+
su postgres -c "pg_ctl stop -D $DATA_DIR"
14+
15+
sudo mkdir -p $DATA_SLAVE_PHYSICAL_DIR
16+
sudo mkdir -p $WAL_DIR
17+
sudo chown -R postgres:postgres $DATA_SLAVE_PHYSICAL_DIR $WAL_DIR
18+
sudo chmod 700 $DATA_SLAVE_PHYSICAL_DIR
19+
20+
sudo -u postgres echo "shared_preload_libraries = 'pg_stat_statements'" >> $DATA_DIR/postgresql.conf
21+
sudo -u postgres echo "pg_stat_statements.track = all" >> $DATA_DIR/postgresql.conf
22+
sudo -u postgres echo "archive_mode=on" >> $DATA_DIR/postgresql.conf
23+
sudo -u postgres echo "archive_command='cp %p $WAL_DIR/%f'" >> $DATA_DIR/postgresql.conf
24+
sudo -u postgres echo "wal_level=replica" >> $DATA_DIR/postgresql.conf
25+
sudo -u postgres echo "max_wal_senders=4" >> $DATA_DIR/postgresql.conf
26+
sudo -u postgres echo "hot_standby=on" >> $DATA_DIR/postgresql.conf
27+
28+
sudo -u postgres echo "track_io_timing = on" >> $DATA_DIR/postgresql.conf
29+
sudo -u postgres echo "track_functions = all" >> $DATA_DIR/postgresql.conf
30+
31+
sudo -u postgres echo "host replication replicator 127.0.0.1/0 trust" >> $DATA_DIR/pg_hba.conf
32+
33+
su postgres -c "pg_ctl start -D $DATA_DIR"
34+
sleep 3
35+
36+
sudo -u postgres psql -c "CREATE DATABASE mamonsu_test_db;"
37+
sudo -u postgres psql -d mamonsu_test_db -c "CREATE EXTENSION pg_stat_statements;"
38+
sudo -u postgres psql -d mamonsu_test_db -c "CREATE EXTENSION pg_buffercache;"
39+
sudo -u postgres psql -d mamonsu_test_db -c "CREATE TABLE mamonsu_test_table(id serial, value integer);"
40+
sudo -u postgres psql -d mamonsu_test_db -c "INSERT INTO mamonsu_test_table(value) SELECT * FROM generate_series(1, 10000);"
41+
sudo -u postgres psql -c "CREATE USER replicator WITH REPLICATION ENCRYPTED PASSWORD 'secret';"
42+
sudo -u postgres pg_basebackup -h 127.0.0.1 -U replicator -Fp -Xs -P -R -D $DATA_SLAVE_PHYSICAL_DIR/
43+
sudo -u postgres sed -i '/^archive_mode/s/^\(.*\)$/#\1/' $DATA_SLAVE_PHYSICAL_DIR/postgresql.conf
44+
sudo -u postgres sed -i '/^archive_command/s/^\(.*\)$/#\1/' $DATA_SLAVE_PHYSICAL_DIR/postgresql.conf
45+
sudo -u postgres echo "port=5433" >> $DATA_SLAVE_PHYSICAL_DIR/postgresql.conf
46+
sudo -u postgres echo "restore_command = 'cp $WAL_DIR/%f %p'" >> $DATA_SLAVE_PHYSICAL_DIR/${RECOVERY_FILE}
47+
48+
su postgres -c "pg_ctl start -D $DATA_SLAVE_PHYSICAL_DIR"
49+
50+
# create logical slave
51+
if [ "$POSTGRES_VERSION" -ge 100 ]; then # TODO: пофиксить и объединить или убрать вообще
52+
# create PGDATA directory
53+
sudo mkdir -p $DATA_SLAVE_LOGICAL_DIR
54+
sudo chown postgres:postgres $DATA_SLAVE_LOGICAL_DIR
55+
sudo chmod 700 $DATA_SLAVE_LOGICAL_DIR
56+
57+
sudo -u postgres sed -i '/^wal_level/s/^\(.*\)$/#\1/' $DATA_DIR/postgresql.conf
58+
sudo -u postgres echo "wal_level=logical" >> $DATA_DIR/postgresql.conf
59+
su postgres -c "pg_ctl restart -D $DATA_DIR"
60+
sleep 3
61+
sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE mamonsu_test_db TO replicator;"
62+
sudo -u postgres psql -d mamonsu_test_db -c "GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO replicator;"
63+
sudo -u postgres psql -d mamonsu_test_db -c "CREATE PUBLICATION mamonsu_publication;"
64+
sudo -u postgres psql -d mamonsu_test_db -c "ALTER PUBLICATION mamonsu_publication ADD TABLE mamonsu_test_table;"
65+
sudo -u postgres echo "host all all 127.0.0.1/0 trust" >> $DATA_SLAVE_LOGICAL_DIR/pg_hba.conf
66+
sudo -u postgres echo "port=5434" >> $DATA_SLAVE_LOGICAL_DIR/postgresql.conf
67+
su postgres -c "pg_ctl start -D $DATA_SLAVE_LOGICAL_DIR"
68+
sleep 3
69+
sudo -u postgres psql -p 5434 -c "CREATE DATABASE mamonsu_test_db;"
70+
sudo -u postgres psql -p 5434 -d mamonsu_test_db -c "CREATE TABLE mamonsu_test_table(id serial, value integer);"
71+
sudo -u postgres psql -p 5434 -d mamonsu_test_db -c "CREATE SUBSCRIPTION mamonsu_subscription CONNECTION 'host=127.0.0.1 port=5432 user=replicator dbname=mamonsu_test_db' PUBLICATION mamonsu_publication;"
72+
fi
73+
74+
mamonsu bootstrap -x --user postgres -d mamonsu_test_db
75+
service mamonsu restart
76+
77+
tail -f /dev/null
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/bin/sh
2+
3+
INIT_MARKER="/app/.init_done"
4+
if [ ! -f "$INIT_MARKER" ]; then
5+
echo "[INFO] Exporting templates"
6+
mamonsu export template template.xml
7+
mamonsu zabbix template export template.xml
8+
9+
echo "[INFO] Adding host in Zabbix"
10+
mamonsu zabbix host create "$(hostname)" \
11+
"$(mamonsu zabbix hostgroup id "Zabbix servers")" \
12+
"$(mamonsu zabbix template id "Mamonsu PostgreSQL Linux")" \
13+
"$(getent hosts "$(hostname)" | awk '{print $1}')"
14+
service mamonsu start
15+
16+
echo "[INFO] Waiting for host to appear in Zabbix"
17+
sleep 5
18+
touch "$INIT_MARKER"
19+
else
20+
echo "[INFO] Initialization already done. Skipping Mamonsu setup"
21+
fi
22+

0 commit comments

Comments
 (0)
0