|
1 | 1 | import os |
| 2 | +from typing import Dict |
2 | 3 | import pytest |
3 | 4 | import subprocess |
4 | 5 | import logging |
|
20 | 21 |
|
21 | 22 |
|
22 | 23 | @pytest.hookimpl(tryfirst=True, hookwrapper=True) |
23 | | -def pytest_runtest_makereport(item, call): |
| 24 | +def pytest_runtest_makereport(item: pytest.Item, call: pytest.CallInfo) -> None: |
24 | 25 | """Collect K8s resources when a test fails.""" |
25 | 26 | outcome = yield |
26 | 27 | report = outcome.get_result() |
@@ -81,7 +82,7 @@ def setup_env_vars() -> None: |
81 | 82 |
|
82 | 83 |
|
83 | 84 | @pytest.fixture(scope="class") |
84 | | -def test_paths(request): |
| 85 | +def test_paths(request: pytest.FixtureRequest) -> Dict[str, Path]: |
85 | 86 | """Fixture to provide paths relative to the test file.""" |
86 | 87 | test_file = Path(request.fspath) |
87 | 88 | test_dir = test_file.parent |
@@ -159,7 +160,7 @@ def _create_namespace(namespace): |
159 | 160 |
|
160 | 161 |
|
161 | 162 | @pytest.fixture(scope="class") |
162 | | -def create_infra(test_paths, create_namespace): |
| 163 | +def create_infra(test_paths: Dict[str, Path], create_namespace): |
163 | 164 | global _current_namespace |
164 | 165 | created_namespaces = [] |
165 | 166 |
|
@@ -191,7 +192,7 @@ def _create_infra(test_name): |
191 | 192 | _current_namespace = None |
192 | 193 |
|
193 | 194 | if os.environ.get("SKIP_DELETE") == "1": |
194 | | - logger.info("SKIP_DELETE = 1. Skipping test environment cleanup") |
| 195 | + logger.info("SKIP_DELETE=1. Skipping test environment cleanup") |
195 | 196 | return |
196 | 197 |
|
197 | 198 | def run_cmd(cmd: list[str]) -> None: |
@@ -375,7 +376,81 @@ def deploy_cert_manager(): |
375 | 376 |
|
376 | 377 |
|
377 | 378 | @pytest.fixture(scope="class") |
378 | | -def psmdb_client(test_paths) -> tools.MongoManager: |
| 379 | +def deploy_minio(): |
| 380 | + """Deploy MinIO and clean up after tests.""" |
| 381 | + service_name = "minio-service" |
| 382 | + bucket = "operator-testing" |
| 383 | + |
| 384 | + logger.info(f"Installing MinIO: {service_name}") |
| 385 | + |
| 386 | + subprocess.run(["helm", "uninstall", service_name], capture_output=True, check=False) |
| 387 | + subprocess.run(["helm", "repo", "remove", "minio"], capture_output=True, check=False) |
| 388 | + subprocess.run(["helm", "repo", "add", "minio", "https://charts.min.io/"], check=True) |
| 389 | + |
| 390 | + endpoint = f"http://{service_name}:9000" |
| 391 | + minio_args = [ |
| 392 | + "helm", "install", service_name, "minio/minio", |
| 393 | + "--version", os.environ.get("MINIO_VER"), |
| 394 | + "--set", "replicas=1", |
| 395 | + "--set", "mode=standalone", |
| 396 | + "--set", "resources.requests.memory=256Mi", |
| 397 | + "--set", "rootUser=rootuser", |
| 398 | + "--set", "rootPassword=rootpass123", |
| 399 | + "--set", "users[0].accessKey=some-access-key", |
| 400 | + "--set", "users[0].secretKey=some-secret-key", |
| 401 | + "--set", "users[0].policy=consoleAdmin", |
| 402 | + "--set", "service.type=ClusterIP", |
| 403 | + "--set", "configPathmc=/tmp/", |
| 404 | + "--set", "securityContext.enabled=false", |
| 405 | + "--set", "persistence.size=2G", |
| 406 | + "--set", f"fullnameOverride={service_name}", |
| 407 | + "--set", "serviceAccount.create=true", |
| 408 | + "--set", f"serviceAccount.name={service_name}-sa", |
| 409 | + ] |
| 410 | + |
| 411 | + tools.retry(lambda: subprocess.run(minio_args, check=True), max_attempts=10, delay=60) |
| 412 | + |
| 413 | + minio_pod = tools.kubectl_bin( |
| 414 | + "get", "pods", f"--selector=release={service_name}", |
| 415 | + "-o", "jsonpath={.items[].metadata.name}" |
| 416 | + ).strip() |
| 417 | + tools.wait_pod(minio_pod) |
| 418 | + |
| 419 | + operator_ns = os.environ.get("OPERATOR_NS") |
| 420 | + if operator_ns: |
| 421 | + namespace = tools.kubectl_bin( |
| 422 | + "config", "view", "--minify", "-o", "jsonpath={..namespace}" |
| 423 | + ).strip() |
| 424 | + tools.kubectl_bin( |
| 425 | + "create", "svc", "-n", operator_ns, "externalname", service_name, |
| 426 | + f"--external-name={service_name}.{namespace}.svc.cluster.local", |
| 427 | + "--tcp=9000", check=False |
| 428 | + ) |
| 429 | + |
| 430 | + logger.info(f"Creating MinIO bucket: {bucket}") |
| 431 | + tools.kubectl_bin( |
| 432 | + "run", "-i", "--rm", "aws-cli", |
| 433 | + "--image=perconalab/awscli", "--restart=Never", |
| 434 | + "--", "bash", "-c", |
| 435 | + f"AWS_ACCESS_KEY_ID=some-access-key " |
| 436 | + f"AWS_SECRET_ACCESS_KEY=some-secret-key " |
| 437 | + f"AWS_DEFAULT_REGION=us-east-1 " |
| 438 | + f"/usr/bin/aws --no-verify-ssl --endpoint-url {endpoint} s3 mb s3://{bucket}", |
| 439 | + ) |
| 440 | + |
| 441 | + yield |
| 442 | + |
| 443 | + try: |
| 444 | + subprocess.run( |
| 445 | + ["helm", "uninstall", service_name, "--wait", "--timeout", "60s"], |
| 446 | + check=True, |
| 447 | + ) |
| 448 | + except subprocess.CalledProcessError as e: |
| 449 | + logger.warning(f"Failed to cleanup minio: {e}") |
| 450 | + |
| 451 | + |
| 452 | +@pytest.fixture(scope="class") |
| 453 | +def psmdb_client(test_paths: Dict[str, Path]) -> tools.MongoManager: |
379 | 454 | """Deploy and get the client pod name.""" |
380 | 455 | tools.kubectl_bin("apply", "-f", f"{test_paths['conf_dir']}/client-70.yml") |
381 | 456 |
|
|
0 commit comments