Skip to content

Commit 2ab2c11

Browse files
committed
Updates for code consolidation
1 parent 62e7f50 commit 2ab2c11

File tree

2 files changed

+13
-223
lines changed

2 files changed

+13
-223
lines changed
+4-101
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
import difflib
21
import logging
3-
import os
4-
import re
5-
import subprocess
62

73
import pytest
84
from validatedpatterns_tests.interop import subscription
@@ -23,104 +19,11 @@ def test_subscription_status_hub(openshift_dyn_client):
2319
"ansible-automation-platform-operator": ["ansible-automation-platform"],
2420
}
2521

26-
operator_versions = []
27-
missing_subs = []
28-
unhealthy_subs = []
29-
missing_installplans = []
30-
upgrades_pending = []
31-
32-
(
33-
operator_versions,
34-
missing_subs,
35-
unhealthy_subs,
36-
missing_installplans,
37-
upgrades_pending,
38-
) = subscription.subscription_status(openshift_dyn_client, expected_subs)
39-
40-
if missing_subs:
41-
logger.error(f"FAIL: The following subscriptions are missing: {missing_subs}")
42-
if unhealthy_subs:
43-
logger.error(
44-
"FAIL: The following subscriptions are unhealthy:" f" {unhealthy_subs}"
45-
)
46-
if missing_installplans:
47-
logger.error(
48-
"FAIL: The install plan for the following subscriptions is"
49-
f" missing: {missing_installplans}"
50-
)
51-
if upgrades_pending:
52-
logger.error(
53-
"FAIL: The following subscriptions are in UpgradePending state:"
54-
f" {upgrades_pending}"
55-
)
56-
57-
cluster_version = subscription.openshift_version(openshift_dyn_client)
58-
logger.info(f"Openshift version:\n{cluster_version.instance.status.history}")
59-
shortversion = re.sub("(.[0-9]+$)", "", os.getenv("OPENSHIFT_VER"))
60-
61-
currentfile = os.getcwd() + "/operators_hub_current"
62-
sourceFile = open(currentfile, "w")
63-
for line in operator_versions:
64-
logger.info(line)
65-
print(line, file=sourceFile)
66-
sourceFile.close()
67-
68-
logger.info("Clone operator-versions repo")
69-
try:
70-
operator_versions_repo = (
71-
"[email protected]:mpqe/mps/vp/operator-versions.git"
72-
)
73-
clone = subprocess.run(
74-
["git", "clone", operator_versions_repo], capture_output=True, text=True
75-
)
76-
logger.info(clone.stdout)
77-
logger.info(clone.stderr)
78-
except Exception:
79-
pass
80-
81-
previouspath = os.getcwd() + f"/operator-versions/aegitops_hub_{shortversion}"
82-
previousfile = f"aegitops_hub_{shortversion}"
83-
84-
logger.info("Ensure previous file exists")
85-
checkpath = os.path.exists(previouspath)
86-
logger.info(checkpath)
87-
88-
if checkpath is True:
89-
logger.info("Diff current operator list with previous file")
90-
diff = opdiff(open(previouspath).readlines(), open(currentfile).readlines())
91-
diffstring = "".join(diff)
92-
logger.info(diffstring)
93-
94-
logger.info("Write diff to file")
95-
sourceFile = open("operator_diffs_hub.log", "w")
96-
print(diffstring, file=sourceFile)
97-
sourceFile.close()
98-
else:
99-
logger.info("Skipping operator diff - previous file not found")
100-
101-
if missing_subs or unhealthy_subs or missing_installplans or upgrades_pending:
102-
err_msg = "Subscription status check failed"
22+
err_msg = subscription.subscription_status(
23+
openshift_dyn_client, expected_subs, diff=True
24+
)
25+
if err_msg:
10326
logger.error(f"FAIL: {err_msg}")
10427
assert False, err_msg
10528
else:
106-
# Only push the new operarator list if the test passed
107-
if checkpath is True:
108-
os.remove(previouspath)
109-
os.rename(currentfile, previouspath)
110-
111-
cwd = os.getcwd() + "/operator-versions"
112-
logger.info(f"CWD: {cwd}")
113-
114-
logger.info("Push new operator list")
115-
subprocess.run(["git", "add", previousfile], cwd=cwd)
116-
subprocess.run(
117-
["git", "commit", "-m", "Update operator versions list"],
118-
cwd=cwd,
119-
)
120-
subprocess.run(["git", "push"], cwd=cwd)
121-
12229
logger.info("PASS: Subscription status check passed")
123-
124-
125-
def opdiff(*args):
126-
return filter(lambda x: not x.startswith(" "), difflib.ndiff(*args))

tests/interop/test_validate_hub_site_components.py

+9-122
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,15 @@
22
import os
33

44
import pytest
5-
from ocp_resources.route import Route
65
from ocp_resources.storage_class import StorageClass
7-
from openshift.dynamic.exceptions import NotFoundError
8-
from validatedpatterns_tests.interop import components
9-
from validatedpatterns_tests.interop.crd import ArgoCD
10-
from validatedpatterns_tests.interop.edge_util import (
11-
get_long_live_bearer_token,
12-
get_site_response,
13-
)
6+
from validatedpatterns_tests.interop import application, components
147

158
from . import __loggername__
169

1710
logger = logging.getLogger(__loggername__)
1811

1912
oc = os.environ["HOME"] + "/oc_client/oc"
2013

21-
"""
22-
Validate following manuela components pods and endpoints
23-
on hub site (central server):
24-
25-
26-
1) argocd
27-
2) openshift-operators
28-
3) pods
29-
4) applications health (Applications deployed through argocd)
30-
"""
31-
3214

3315
@pytest.mark.test_validate_hub_site_components
3416
def test_validate_hub_site_components(openshift_dyn_client):
@@ -47,29 +29,8 @@ def test_validate_hub_site_components(openshift_dyn_client):
4729
@pytest.mark.validate_hub_site_reachable
4830
def test_validate_hub_site_reachable(kube_config, openshift_dyn_client):
4931
logger.info("Check if hub site API end point is reachable")
50-
hub_api_url = kube_config.host
51-
if not hub_api_url:
52-
err_msg = "Hub site url is missing in kubeconfig file"
53-
logger.error(f"FAIL: {err_msg}")
54-
assert False, err_msg
55-
else:
56-
logger.info(f"HUB api url : {hub_api_url}")
57-
58-
bearer_token = get_long_live_bearer_token(
59-
dyn_client=openshift_dyn_client,
60-
namespace="openshift-gitops",
61-
sub_string="argocd-dex-server-token",
62-
)
63-
64-
if not bearer_token:
65-
assert False, "Bearer token is missing for argocd-dex-server"
66-
67-
hub_api_response = get_site_response(
68-
site_url=hub_api_url, bearer_token=bearer_token
69-
)
70-
71-
if hub_api_response.status_code != 200:
72-
err_msg = "Hub site is not reachable. Please check the deployment."
32+
err_msg = components.validate_site_reachable(kube_config, openshift_dyn_client)
33+
if err_msg:
7334
logger.error(f"FAIL: {err_msg}")
7435
assert False, err_msg
7536
else:
@@ -79,11 +40,6 @@ def test_validate_hub_site_reachable(kube_config, openshift_dyn_client):
7940
@pytest.mark.check_pod_status_hub
8041
def test_check_pod_status(openshift_dyn_client):
8142
logger.info("Checking pod status")
82-
83-
err_msg = []
84-
failed_pods = []
85-
missing_pods = []
86-
missing_projects = []
8743
projects = [
8844
"openshift-operators",
8945
"ansible-automation-platform",
@@ -92,27 +48,7 @@ def test_check_pod_status(openshift_dyn_client):
9248
"edge-gitops-vms",
9349
"vault",
9450
]
95-
96-
missing_projects = components.check_project_absense(openshift_dyn_client, projects)
97-
missing_pods = []
98-
failed_pods = []
99-
100-
for project in projects:
101-
logger.info(f"Checking pods in namespace '{project}'")
102-
missing_pods += components.check_pod_absence(openshift_dyn_client, project)
103-
failed_pods += components.check_pod_status(openshift_dyn_client, projects)
104-
105-
if missing_projects:
106-
err_msg.append(f"The following namespaces are missing: {missing_projects}")
107-
108-
if missing_pods:
109-
err_msg.append(
110-
f"The following namespaces have no pods deployed: {missing_pods}"
111-
)
112-
113-
if failed_pods:
114-
err_msg.append(f"The following pods are failed: {failed_pods}")
115-
51+
err_msg = components.check_pod_status(openshift_dyn_client, projects)
11652
if err_msg:
11753
logger.error(f"FAIL: {err_msg}")
11854
assert False, err_msg
@@ -122,43 +58,9 @@ def test_check_pod_status(openshift_dyn_client):
12258

12359
@pytest.mark.validate_argocd_reachable_hub_site
12460
def test_validate_argocd_reachable_hub_site(openshift_dyn_client):
125-
namespace = "openshift-gitops"
12661
logger.info("Check if argocd route/url on hub site is reachable")
127-
try:
128-
for route in Route.get(
129-
dyn_client=openshift_dyn_client,
130-
namespace=namespace,
131-
name="openshift-gitops-server",
132-
):
133-
argocd_route_url = route.instance.spec.host
134-
except NotFoundError:
135-
err_msg = "Argocd url/route is missing in open-cluster-management namespace"
136-
logger.error(f"FAIL: {err_msg}")
137-
assert False, err_msg
138-
139-
final_argocd_url = f"{'http://'}{argocd_route_url}"
140-
logger.info(f"ACM route/url : {final_argocd_url}")
141-
142-
bearer_token = get_long_live_bearer_token(
143-
dyn_client=openshift_dyn_client,
144-
namespace=namespace,
145-
sub_string="argocd-dex-server-token",
146-
)
147-
if not bearer_token:
148-
err_msg = "Bearer token is missing for argocd-dex-server"
149-
logger.error(f"FAIL: {err_msg}")
150-
assert False, err_msg
151-
else:
152-
logger.debug(f"Argocd bearer token : {bearer_token}")
153-
154-
argocd_route_response = get_site_response(
155-
site_url=final_argocd_url, bearer_token=bearer_token
156-
)
157-
158-
logger.info(f"Argocd route response : {argocd_route_response}")
159-
160-
if argocd_route_response.status_code != 200:
161-
err_msg = "Argocd is not reachable. Please check the deployment"
62+
err_msg = components.validate_argocd_reachable(openshift_dyn_client)
63+
if err_msg:
16264
logger.error(f"FAIL: {err_msg}")
16365
assert False, err_msg
16466
else:
@@ -167,26 +69,11 @@ def test_validate_argocd_reachable_hub_site(openshift_dyn_client):
16769

16870
@pytest.mark.validate_argocd_applications_health_hub_site
16971
def test_validate_argocd_applications_health_hub_site(openshift_dyn_client):
170-
unhealthy_apps = []
17172
logger.info("Get all applications deployed by argocd on hub site")
17273
projects = ["openshift-gitops", "ansible-edge-gitops-hub"]
173-
for project in projects:
174-
for app in ArgoCD.get(dyn_client=openshift_dyn_client, namespace=project):
175-
app_name = app.instance.metadata.name
176-
app_health = app.instance.status.health.status
177-
app_sync = app.instance.status.sync.status
178-
179-
logger.info(f"Status for {app_name} : {app_health} : {app_sync}")
180-
181-
if "Healthy" != app_health or "Synced" != app_sync:
182-
logger.info(f"Dumping failed resources for app: {app_name}")
183-
unhealthy_apps.append(app_name)
184-
for res in app.instance.status.resources:
185-
if (
186-
res.health and res.health.status != "Healthy"
187-
) or res.status != "Synced":
188-
logger.info(f"\n{res}")
189-
74+
unhealthy_apps = application.get_argocd_application_status(
75+
openshift_dyn_client, projects
76+
)
19077
if unhealthy_apps:
19178
err_msg = "Some or all applications deployed on hub site are unhealthy"
19279
logger.error(f"FAIL: {err_msg}:\n{unhealthy_apps}")

0 commit comments

Comments
 (0)