CHANGE: Added auto-tests with Docker.

This commit is contained in:
2025-12-16 14:11:46 +01:00
parent cecd55cd3d
commit b1595ee9af
6 changed files with 132 additions and 1 deletions

2
.gitignore vendored
View File

@@ -3,4 +3,4 @@ venv/
export/
*_host_ids.txt
*.log
partitioning/tests/
backup/

View File

@@ -0,0 +1,36 @@
# Zabbix Partitioning Tests
This directory contains a Docker-based test environment for the Zabbix Partitioning script.
## Prerequisites
- Docker & Docker Compose
- Python 3
## Setup & Run
1. Start the database container:
```bash
docker compose up -d
```
This will start a MySQL 8.0 container and import the Zabbix schema.
2. Create valid config (done automatically):
The `test_config.yaml` references the running container.
3. Run the partitioning script:
```bash
# Create virtual environment if needed
python3 -m venv venv
./venv/bin/pip install pymysql pyyaml
# Dry Run
./venv/bin/python3 ../../partitioning/zabbix_partitioning.py -c test_config.yaml --dry-run --init
# Live Run
./venv/bin/python3 ../../partitioning/zabbix_partitioning.py -c test_config.yaml --init
```
## Cleanup
```bash
docker compose down
rm -rf venv
```

View File

@@ -0,0 +1,14 @@
services:
zabbix-db:
image: mysql:8.0
container_name: zabbix-partition-test
environment:
MYSQL_ROOT_PASSWORD: root_password
MYSQL_DATABASE: zabbix
MYSQL_USER: zbx_part
MYSQL_PASSWORD: zbx_password
volumes:
- ../../partitioning/schemas/70-schema-mysql.txt:/docker-entrypoint-initdb.d/schema.sql
ports:
- "33060:3306"
command: --default-authentication-plugin=mysql_native_password

View File

@@ -0,0 +1,31 @@
import re
def get_partitionable_tables(schema_path):
with open(schema_path, 'r', encoding='utf-8', errors='ignore') as f:
content = f.read()
# Split into CREATE TABLE statements
tables = content.split('CREATE TABLE')
valid_tables = []
for table_def in tables:
# Extract table name
name_match = re.search(r'`(\w+)`', table_def)
if not name_match:
continue
table_name = name_match.group(1)
# Check for PRIMARY KEY definition
pk_match = re.search(r'PRIMARY KEY \((.*?)\)', table_def, re.DOTALL)
if pk_match:
pk_cols = pk_match.group(1)
if 'clock' in pk_cols:
valid_tables.append(table_name)
return valid_tables
if __name__ == '__main__':
tables = get_partitionable_tables('/opt/git/Zabbix/partitioning/70-schema-mysql.txt')
print("Partitionable tables (PK contains 'clock'):")
for t in tables:
print(f" - {t}")

View File

@@ -0,0 +1,25 @@
database:
type: mysql
host: 127.0.0.1
socket:
user: root
passwd: root_password
db: zabbix
# Port mapping in docker-compose is 33060
port: 33060
partitions:
daily:
- history: 7d
- history_uint: 7d
- history_str: 7d
- history_log: 7d
- history_text: 7d
- history_bin: 7d
- trends: 365d
- trends_uint: 365d
logging: console
premake: 2
replicate_sql: False
initial_partitioning_start: retention

View File

@@ -0,0 +1,25 @@
import time
import pymysql
import sys
config = {
'host': '127.0.0.1',
'port': 33060,
'user': 'root',
'password': 'root_password',
'database': 'zabbix'
}
max_retries = 90
for i in range(max_retries):
try:
conn = pymysql.connect(**config)
print("Database is ready!")
conn.close()
sys.exit(0)
except Exception as e:
print(f"Waiting for DB... ({e})")
time.sleep(2)
print("Timeout waiting for DB")
sys.exit(1)