Repository navigation
Add ci test for AAP login #162
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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,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 " | ||
| "<cluster>-admin-password next to VP_HUBCONFIG" | ||
| ) |
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 |
|---|---|---|
| @@ -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"], | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you ever do an update here, maybe use file to build an absolute path |
||
| capture_output=True, | ||
| text=True, | ||
| check=False, | ||
| ) | ||
|
|
||
| assert result.returncode == 0 | ||
| assert clip in result.stdout | ||
| assert result.stderr == "" | ||
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.
In a follow up maybe drop the timeout, seems like it is a noop anyways (https://playwright.dev/python/docs/api/class-locator#locator-is-visible)