Skip to content

Commit

Permalink
Use qe-common libs
Browse files Browse the repository at this point in the history
  • Loading branch information
mlabonte-rh committed Aug 21, 2024
1 parent 40cd763 commit 15de7b7
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 121 deletions.
114 changes: 60 additions & 54 deletions tests/interop/test_subscription_status_hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,68 +56,74 @@ def test_subscription_status_hub(openshift_dyn_client):

cluster_version = subscription.openshift_version(openshift_dyn_client)
logger.info(f"Openshift version:\n{cluster_version.instance.status.history}")
shortversion = re.sub("(.[0-9]+$)", "", os.getenv("OPENSHIFT_VER"))

currentfile = os.getcwd() + "/operators_hub_current"
sourceFile = open(currentfile, "w")
for line in operator_versions:
logger.info(line)
print(line, file=sourceFile)
sourceFile.close()

logger.info("Clone operator-versions repo")
try:
operator_versions_repo = (
"git@gitlab.cee.redhat.com:mpqe/mps/vp/operator-versions.git"
)
clone = subprocess.run(
["git", "clone", operator_versions_repo], capture_output=True, text=True
)
logger.info(clone.stdout)
logger.info(clone.stderr)
except Exception:
pass

previouspath = os.getcwd() + f"/operator-versions/medicaldiag_hub_{shortversion}"
previousfile = f"medicaldiag_hub_{shortversion}"

logger.info("Ensure previous file exists")
checkpath = os.path.exists(previouspath)
logger.info(checkpath)

if checkpath is True:
logger.info("Diff current operator list with previous file")
diff = opdiff(open(previouspath).readlines(), open(currentfile).readlines())
diffstring = "".join(diff)
logger.info(diffstring)

logger.info("Write diff to file")
sourceFile = open("operator_diffs_hub.log", "w")
print(diffstring, file=sourceFile)

if os.getenv("EXTERNAL_TEST") != "true":
shortversion = re.sub("(.[0-9]+$)", "", os.getenv("OPENSHIFT_VER"))
currentfile = os.getcwd() + "/operators_hub_current"
sourceFile = open(currentfile, "w")
for line in operator_versions:
logger.info(line)
print(line, file=sourceFile)
sourceFile.close()
else:
logger.info("Skipping operator diff - previous file not found")

logger.info("Clone operator-versions repo")
try:
operator_versions_repo = (
"git@gitlab.cee.redhat.com:mpqe/mps/vp/operator-versions.git"
)
clone = subprocess.run(
["git", "clone", operator_versions_repo], capture_output=True, text=True
)
logger.info(clone.stdout)
logger.info(clone.stderr)
except Exception:
pass

previouspath = (
os.getcwd() + f"/operator-versions/medicaldiag_hub_{shortversion}"
)
previousfile = f"medicaldiag_hub_{shortversion}"

logger.info("Ensure previous file exists")
checkpath = os.path.exists(previouspath)
logger.info(checkpath)

if checkpath is True:
logger.info("Diff current operator list with previous file")
diff = opdiff(open(previouspath).readlines(), open(currentfile).readlines())
diffstring = "".join(diff)
logger.info(diffstring)

logger.info("Write diff to file")
sourceFile = open("operator_diffs_hub.log", "w")
print(diffstring, file=sourceFile)
sourceFile.close()
else:
logger.info("Skipping operator diff - previous file not found")

if missing_subs or unhealthy_subs or missing_installplans or upgrades_pending:
err_msg = "Subscription status check failed"
logger.error(f"FAIL: {err_msg}")
assert False, err_msg
else:
# Only push the new operarator list if the test passed
if checkpath is True:
os.remove(previouspath)
os.rename(currentfile, previouspath)

cwd = os.getcwd() + "/operator-versions"
logger.info(f"CWD: {cwd}")

logger.info("Push new operator list")
subprocess.run(["git", "add", previousfile], cwd=cwd)
subprocess.run(
["git", "commit", "-m", "Update operator versions list"],
cwd=cwd,
)
subprocess.run(["git", "push"], cwd=cwd)
# and we are not testing a pre-release operator nor
# running externally
if os.getenv("EXTERNAL_TEST") != "true":
if checkpath is True and not os.environ["INDEX_IMAGE"]:
os.remove(previouspath)
os.rename(currentfile, previouspath)

cwd = os.getcwd() + "/operator-versions"
logger.info(f"CWD: {cwd}")

logger.info("Push new operator list")
subprocess.run(["git", "add", previousfile], cwd=cwd)
subprocess.run(
["git", "commit", "-m", "Update operator versions list"],
cwd=cwd,
)
subprocess.run(["git", "push"], cwd=cwd)

logger.info("PASS: Subscription status check passed")

Expand Down
91 changes: 24 additions & 67 deletions tests/interop/test_validate_hub_site_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,8 @@
import os

import pytest
from ocp_resources.route import Route
from ocp_resources.storage_class import StorageClass
from openshift.dynamic.exceptions import NotFoundError
from validatedpatterns_tests.interop import components
from validatedpatterns_tests.interop.crd import ArgoCD
from validatedpatterns_tests.interop.edge_util import (
get_long_live_bearer_token,
get_site_response,
)
from validatedpatterns_tests.interop import application, components

from . import __loggername__

Expand Down Expand Up @@ -47,26 +40,16 @@ def test_validate_hub_site_components(openshift_dyn_client):
@pytest.mark.validate_hub_site_reachable
def test_validate_hub_site_reachable(kube_config, openshift_dyn_client):
logger.info("Check if hub site API end point is reachable")
hub_api_url = kube_config.host
if not hub_api_url:
err_msg = "Hub site url is missing in kubeconfig file"
logger.error(f"FAIL: {err_msg}")
assert False, err_msg
else:
logger.info(f"HUB api url : {hub_api_url}")

bearer_token = get_long_live_bearer_token(
dyn_client=openshift_dyn_client,
namespace="openshift-gitops",
sub_string="argocd-dex-server-token",
)

if not bearer_token:
assert False, "Bearer token is missing for argocd-dex-server"

hub_api_response = get_site_response(
site_url=hub_api_url, bearer_token=bearer_token
)
namespace = "openshift-gitops"
sub_string = "argocd-dex-server-token"
try:
hub_api_url = application.get_site_api_url(kube_config)
hub_api_response = application.get_site_api_response(
openshift_dyn_client, hub_api_url, namespace, sub_string
)
except AssertionError as e:
logger.error(f"FAIL: {e}")
assert False, e

if hub_api_response.status_code != 200:
err_msg = "Hub site is not reachable. Please check the deployment."
Expand Down Expand Up @@ -122,37 +105,24 @@ def test_check_pod_status(openshift_dyn_client):
@pytest.mark.validate_argocd_reachable_hub_site
def test_validate_argocd_reachable_hub_site(openshift_dyn_client):
namespace = "openshift-gitops"
name = "openshift-gitops-server"
sub_string = "argocd-dex-server-token"
logger.info("Check if argocd route/url on hub site is reachable")
try:
for route in Route.get(
dyn_client=openshift_dyn_client,
namespace=namespace,
name="openshift-gitops-server",
):
argocd_route_url = route.instance.spec.host
except NotFoundError:
argocd_route_url = application.get_argocd_route_url(
openshift_dyn_client, namespace, name
)
argocd_route_response = application.get_site_api_response(
openshift_dyn_client, argocd_route_url, namespace, sub_string
)
except StopIteration:
err_msg = "Argocd url/route is missing in open-cluster-management namespace"
logger.error(f"FAIL: {err_msg}")
assert False, err_msg

final_argocd_url = f"{'http://'}{argocd_route_url}"
logger.info(f"ACM route/url : {final_argocd_url}")

bearer_token = get_long_live_bearer_token(
dyn_client=openshift_dyn_client,
namespace=namespace,
sub_string="argocd-dex-server-token",
)
if not bearer_token:
except AssertionError:
err_msg = "Bearer token is missing for argocd-dex-server"
logger.error(f"FAIL: {err_msg}")
assert False, err_msg
else:
logger.debug(f"Argocd bearer token : {bearer_token}")

argocd_route_response = get_site_response(
site_url=final_argocd_url, bearer_token=bearer_token
)

logger.info(f"Argocd route response : {argocd_route_response}")

Expand All @@ -170,22 +140,9 @@ def test_validate_argocd_applications_health_hub_site(openshift_dyn_client):
logger.info("Get all applications deployed by argocd on hub site")
projects = ["openshift-gitops", "medical-diagnosis-hub"]
for project in projects:
for app in ArgoCD.get(dyn_client=openshift_dyn_client, namespace=project):
app_name = app.instance.metadata.name
app_health = app.instance.status.health.status
app_sync = app.instance.status.sync.status

logger.info(f"Status for {app_name} : {app_health} : {app_sync}")

if "Healthy" != app_health or "Synced" != app_sync:
logger.info(f"Dumping failed resources for app: {app_name}")
unhealthy_apps.append(app_name)
for res in app.instance.status.resources:
if (
res.health and res.health.status != "Healthy"
) or res.status != "Synced":
logger.info(f"\n{res}")

unhealthy_apps += application.get_argocd_application_status(
openshift_dyn_client, project
)
if unhealthy_apps:
err_msg = "Some or all applications deployed on hub site are unhealthy"
logger.error(f"FAIL: {err_msg}:\n{unhealthy_apps}")
Expand Down

0 comments on commit 15de7b7

Please sign in to comment.