|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import pytest |
| 4 | +import logging |
| 5 | +import re |
| 6 | + |
| 7 | +import tools |
| 8 | + |
| 9 | +logger = logging.getLogger(__name__) |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture(scope="class", autouse=True) |
| 13 | +def config(create_infra): |
| 14 | + """Configuration for tests""" |
| 15 | + return { |
| 16 | + "namespace": create_infra("liveness"), |
| 17 | + "cluster": "liveness", |
| 18 | + } |
| 19 | + |
| 20 | + |
| 21 | +@pytest.fixture(scope="class", autouse=True) |
| 22 | +def setup_tests(test_paths): |
| 23 | + """Setup test environment""" |
| 24 | + tools.kubectl_bin("apply", "-f", f"{test_paths['conf_dir']}/secrets_with_tls.yml") |
| 25 | + |
| 26 | + |
| 27 | +class TestLiveness: |
| 28 | + @pytest.mark.dependency() |
| 29 | + def test_create_first_cluster(self, config, test_paths): |
| 30 | + """Create first PSMDB cluster""" |
| 31 | + tools.apply_cluster(f"{test_paths['test_dir']}/conf/{config['cluster']}-rs0.yml") |
| 32 | + tools.wait_for_running(f"{config['cluster']}-rs0", 3) |
| 33 | + |
| 34 | + tools.compare_kubectl( |
| 35 | + test_paths["test_dir"], f"statefulset/{config['cluster']}-rs0", config["namespace"] |
| 36 | + ) |
| 37 | + |
| 38 | + @pytest.mark.dependency(depends=["TestLiveness::test_create_first_cluster"]) |
| 39 | + def test_liveness_check_fails_with_invalid_ssl_option(self, config): |
| 40 | + tools.kubectl_bin( |
| 41 | + "exec", |
| 42 | + f"{config['cluster']}-rs0-0", |
| 43 | + "-c", |
| 44 | + "mongod", |
| 45 | + "--", |
| 46 | + "bash", |
| 47 | + "-c", |
| 48 | + "/opt/percona/mongodb-healthcheck k8s liveness --ssl", |
| 49 | + check=False, |
| 50 | + ) |
| 51 | + |
| 52 | + logs_output = tools.kubectl_bin( |
| 53 | + "exec", |
| 54 | + f"{config['cluster']}-rs0-0", |
| 55 | + "-c", |
| 56 | + "mongod", |
| 57 | + "--", |
| 58 | + "bash", |
| 59 | + "-c", |
| 60 | + "ls /data/db/mongod-data/logs", |
| 61 | + ) |
| 62 | + log_count = logs_output.count("mongodb-healthcheck.log") |
| 63 | + assert log_count == 1, f"Expected 1 healthcheck log file, got {log_count}" |
| 64 | + |
| 65 | + rotated_count = len(re.findall(r"mongodb-healthcheck-.*\.log\.gz", logs_output)) |
| 66 | + assert rotated_count >= 1, f"Expected >=1 rotated logs, got {rotated_count}" |
| 67 | + |
| 68 | + @pytest.mark.dependency( |
| 69 | + depends=["TestLiveness::test_liveness_check_fails_with_invalid_ssl_option"] |
| 70 | + ) |
| 71 | + def test_change_liveness_config(self, config, test_paths): |
| 72 | + tools.apply_cluster(f"{test_paths['test_dir']}/conf/{config['cluster']}-rs0-changed.yml") |
| 73 | + |
| 74 | + tools.wait_for_running(f"{config['cluster']}-rs0", 3) |
| 75 | + |
| 76 | + tools.compare_kubectl( |
| 77 | + test_paths["test_dir"], |
| 78 | + f"statefulset/{config['cluster']}-rs0", |
| 79 | + config["namespace"], |
| 80 | + "-changed", |
| 81 | + ) |
0 commit comments