-
Notifications
You must be signed in to change notification settings - Fork 11
test(MTV-5663): add plan archive PVC cleanup regression test #624
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
Open
myakove
wants to merge
22
commits into
main
Choose a base branch
from
feat/MTV-5663-plan-archive-pvc-cleanup
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.
Open
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
0409455
test(MTV-5663): add plan archive PVC cleanup regression test
myakove a0f48bb
fix: address Qodo review — vm_namespace and idempotent unregister
myakove ec7328a
fix: address CodeRabbit review findings
myakove 50764fa
fix: use consistent vm_namespace in baseline + add test execution con…
myakove 8d78e55
fix: address Qodo cycle 2 — parallel safety caveat + scoped baseline …
myakove 4857030
fix: scope baseline PVC/DV check by session_uuid
myakove 951ce42
fix: move orphan helper to utilities + align parallel safety docs
myakove 9dcc5f8
fix: scope final orphan check by session_uuid
myakove ba9b286
chore: trigger Qodo re-scan for stale sticky finding
myakove 63d732a
fix: add partial_name filter to get_orphan_resource_names
myakove 5dad3f4
fix: handle timeout boundary + update docstring scope
myakove debde7d
fix: inline orphan helper back into test file
myakove 7c1b543
fix: remove wrong session_uuid PVC name filter
myakove b9fb3a1
fix: prefix orphan resource names with kind (PVC/DV)
myakove f7822a6
chore: pre-commit autoupdate (#625)
pre-commit-ci[bot] ce8ddd4
test: add AAP hook integration test (MTV-6031) (#594)
AmenB b061c8e
ci(deps): lock file maintenance (#630)
renovate[bot] 3e634de
chore: trigger Qodo re-scan for stale sticky finding
myakove b75c051
Assert VM cleanup success in test_verify_pvc_cleanup
myakove c1dc10a
Add filelock serialization to aap_mtv_settings and justify Any usage
myakove 806ec50
Refactor AWX instance creation to use Resource kind_dict
myakove 57694bf
Fix AWX instance leak in awx_deployment fixture
myakove 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
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
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,301 @@ | ||
| """ | ||
| Fixtures for AAP (Ansible Automation Platform) hook integration tests. | ||
|
|
||
| Session-scoped fixtures deploy AWX on the cluster and configure MTV | ||
| to use it. Class-scoped fixtures create AAP Hook CRs for each test class. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import tempfile | ||
| from pathlib import Path | ||
| from typing import TYPE_CHECKING, Any, Generator | ||
|
|
||
| import filelock | ||
| import pytest | ||
| from ocp_resources.forklift_controller import ForkliftController | ||
| from ocp_resources.resource import ResourceEditor | ||
| from simple_logger.logger import get_logger | ||
|
|
||
| from utilities.aap import ( | ||
| AAP_TEST_PLAYBOOKS_REPO, | ||
| AWX_ADMIN_USERNAME, | ||
| AWX_NAMESPACE, | ||
| AWX_POSTHOOK_PLAYBOOK, | ||
| AWX_POSTHOOK_TEMPLATE_NAME, | ||
| AWX_PREHOOK_PLAYBOOK, | ||
| AWX_PREHOOK_TEMPLATE_NAME, | ||
| AWX_PROJECT_NAME, | ||
| create_aap_token_secret, | ||
| create_awx_auth_token, | ||
| create_awx_instance, | ||
| create_awx_inventory, | ||
| create_awx_job_template, | ||
| create_awx_project, | ||
| deploy_awx_via_helm, | ||
| get_awx_admin_password, | ||
| is_awx_installed, | ||
| get_awx_route_url, | ||
| teardown_awx, | ||
| wait_for_awx_api_ready, | ||
| wait_for_awx_project_sync, | ||
| wait_for_awx_ready, | ||
| ) | ||
| from utilities.forklift_controller_populator import ensure_secure_shared_lock_dir | ||
| from utilities.hooks import create_hook_for_plan | ||
|
|
||
| if TYPE_CHECKING: | ||
| from kubernetes.dynamic import DynamicClient | ||
|
|
||
| LOGGER = get_logger(__name__) | ||
|
|
||
| FORKLIFT_CONTROLLER_NAME: str = ( | ||
| "forklift-controller" # Well-known name of the ForkliftController CR created by MTV operator | ||
| ) | ||
| AAP_FORKLIFT_CONTROLLER_LOCK_TIMEOUT: int = 3600 # seconds; covers entire session-scoped fixture | ||
|
|
||
|
|
||
| def _get_aap_forklift_controller_lock_path() -> Path: | ||
| """Return the cross-worker lock path for ForkliftController AAP setting changes. | ||
|
|
||
| Returns: | ||
| Path: File lock path under a secured shared temp directory. | ||
| """ | ||
| lock_dir = Path(tempfile.gettempdir()) / "pytest-shared-forklift" | ||
| ensure_secure_shared_lock_dir(lock_dir=lock_dir) | ||
| return lock_dir / "aap-settings.lock" | ||
|
|
||
|
|
||
| @pytest.fixture(scope="session") | ||
| def awx_deployment( | ||
| ocp_admin_client: "DynamicClient", | ||
| fixture_store: dict[str, Any], | ||
| ) -> Generator[str, None, None]: | ||
| """Deploy AWX via Helm and return the route URL. | ||
|
|
||
| Installs the AWX Operator, creates an AWX instance with CephFS storage, | ||
| and waits for all pods to be ready. AWX is a third-party CRD installed | ||
| at runtime — cleanup is handled by namespace deletion in teardown_awx(). | ||
|
|
||
| Args: | ||
| ocp_admin_client: OpenShift admin client. | ||
| fixture_store: Fixture store for resource tracking. | ||
|
|
||
| Yields: | ||
| str: AWX web route URL (https://...). | ||
| """ | ||
| helm_installed_by_us = not is_awx_installed() | ||
| awx_cr_created_by_us = False | ||
| try: | ||
| deploy_awx_via_helm() | ||
| awx_cr_created_by_us = create_awx_instance( | ||
| ocp_admin_client=ocp_admin_client, fixture_store=fixture_store, namespace=AWX_NAMESPACE | ||
| ) | ||
| wait_for_awx_ready(ocp_admin_client=ocp_admin_client, namespace=AWX_NAMESPACE) | ||
| awx_url = get_awx_route_url(ocp_admin_client=ocp_admin_client, namespace=AWX_NAMESPACE) | ||
| password = get_awx_admin_password(ocp_admin_client=ocp_admin_client, namespace=AWX_NAMESPACE) | ||
| wait_for_awx_api_ready(awx_url=awx_url, username=AWX_ADMIN_USERNAME, password=password) | ||
| yield awx_url | ||
| finally: | ||
| if helm_installed_by_us or awx_cr_created_by_us: | ||
| teardown_awx() | ||
|
|
||
|
|
||
| @pytest.fixture(scope="session") | ||
| def awx_api_token( | ||
| ocp_admin_client: "DynamicClient", | ||
| awx_deployment: str, | ||
| ) -> str: | ||
| """Create an AWX OAuth2 API token. | ||
|
|
||
| Args: | ||
| ocp_admin_client: OpenShift admin client. | ||
| awx_deployment: AWX route URL. | ||
|
|
||
| Returns: | ||
| str: OAuth2 token for AWX API authentication. | ||
| """ | ||
| password = get_awx_admin_password( | ||
| ocp_admin_client=ocp_admin_client, | ||
| namespace=AWX_NAMESPACE, | ||
| ) | ||
| return create_awx_auth_token( | ||
| awx_url=awx_deployment, | ||
| username=AWX_ADMIN_USERNAME, | ||
| password=password, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.fixture(scope="session") | ||
| def awx_job_templates( | ||
| awx_deployment: str, | ||
| awx_api_token: str, | ||
| ) -> dict[str, int]: | ||
| """Create AWX project and job templates for pre/post hooks. | ||
|
|
||
| Creates a project from the mtv-aap-test-playbooks git repo, | ||
| waits for SCM sync, then creates two job templates. | ||
|
|
||
| Args: | ||
| awx_deployment: AWX route URL. | ||
| awx_api_token: AWX API token. | ||
|
|
||
| Returns: | ||
| dict[str, int]: Mapping of hook type to template ID: | ||
| ``{"pre_hook": <id>, "post_hook": <id>}`` | ||
| """ | ||
| project_id = create_awx_project( | ||
| awx_url=awx_deployment, | ||
| token=awx_api_token, | ||
| name=AWX_PROJECT_NAME, | ||
| scm_url=AAP_TEST_PLAYBOOKS_REPO, | ||
| ) | ||
| wait_for_awx_project_sync( | ||
| awx_url=awx_deployment, | ||
| token=awx_api_token, | ||
| project_id=project_id, | ||
| ) | ||
|
|
||
| inventory_id = create_awx_inventory( | ||
| awx_url=awx_deployment, | ||
| token=awx_api_token, | ||
| ) | ||
|
|
||
| pre_hook_id = create_awx_job_template( | ||
| awx_url=awx_deployment, | ||
| token=awx_api_token, | ||
| name=AWX_PREHOOK_TEMPLATE_NAME, | ||
| project_id=project_id, | ||
| playbook=AWX_PREHOOK_PLAYBOOK, | ||
| inventory_id=inventory_id, | ||
| ) | ||
| post_hook_id = create_awx_job_template( | ||
| awx_url=awx_deployment, | ||
| token=awx_api_token, | ||
| name=AWX_POSTHOOK_TEMPLATE_NAME, | ||
| project_id=project_id, | ||
| playbook=AWX_POSTHOOK_PLAYBOOK, | ||
| inventory_id=inventory_id, | ||
| ) | ||
|
|
||
| return {"pre_hook": pre_hook_id, "post_hook": post_hook_id} | ||
|
|
||
|
|
||
| @pytest.fixture(scope="session") | ||
| def aap_mtv_settings( | ||
| ocp_admin_client: "DynamicClient", | ||
| fixture_store: dict[str, Any], | ||
| mtv_namespace: str, | ||
| session_uuid: str, | ||
| awx_deployment: str, | ||
| awx_api_token: str, | ||
| ) -> Generator[None, None, None]: | ||
| """Configure MTV to use AWX for AAP hooks. | ||
|
|
||
| Creates a session-unique AWX token Secret in the MTV namespace and patches | ||
| the ForkliftController with ``aap_token_secret_name``, ``aap_url``, and | ||
| ``aap_insecure_skip_verify``. A file lock serializes ForkliftController | ||
| changes across pytest-xdist workers for the entire session duration. | ||
|
|
||
| Args: | ||
| ocp_admin_client: OpenShift admin client. | ||
| fixture_store: Fixture store for resource tracking. | ||
| mtv_namespace: MTV operator namespace (e.g., openshift-mtv). | ||
| session_uuid: Session UUID for unique resource naming. | ||
| awx_deployment: AWX route URL. | ||
| awx_api_token: AWX OAuth2 token. | ||
|
|
||
| Yields: | ||
| None | ||
|
|
||
| Raises: | ||
| TimeoutError: If the cross-worker file lock cannot be acquired. | ||
| """ | ||
| lock_path = _get_aap_forklift_controller_lock_path() | ||
| try: | ||
| with filelock.FileLock(lock_path, timeout=AAP_FORKLIFT_CONTROLLER_LOCK_TIMEOUT): | ||
| token_secret_name = f"{session_uuid}-awx-aap" | ||
| create_aap_token_secret( | ||
| ocp_admin_client=ocp_admin_client, | ||
| fixture_store=fixture_store, | ||
| namespace=mtv_namespace, | ||
| token=awx_api_token, | ||
| name=token_secret_name, | ||
| ) | ||
|
|
||
| forklift_controller = ForkliftController( | ||
| client=ocp_admin_client, | ||
| name=FORKLIFT_CONTROLLER_NAME, | ||
| namespace=mtv_namespace, | ||
| ensure_exists=True, | ||
| ) | ||
|
|
||
| LOGGER.info( | ||
| f"Patching ForkliftController with aap_url='{awx_deployment}', " | ||
| f"aap_token_secret_name='{token_secret_name}', and aap_insecure_skip_verify=true" | ||
| ) | ||
| editor = ResourceEditor( | ||
| patches={ | ||
| forklift_controller: { | ||
| "spec": { | ||
| "aap_url": awx_deployment, | ||
| "aap_token_secret_name": token_secret_name, | ||
| "aap_insecure_skip_verify": "true", | ||
| } | ||
| } | ||
| } | ||
| ) | ||
| editor.update(backup_resources=True) | ||
| try: | ||
| forklift_controller.wait_for_condition( | ||
| status=forklift_controller.Condition.Status.TRUE, | ||
| condition=forklift_controller.Condition.Type.SUCCESSFUL, | ||
| timeout=300, | ||
| ) | ||
| yield | ||
| finally: | ||
| editor.restore() | ||
| except filelock.Timeout as err: | ||
| raise TimeoutError( | ||
| f"Timeout ({AAP_FORKLIFT_CONTROLLER_LOCK_TIMEOUT}s) waiting for ForkliftController " | ||
| f"AAP settings lock at {lock_path}. Another worker may be running AAP hook tests." | ||
| ) from err | ||
|
|
||
|
|
||
| @pytest.fixture(scope="class") | ||
| def aap_hook_refs( | ||
| prepared_plan: dict[str, Any], | ||
| fixture_store: dict[str, Any], | ||
| ocp_admin_client: "DynamicClient", | ||
| target_namespace: str, | ||
| awx_job_templates: dict[str, int], | ||
| aap_mtv_settings: None, | ||
| ) -> None: | ||
| """Create AAP Hook CRs and store references in prepared_plan. | ||
|
|
||
| Creates pre-hook and post-hook Hook CRs with ``spec.aap.jobTemplateId`` | ||
| pointing to AWX job templates. Stores ``_pre_hook_name``, | ||
| ``_pre_hook_namespace``, ``_post_hook_name``, ``_post_hook_namespace`` | ||
| in the prepared_plan dict for ``test_create_plan`` to use. | ||
|
|
||
| Args: | ||
| prepared_plan: The prepared migration plan dict. | ||
| fixture_store: Fixture store for resource tracking. | ||
| ocp_admin_client: OpenShift admin client. | ||
| target_namespace: Target namespace for Hook CR creation. | ||
| awx_job_templates: AWX job template IDs. | ||
| aap_mtv_settings: Ensures MTV is configured with AAP token. | ||
| """ | ||
| for hook_type, template_key in [("pre", "pre_hook"), ("post", "post_hook")]: | ||
| hook_config: dict[str, Any] = { | ||
| "aap_job_template_id": awx_job_templates[template_key], | ||
| } | ||
| hook_name, hook_namespace = create_hook_for_plan( | ||
| hook_config=hook_config, | ||
| hook_type=hook_type, | ||
| fixture_store=fixture_store, | ||
| ocp_admin_client=ocp_admin_client, | ||
| target_namespace=target_namespace, | ||
| ) | ||
| prepared_plan[f"_{hook_type}_hook_name"] = hook_name | ||
| prepared_plan[f"_{hook_type}_hook_namespace"] = hook_namespace |
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.
Uh oh!
There was an error while loading. Please reload this page.