diff --git a/tests/ci/helpers.py b/tests/ci/helpers.py new file mode 100644 index 00000000..328cce32 --- /dev/null +++ b/tests/ci/helpers.py @@ -0,0 +1,88 @@ +import os + +import pytest +import requests +from ocp_resources.gateway_gateway_networking_k8s_io import Gateway +from ocp_resources.route import Route + + +def route_url(openshift_dyn_client, namespace, name): + """Return http(s) URL for a named OpenShift Route.""" + routes = [ + route + for route in Route.get( + dyn_client=openshift_dyn_client, namespace=namespace, name=name + ) + ] + + assert ( + len(routes) == 1 + ), f"Expected to find the route '{name}' in the namespace '{namespace}'" + + spec = routes[0].instance.spec + scheme = "https" if getattr(spec, "tls", None) else "http" + return f"{scheme}://{spec.host}" + + +def gateway_url(openshift_dyn_client, namespace, name): + """Return http(s) URL for a named Gateway API Gateway address.""" + gateways = [ + gateway + for gateway in Gateway.get( + dyn_client=openshift_dyn_client, namespace=namespace, name=name + ) + ] + + assert ( + len(gateways) == 1 + ), f"Expected to find the gateway '{name}' in the namespace '{namespace}'" + + instance = gateways[0].instance + addresses = getattr(instance.status, "addresses", None) or [] + assert ( + addresses + ), f"Expected gateway '{namespace}/{name}' to have at least one status address" + + listeners = getattr(instance.spec, "listeners", None) or [] + scheme = "https" + if listeners and str(listeners[0].protocol).upper() == "HTTP": + scheme = "http" + + return f"{scheme}://{addresses[0].value}" + + +def assert_url_reachable(url, timeout=60): + response = requests.get(url, verify=False, timeout=timeout) + assert ( + response.status_code == 200 + ), f"Expected a 200 from '{url}' but got {response.status_code}" + + +def kubeadmin_password(): + """Return kubeadmin password from env or the file next to VP_HUBCONFIG.""" + password = os.getenv("KUBEADMIN_PASSWORD") or os.getenv("VP_KUBEADMIN") + if password: + return password.strip() + + kubeconfig = os.getenv("VP_HUBCONFIG") + if not kubeconfig: + pytest.fail( + "Set KUBEADMIN_PASSWORD or VP_HUBCONFIG (with a sibling admin-password file)" + ) + + candidates = [] + if kubeconfig.endswith("-kubeconfig"): + candidates.append(f"{kubeconfig[: -len('-kubeconfig')]}-admin-password") + candidates.append(os.path.join(os.path.dirname(kubeconfig), "kubeadmin-password")) + + for path in candidates: + if os.path.isfile(path): + with open(path, encoding="utf-8") as handle: + value = handle.read().strip() + if value: + return value + + pytest.fail( + "kubeadmin password not found; set KUBEADMIN_PASSWORD or place " + "-admin-password next to VP_HUBCONFIG" + ) diff --git a/tests/ci/test_hub.py b/tests/ci/test_hub.py index 47f8a5a2..af8a1354 100644 --- a/tests/ci/test_hub.py +++ b/tests/ci/test_hub.py @@ -1,8 +1,30 @@ +import os +import subprocess + import pytest from ocp_resources.virtual_machine import VirtualMachine from openshift.dynamic.exceptions import NotFoundError +from playwright.sync_api import sync_playwright from validatedpatterns_tests.interop import application, components, subscription +from .helpers import ( + kubeadmin_password, + route_url, +) + + +def _results_dir(): + path = os.path.join(os.path.dirname(os.path.abspath(__file__)), ".results") + os.makedirs(path, exist_ok=True) + return path + + +def _openshift_login(page, url, password): + page.goto(url) + page.get_by_label("Username *").fill("kubeadmin") + page.get_by_label("Password *").fill(password) + page.get_by_role("button", name="Log in").click() + @pytest.mark.parametrize( "openshift_dyn_client", @@ -107,3 +129,66 @@ def test_vm_status_hub(openshift_dyn_client): errors.append(f"Unhealthy vms: {', '.join(unhealthy_vms)}") assert not errors, "VM status check failed:\n" + "\n".join(errors) + + +@pytest.mark.parametrize( + "openshift_dyn_client", + ["VP_HUBCONFIG"], + indirect=True, +) +def test_aap_login(openshift_dyn_client): + console_url = route_url(openshift_dyn_client, "openshift-console", "console") + secrets_url = console_url + "/k8s/ns/ansible-automation-platform/core~v1~Secret" + aap_url = route_url(openshift_dyn_client, "ansible-automation-platform", "aap") + password = kubeadmin_password() + + with sync_playwright() as playwright: + browser = playwright.chromium.launch(headless=True) + context = browser.new_context(ignore_https_errors=True) + context.grant_permissions(["clipboard-read", "clipboard-write"]) + context.set_default_timeout(120_000) + page = context.new_page() + try: + page.goto(secrets_url) + _openshift_login(page, secrets_url, password) + + page.wait_for_timeout(5000) + + # Dismiss welcome screen if visible + welcome = page.get_by_role("button", name="Close") + if welcome.is_visible(timeout=60): + welcome.click() + + page.locator('[data-test-id="aap-admin-password"]').click() + page.locator('[data-test="reveal-values"]').click() + page.get_by_role("button", name="Copy to clipboard").click() + + clip = page.evaluate("() => navigator.clipboard.readText()") + + page.goto(aap_url) + page.get_by_role("textbox", name="Username").fill("admin") + page.get_by_role("textbox", name="Password").click(button="right") + page.keyboard.press("ControlOrMeta+V") + page.get_by_role("button", name="Log in").click() + + page.get_by_test_id("platform-overview").click() + page.get_by_role("button", name="Settings").click() + page.get_by_test_id("platform-subscription-details").click() + + finally: + context.close() + browser.close() + + os.environ["KUBECONFIG"] = os.getenv("VP_HUBCONFIG", "") + + # Verify that the script returns the same password + result = subprocess.run( + ["bash", "../../pattern.sh", "../../scripts/ansible_get_credentials.sh"], + capture_output=True, + text=True, + check=False, + ) + + assert result.returncode == 0 + assert clip in result.stdout + assert result.stderr == ""