-
Notifications
You must be signed in to change notification settings - Fork 31
Rewrite bash tests to python #213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
baliukr
wants to merge
7
commits into
master
Choose a base branch
from
feature/MMNS-41-python-tests
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d8697f2
commit python tests
baliukr 4ab97fb
add new postgres versions in runner.py
baliukr b148085
fix key_and_queries in statements.py with adding info_view
baliukr b0f9bce
remove unused requirements.txt
baliukr 4fe8d25
fix spacings
baliukr ecdfa9c
add README.md
baliukr 863757c
fix dockerfile name
baliukr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
WAIT_MAMONSU_TIMEOUT=180 | ||
DEFAULT_HOSTGROUP="Zabbix servers" | ||
DEFAULT_TEMPLATE="Mamonsu PostgreSQL Linux" | ||
POSTGRES_VERSION=15 | ||
|
||
# creds | ||
POSTGRES_USER=postgres | ||
POSTGRES_PASSWORD=postgres | ||
POSTGRES_DB=mamonsu_test_db | ||
|
||
ZABBIX_ADMIN_USER=Admin | ||
ZABBIX_ADMIN_PASS=zabbix | ||
|
||
# hosts | ||
ZABBIX_EXT_URL=127.0.0.1:1337 | ||
ZABBIX_INT_URL=zabbix-web:8080 | ||
POSTGRES_EXT_HOST=127.0.0.1 | ||
|
||
# external ports | ||
POSTGRES_EXT_PORT=15432 | ||
MAMONSU_AGENT_EXT_PORT=11050 | ||
ZABBIX_SERVER_EXT_PORT=11051 | ||
ZABBIX_WEB_EXT_PORT=1337 | ||
|
||
# internal ports | ||
POSTGRES_PORT=5432 | ||
MAMONSU_AGENT_PORT=10050 | ||
ZABBIX_SERVER_PORT=10051 | ||
ZABBIX_WEB_PORT=8080 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
|
||
# Mamonsu autotests | ||
|
||
Mamonsu testing with different Postgres version, different operation systems(not supported yet). Uses docker-compose to run all services. | ||
|
||
|
||
## Installation | ||
|
||
|
||
```bash | ||
pip3 install -e requirement.txt | ||
``` | ||
|
||
## Usage/Examples | ||
|
||
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 | ||
|
||
```bash | ||
pytest -m bash | ||
``` | ||
|
||
You can run tests with different Postgres versions with POSTGRES_VERSIONS variable | ||
|
||
```bash | ||
POSTGRES_VERSIONS=12,13 pytest -m bash | ||
``` | ||
|
||
To run specific test you have to use -k flag with function name | ||
|
||
```bash | ||
POSTGRES_VERSIONS=12,13 pytest -k test_export_zabbix_params | ||
``` |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import os | ||
from pathlib import Path | ||
from typing import Any | ||
|
||
from dotenv import load_dotenv | ||
|
||
|
||
class Config: | ||
def __init__(self, env_path: Path | None = None): | ||
self._root_path = Path(__file__).parent.parent | ||
load_dotenv(env_path or self._root_path / ".env") | ||
|
||
def __getattr__(self, name: str) -> Any: | ||
value = os.getenv(name) | ||
if value is None: | ||
return None | ||
return self._convert_value(value) | ||
|
||
@staticmethod | ||
def _convert_value(value: str) -> Any: | ||
if value.lower() in ("true", "false"): | ||
return value.lower() == "true" | ||
try: | ||
return int(value) | ||
except ValueError: | ||
try: | ||
return float(value) | ||
except ValueError: | ||
return value |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from enum import StrEnum | ||
|
||
|
||
class ContainersEnum(StrEnum): | ||
POSTGRES = "mamonsu-pg" | ||
MAMONSU = "mamonsu-pg" | ||
ZABBIX_WEB = "zabbix-web" | ||
ZABBIX_SERVER = "zabbix-server" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Стейдж нужен для того, что бы потом скопировать из него энтрипоинт нужной версии, т.к. в --from= нельзя использовать env | ||
ARG POSTGRES_VERSION=15 | ||
FROM postgres:${POSTGRES_VERSION} AS postgres_base | ||
|
||
FROM debian:bookworm-slim AS builder | ||
RUN apt-get update && \ | ||
apt-get install -y --no-install-recommends \ | ||
curl \ | ||
software-properties-common \ | ||
make \ | ||
dpkg-dev \ | ||
debhelper \ | ||
build-essential \ | ||
python3-dev \ | ||
python3-setuptools && \ | ||
rm -rf /var/lib/apt/lists/* | ||
|
||
WORKDIR /app | ||
COPY . /app | ||
RUN make deb | ||
|
||
FROM postgres:${POSTGRES_VERSION} | ||
|
||
COPY --from=builder /app/mamonsu*.deb /tmp/ | ||
|
||
RUN apt-get update && \ | ||
apt-get install -y --no-install-recommends \ | ||
python3 \ | ||
python3-setuptools \ | ||
sudo \ | ||
&& rm -rf /var/lib/apt/lists/* | ||
RUN dpkg -i /tmp/mamonsu*.deb || apt-get install -f -y && \ | ||
rm /tmp/mamonsu*.deb | ||
RUN mkdir -p /var/log/mamonsu && \ | ||
chown postgres:postgres /var/log/mamonsu && \ | ||
chmod 755 /var/log/mamonsu | ||
|
||
COPY --from=postgres_base /usr/local/bin/docker-entrypoint.sh /usr/local/bin/ | ||
COPY ./tests/service-scripts/mamonsu-pg/mamonsu.conf /etc/mamonsu/agent.conf | ||
COPY ./tests/service-scripts/mamonsu-pg/entrypoint.sh ./tests/service-scripts/mamonsu-pg/init_mamonsu_in_zbx.sh /app/ | ||
|
||
RUN chmod +x /app/entrypoint.sh /app/init_mamonsu_in_zbx.sh | ||
|
||
ENTRYPOINT ["/app/entrypoint.sh"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
services: | ||
mamonsu-pg: | ||
build: | ||
context: . | ||
dockerfile: tests/debian.Dockerfile | ||
args: | ||
POSTGRES_VERSION: ${POSTGRES_VERSION} | ||
container_name: mamonsu-pg | ||
hostname: mamonsu-pg | ||
image: mamonsu-pg | ||
ports: | ||
- "${MAMONSU_AGENT_EXT_PORT}:${MAMONSU_AGENT_PORT}" | ||
- "${POSTGRES_EXT_PORT}:${POSTGRES_PORT}" | ||
environment: | ||
POSTGRES_VERSION: ${POSTGRES_VERSION} | ||
MAMONSU_AGENT_PORT: ${MAMONSU_AGENT_PORT} | ||
POSTGRES_USER: ${POSTGRES_USER} | ||
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD} | ||
POSTGRES_DB: postgres | ||
POSTGRES_HOST_AUTH_METHOD: md5 | ||
ZABBIX_USER: ${ZABBIX_ADMIN_USER} | ||
ZABBIX_PASSWD: ${ZABBIX_ADMIN_PASS} | ||
ZABBIX_URL: http://${ZABBIX_INT_URL}/ | ||
restart: no | ||
|
||
zabbix: | ||
image: zabbix/zabbix-server-pgsql:6.4.13-ubuntu | ||
container_name: zabbix | ||
hostname: zabbix | ||
environment: | ||
- DB_SERVER_HOST=mamonsu-pg | ||
- POSTGRES_USER=${POSTGRES_USER} | ||
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD} | ||
- POSTGRES_DB=zabbix | ||
- PGPASSWORD=${POSTGRES_PASSWORD} | ||
ports: | ||
- "${ZABBIX_SERVER_EXT_PORT}:${ZABBIX_SERVER_PORT}" | ||
depends_on: | ||
- mamonsu-pg | ||
|
||
zabbix-web: | ||
image: zabbix/zabbix-web-nginx-pgsql:6.4.13-ubuntu | ||
container_name: zabbix-web | ||
hostname: zabbix-web | ||
environment: | ||
- DB_SERVER_HOST=mamonsu-pg | ||
- POSTGRES_USER=${POSTGRES_USER} | ||
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD} | ||
- POSTGRES_DB=zabbix | ||
- ZBX_SERVER_HOST=zabbix-server | ||
- ZBX_SERVER_PORT=${ZABBIX_SERVER_PORT} | ||
- ZABBIX_ADMIN_USER=Admin | ||
- ZABBIX_ADMIN_PASS=zabbix | ||
ports: | ||
- "${ZABBIX_WEB_EXT_PORT}:${ZABBIX_WEB_PORT}" | ||
depends_on: | ||
- zabbix | ||
healthcheck: | ||
test: | | ||
curl -fsS "http://localhost:${ZABBIX_WEB_PORT}/api_jsonrpc.php" \ | ||
-X POST \ | ||
-H "Content-Type: application/json-rpc" \ | ||
-d '{"jsonrpc":"2.0","method":"apiinfo.version","id":1,"auth":null,"params":{}}' \ | ||
| grep -q '"result"' || exit 1 | ||
interval: 5s | ||
timeout: 5s | ||
retries: 15 | ||
start_period: 30s |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
|
||
[pytest] | ||
log_cli=true | ||
log_level=INFO | ||
log_format = %(asctime)s %(levelname)s %(message)s | ||
log_date_format = %Y-%m-%d %H:%M:%S | ||
markers = | ||
bash |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pytest==8.3.5 | ||
docker==7.1.0 | ||
zabbix==1.3.1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. почему-то данная версия не ищется: |
||
python-dotenv==1.1.0 | ||
psycopg2==2.9.10 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#!/bin/bash | ||
set -e | ||
|
||
RECOVERY_FILE="standby.signal" | ||
|
||
DATA_DIR=/var/lib/postgresql/data | ||
DATA_SLAVE_PHYSICAL_DIR=/var/lib/postgresql/data_slave_physical | ||
WAL_DIR=/var/lib/postgresql/wals | ||
DATA_SLAVE_LOGICAL_DIR=/var/lib/postgresql/data_slave_logical | ||
|
||
su postgres -c '/usr/local/bin/docker-entrypoint.sh postgres "$@" &' | ||
sleep 5 | ||
su postgres -c "pg_ctl stop -D $DATA_DIR" | ||
|
||
sudo mkdir -p $DATA_SLAVE_PHYSICAL_DIR | ||
sudo mkdir -p $WAL_DIR | ||
sudo chown -R postgres:postgres $DATA_SLAVE_PHYSICAL_DIR $WAL_DIR | ||
sudo chmod 700 $DATA_SLAVE_PHYSICAL_DIR | ||
|
||
sudo -u postgres echo "shared_preload_libraries = 'pg_stat_statements'" >> $DATA_DIR/postgresql.conf | ||
sudo -u postgres echo "pg_stat_statements.track = all" >> $DATA_DIR/postgresql.conf | ||
sudo -u postgres echo "archive_mode=on" >> $DATA_DIR/postgresql.conf | ||
sudo -u postgres echo "archive_command='cp %p $WAL_DIR/%f'" >> $DATA_DIR/postgresql.conf | ||
sudo -u postgres echo "wal_level=replica" >> $DATA_DIR/postgresql.conf | ||
sudo -u postgres echo "max_wal_senders=4" >> $DATA_DIR/postgresql.conf | ||
sudo -u postgres echo "hot_standby=on" >> $DATA_DIR/postgresql.conf | ||
|
||
sudo -u postgres echo "track_io_timing = on" >> $DATA_DIR/postgresql.conf | ||
sudo -u postgres echo "track_functions = all" >> $DATA_DIR/postgresql.conf | ||
|
||
sudo -u postgres echo "host replication replicator 127.0.0.1/0 trust" >> $DATA_DIR/pg_hba.conf | ||
|
||
su postgres -c "pg_ctl start -D $DATA_DIR" | ||
sleep 3 | ||
|
||
sudo -u postgres psql -c "CREATE DATABASE mamonsu_test_db;" | ||
sudo -u postgres psql -d mamonsu_test_db -c "CREATE EXTENSION pg_stat_statements;" | ||
sudo -u postgres psql -d mamonsu_test_db -c "CREATE EXTENSION pg_buffercache;" | ||
sudo -u postgres psql -d mamonsu_test_db -c "CREATE TABLE mamonsu_test_table(id serial, value integer);" | ||
sudo -u postgres psql -d mamonsu_test_db -c "INSERT INTO mamonsu_test_table(value) SELECT * FROM generate_series(1, 10000);" | ||
sudo -u postgres psql -c "CREATE USER replicator WITH REPLICATION ENCRYPTED PASSWORD 'secret';" | ||
sudo -u postgres pg_basebackup -h 127.0.0.1 -U replicator -Fp -Xs -P -R -D $DATA_SLAVE_PHYSICAL_DIR/ | ||
sudo -u postgres sed -i '/^archive_mode/s/^\(.*\)$/#\1/' $DATA_SLAVE_PHYSICAL_DIR/postgresql.conf | ||
sudo -u postgres sed -i '/^archive_command/s/^\(.*\)$/#\1/' $DATA_SLAVE_PHYSICAL_DIR/postgresql.conf | ||
sudo -u postgres echo "port=5433" >> $DATA_SLAVE_PHYSICAL_DIR/postgresql.conf | ||
sudo -u postgres echo "restore_command = 'cp $WAL_DIR/%f %p'" >> $DATA_SLAVE_PHYSICAL_DIR/${RECOVERY_FILE} | ||
|
||
su postgres -c "pg_ctl start -D $DATA_SLAVE_PHYSICAL_DIR" | ||
|
||
# create logical slave | ||
if [ "$POSTGRES_VERSION" -ge 100 ]; then # TODO: Пофиксить, пока что отключено | ||
# create PGDATA directory | ||
sudo mkdir -p $DATA_SLAVE_LOGICAL_DIR | ||
sudo chown postgres:postgres $DATA_SLAVE_LOGICAL_DIR | ||
sudo chmod 700 $DATA_SLAVE_LOGICAL_DIR | ||
|
||
sudo -u postgres sed -i '/^wal_level/s/^\(.*\)$/#\1/' $DATA_DIR/postgresql.conf | ||
sudo -u postgres echo "wal_level=logical" >> $DATA_DIR/postgresql.conf | ||
su postgres -c "pg_ctl restart -D $DATA_DIR" | ||
sleep 3 | ||
sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE mamonsu_test_db TO replicator;" | ||
sudo -u postgres psql -d mamonsu_test_db -c "GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO replicator;" | ||
sudo -u postgres psql -d mamonsu_test_db -c "CREATE PUBLICATION mamonsu_publication;" | ||
sudo -u postgres psql -d mamonsu_test_db -c "ALTER PUBLICATION mamonsu_publication ADD TABLE mamonsu_test_table;" | ||
sudo -u postgres echo "host all all 127.0.0.1/0 trust" >> $DATA_SLAVE_LOGICAL_DIR/pg_hba.conf | ||
sudo -u postgres echo "port=5434" >> $DATA_SLAVE_LOGICAL_DIR/postgresql.conf | ||
su postgres -c "pg_ctl start -D $DATA_SLAVE_LOGICAL_DIR" | ||
sleep 3 | ||
sudo -u postgres psql -p 5434 -c "CREATE DATABASE mamonsu_test_db;" | ||
sudo -u postgres psql -p 5434 -d mamonsu_test_db -c "CREATE TABLE mamonsu_test_table(id serial, value integer);" | ||
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;" | ||
fi | ||
|
||
mamonsu bootstrap -x --user postgres -d mamonsu_test_db | ||
service mamonsu restart | ||
|
||
tail -f /dev/null |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#!/bin/sh | ||
|
||
INIT_MARKER="/app/.init_done" | ||
if [ ! -f "$INIT_MARKER" ]; then | ||
echo "[INFO] Exporting templates" | ||
mamonsu export template template.xml | ||
mamonsu zabbix template export template.xml | ||
|
||
echo "[INFO] Adding host in Zabbix" | ||
mamonsu zabbix host create "$(hostname)" \ | ||
"$(mamonsu zabbix hostgroup id "Zabbix servers")" \ | ||
"$(mamonsu zabbix template id "Mamonsu PostgreSQL Linux")" \ | ||
"$(getent hosts "$(hostname)" | awk '{print $1}')" | ||
service mamonsu start | ||
|
||
echo "[INFO] Waiting for host to appear in Zabbix" | ||
sleep 5 | ||
touch "$INIT_MARKER" | ||
else | ||
echo "[INFO] Initialization already done. Skipping Mamonsu setup" | ||
fi | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
класс StrEnum доступен начиная с python3.11, соответственно, необходимо отразить в README, что для запуска тестов необходим python3.11+ и не забыть это в дальнейшем учесть при добавлении запуска данных тестов в github-actions.