-
Notifications
You must be signed in to change notification settings - Fork 174
CLOUD-929 - Initial pytest test migration #2058
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
Draft
jvpasinatto
wants to merge
24
commits into
main
Choose a base branch
from
pytest-complete
base: main
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.
+3,010
−33
Draft
Changes from 2 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
f3e089d
Initial pytest test migration
jvpasinatto 7bdf097
Merge branch 'main' into pytest-complete
jvpasinatto fb96a0c
Refactor to adress comments
jvpasinatto 1fc136f
Merge branch 'main' into pytest-complete
jvpasinatto 37aa18d
Make wait more robust
jvpasinatto c784bf0
Add liveness test
jvpasinatto 4b4bf43
Merge branch 'main' into pytest-complete
gkech cafa5f4
Add python rules in makefile
jvpasinatto ab14a77
update dependencies
jvpasinatto 8fabdbe
Add more type hints
jvpasinatto 9195e8a
Print env vars in test initialization and more
jvpasinatto b572b0d
Add resources collection on failure
jvpasinatto 44858f8
move python scripts to folder
jvpasinatto ad7521d
fix liveness test and add more type hints
jvpasinatto 9adffbc
fix init deploy test
jvpasinatto 70d4f06
add py-fmt rule
jvpasinatto 974c0d1
Update test readme and small fixes
jvpasinatto 11eed52
use rich to handle logging
jvpasinatto 8a100f3
Add bash wrapper
jvpasinatto ad60319
update lock file
jvpasinatto ba739c0
fix openshift detection
jvpasinatto a8cd1d4
divide tools into separated files plus improvements
jvpasinatto 079499c
Fix report generation
jvpasinatto 9a7560b
Merge branch 'main' into pytest-complete
jvpasinatto 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
| 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/ |
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,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): | ||
|
||
| """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( | ||
jvpasinatto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| [ | ||
| "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}") | ||
Oops, something went wrong.
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.
UPDATE_COMPARE_FILESisn't implemented in this PR