Skip to content
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions .github/workflows/e2e-py-check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: e2e-tests Python Quality Check

on:
pull_request:
paths:
- 'e2e-tests/**/*.py'

jobs:
quality-check:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Install uv
uses: astral-sh/setup-uv@v5

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version-file: "pyproject.toml"

- name: Install dependencies
run: uv sync --locked

- name: Run ruff check
run: uv run ruff check e2e-tests/

- name: Run mypy
run: uv run mypy e2e-tests/
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,7 @@ bin/
projects/
installers/olm/operator_*.yaml
installers/olm/bundles

# Test Reports
e2e-tests/reports/
e2e-tests/**/__pycache__/
346 changes: 346 additions & 0 deletions e2e-tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,346 @@
import os
import pytest
import subprocess
import logging
import yaml
import json
import time
import random

from pathlib import Path
from concurrent.futures import ThreadPoolExecutor

import tools

logging.getLogger("pytest_dependency").setLevel(logging.WARNING)
logger = logging.getLogger(__name__)


@pytest.fixture(scope="session", autouse=True)
def setup_env_vars():
"""Setup environment variables for the test session."""
git_branch = tools.get_git_branch()
git_version, kube_version = tools.get_kubernetes_versions()

os.environ.setdefault("KUBE_VERSION", kube_version)
os.environ.setdefault("EKS", "1" if "eks" in git_version else "0")
os.environ.setdefault("GKE", "1" if "gke" in git_version else "0")
os.environ.setdefault("OPENSHIFT", "0")

os.environ.setdefault("API", "psmdb.percona.com/v1")
os.environ.setdefault("GIT_COMMIT", tools.get_git_commit())
os.environ.setdefault("GIT_BRANCH", git_branch)
os.environ.setdefault("OPERATOR_VERSION", tools.get_cr_version())
os.environ.setdefault("IMAGE", f"perconalab/percona-server-mongodb-operator:{git_branch}")
os.environ.setdefault(
"IMAGE_MONGOD", "perconalab/percona-server-mongodb-operator:main-mongod7.0"
)
os.environ.setdefault(
"IMAGE_MONGOD_CHAIN",
"perconalab/percona-server-mongodb-operator:main-mongod6.0\n"
"perconalab/percona-server-mongodb-operator:main-mongod7.0\n"
"perconalab/percona-server-mongodb-operator:main-mongod8.0",
)
os.environ.setdefault("IMAGE_BACKUP", "perconalab/percona-server-mongodb-operator:main-backup")
os.environ.setdefault("IMAGE_PMM_CLIENT", "percona/pmm-client:2.44.1-1")
os.environ.setdefault("IMAGE_PMM_SERVER", "percona/pmm-server:2.44.1-1")
os.environ.setdefault("IMAGE_PMM3_CLIENT", "perconalab/pmm-client:3.1.0")
os.environ.setdefault("IMAGE_PMM3_SERVER", "perconalab/pmm-server:3.1.0")

os.environ.setdefault("CERT_MANAGER_VER", "1.18.2")
os.environ.setdefault("CHAOS_MESH_VER", "2.7.1")
os.environ.setdefault("MINIO_VER", "5.4.0")
os.environ.setdefault("PMM_SERVER_VER", "9.9.9")

os.environ.setdefault("CLEAN_NAMESPACE", "0")
os.environ.setdefault("DELETE_CRD_ON_START", "1")
os.environ.setdefault("SKIP_DELETE", "0")
os.environ.setdefault("SKIP_BACKUPS_TO_AWS_GCP_AZURE", "1")
os.environ.setdefault("UPDATE_COMPARE_FILES", "0")


@pytest.fixture(scope="class")
def test_paths(request):
"""Fixture to provide paths relative to the test file."""
test_file = Path(request.fspath)
test_dir = test_file.parent
conf_dir = test_dir.parent / "conf"
src_dir = test_dir.parent.parent

return {"test_file": test_file, "test_dir": test_dir, "conf_dir": conf_dir, "src_dir": src_dir}


@pytest.fixture(scope="class")
def create_namespace():
def _create_namespace(namespace):
"""Create kubernetes namespace and clean up if exists."""
operator_ns = os.environ.get("OPERATOR_NS")

if int(os.environ.get("CLEAN_NAMESPACE")):
tools.clean_all_namespaces()

if int(os.environ.get("OPENSHIFT")):
logger.info("Cleaning up all old namespaces from openshift")

if operator_ns:
try:
result = subprocess.run(
["oc", "get", "project", operator_ns, "-o", "json"],
capture_output=True,
text=True,
check=False,
)

if result.returncode == 0:
project_data = json.loads(result.stdout)
if project_data.get("metadata", {}).get("name"):
subprocess.run(
[
"oc",
"delete",
"--grace-period=0",
"--force=true",
"project",
namespace,
],
check=False,
)
time.sleep(120)
else:
subprocess.run(["oc", "delete", "project", namespace], check=False)
time.sleep(40)
except Exception:
pass

logger.info(f"Create namespace {namespace}")
subprocess.run(["oc", "new-project", namespace], check=True)
subprocess.run(["oc", "project", namespace], check=True)
subprocess.run(
["oc", "adm", "policy", "add-scc-to-user", "hostaccess", "-z", "default"],
check=False,
)
else:
logger.info("Cleaning up existing namespace")

# Delete namespace if exists
try:
tools.kubectl_bin("delete", "namespace", namespace, "--ignore-not-found")
tools.kubectl_bin("wait", "--for=delete", f"namespace/{namespace}")
except subprocess.CalledProcessError:
pass

logger.info(f"Create namespace {namespace}")
tools.kubectl_bin("create", "namespace", namespace)
tools.kubectl_bin("config", "set-context", "--current", f"--namespace={namespace}")
return namespace

return _create_namespace


@pytest.fixture(scope="class")
def create_infra(test_paths, create_namespace):
def _create_infra(test_name):
"""Create the necessary infrastructure for the tests."""
logger.info("Creating test environment")
if os.environ.get("DELETE_CRD_ON_START") == "1":
tools.delete_crd_rbac(test_paths["src_dir"])
tools.check_crd_for_deletion(f"{test_paths['src_dir']}/deploy/crd.yaml")

if os.environ.get("OPERATOR_NS"):
create_namespace(os.environ.get("OPERATOR_NS"))
tools.deploy_operator(test_paths["test_dir"], test_paths["src_dir"])
namespace = create_namespace(f"{test_name}-{random.randint(0, 32767)}")
else:
namespace = create_namespace(f"{test_name}-{random.randint(0, 32767)}")
tools.deploy_operator(test_paths["test_dir"], test_paths["src_dir"])

return namespace

return _create_infra


@pytest.fixture(scope="class")
def destroy_infra(test_paths):
"""Destroy the infrastructure created for the tests."""

def _destroy_infra(namespace):
if os.environ.get("SKIP_DELETE") == "1":
logger.info("SKIP_DELETE = 1. Skipping test environment cleanup")
return

def run_cmd(cmd):
try:
tools.kubectl_bin(*cmd)
except (subprocess.CalledProcessError, FileNotFoundError, OSError) as e:
logger.debug(f"Command failed (continuing cleanup): {' '.join(cmd)}, error: {e}")

def cleanup_crd():
crd_file = f"{test_paths['src_dir']}/deploy/crd.yaml"
run_cmd(["delete", "-f", crd_file, "--ignore-not-found", "--wait=false"])

try:
with open(crd_file, "r") as f:
for doc in f.read().split("---"):
if not doc.strip():
continue
crd_name = yaml.safe_load(doc)["metadata"]["name"]
run_cmd(
[
"patch",
crd_name,
"--all-namespaces",
"--type=merge",
"-p",
'{"metadata":{"finalizers":[]}}',
]
)
run_cmd(["wait", "--for=delete", "crd", crd_name])
except (FileNotFoundError, yaml.YAMLError, KeyError, TypeError) as e:
logger.debug(f"CRD cleanup failed (continuing): {e}")

logger.info("Cleaning up test environment")

commands = [
["delete", "psmdb-backup", "--all", "--ignore-not-found"],
[
"delete",
"-f",
f"{test_paths['test_dir']}/../conf/container-rc.yaml",
"--ignore-not-found",
],
[
"delete",
"-f",
f"{test_paths['src_dir']}/deploy/{'cw-' if os.environ.get('OPERATOR_NS') else ''}rbac.yaml",
"--ignore-not-found",
],
]

with ThreadPoolExecutor(max_workers=3) as executor:
futures = [executor.submit(run_cmd, cmd) for cmd in commands]
futures.append(executor.submit(cleanup_crd))

namespace_commands = [
["delete", "--grace-period=0", "--force", "namespace", namespace, "--ignore-not-found"]
]
if os.environ.get("OPERATOR_NS"):
namespace_commands.append(
[
"delete",
"--grace-period=0",
"--force",
"namespace",
os.environ.get("OPERATOR_NS"),
"--ignore-not-found",
]
)

for cmd in namespace_commands:
run_cmd(cmd)

return _destroy_infra


@pytest.fixture(scope="class")
def deploy_chaos_mesh(namespace):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could have separate file for helm installed dependencies and use a generic function with parameters to install a helm chart since subprocess is going to be used fot it

"""Deploy Chaos Mesh and clean up after tests."""
try:
subprocess.run(
["helm", "repo", "add", "chaos-mesh", "https://charts.chaos-mesh.org"], check=True
)
subprocess.run(["helm", "repo", "update"], check=True)
subprocess.run(
[
"helm",
"install",
"chaos-mesh",
"chaos-mesh/chaos-mesh",
"--namespace",
namespace,
"--version",
os.environ["CHAOS_MESH_VER"],
"--set",
"dashboard.create=false",
"--set",
"chaosDaemon.runtime=containerd",
"--set",
"chaosDaemon.socketPath=/run/containerd/containerd.sock",
"--wait",
],
check=True,
)

except subprocess.CalledProcessError as e:
try:
subprocess.run(
[
"helm",
"uninstall",
"chaos-mesh",
"--namespace",
namespace,
"--ignore-not-found",
"--wait",
"--timeout",
"60s",
]
)
except (subprocess.CalledProcessError, FileNotFoundError, OSError) as cleanup_error:
logger.warning(f"Failed to cleanup chaos-mesh during error handling: {cleanup_error}")
raise e

yield

try:
subprocess.run(
[
"helm",
"uninstall",
"chaos-mesh",
"--namespace",
namespace,
"--wait",
"--timeout",
"60s",
],
check=True,
)
except subprocess.CalledProcessError as e:
logger.error(f"Failed to cleanup chaos-mesh: {e}")


@pytest.fixture(scope="class")
def deploy_cert_manager():
"""Deploy Cert Manager and clean up after tests."""
logger.info("Deploying cert-manager")
cert_manager_url = f"https://github.com/cert-manager/cert-manager/releases/download/v{os.environ.get('CERT_MANAGER_VER')}/cert-manager.yaml"
try:
tools.kubectl_bin("create", "namespace", "cert-manager")
tools.kubectl_bin(
"label", "namespace", "cert-manager", "certmanager.k8s.io/disable-validation=true"
)
tools.kubectl_bin("apply", "-f", cert_manager_url, "--validate=false")
tools.kubectl_bin(
"wait",
"pod",
"-l",
"app.kubernetes.io/instance=cert-manager",
"--for=condition=ready",
"-n",
"cert-manager",
)
except Exception as e:
try:
tools.kubectl_bin("delete", "-f", cert_manager_url, "--ignore-not-found")
except (subprocess.CalledProcessError, FileNotFoundError, OSError) as cleanup_error:
logger.warning(
f"Failed to cleanup cert-manager during error handling: {cleanup_error}"
)
raise e

yield

try:
tools.kubectl_bin("delete", "-f", cert_manager_url, "--ignore-not-found")
except Exception as e:
logger.error(f"Failed to cleanup cert-manager: {e}")
Loading
Loading