Skip to content

Commit c9477ff

Browse files
committed
Updates for code consolidation
1 parent cbd709b commit c9477ff

File tree

2 files changed

+12
-183
lines changed

2 files changed

+12
-183
lines changed
Lines changed: 4 additions & 105 deletions
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,108 +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-
60-
if os.getenv("EXTERNAL_TEST") != "true":
61-
shortversion = re.sub("(.[0-9]+$)", "", os.getenv("OPENSHIFT_VER"))
62-
currentfile = os.getcwd() + "/operators_hub_current"
63-
sourceFile = open(currentfile, "w")
64-
for line in operator_versions:
65-
logger.info(line)
66-
print(line, file=sourceFile)
67-
sourceFile.close()
68-
69-
logger.info("Clone operator-versions repo")
70-
try:
71-
operator_versions_repo = (
72-
"[email protected]:mpqe/mps/vp/operator-versions.git"
73-
)
74-
clone = subprocess.run(
75-
["git", "clone", operator_versions_repo], capture_output=True, text=True
76-
)
77-
logger.info(clone.stdout)
78-
logger.info(clone.stderr)
79-
except Exception:
80-
pass
81-
82-
previouspath = os.getcwd() + f"/operator-versions/aegitops_hub_{shortversion}"
83-
previousfile = f"aegitops_hub_{shortversion}"
84-
85-
logger.info("Ensure previous file exists")
86-
checkpath = os.path.exists(previouspath)
87-
logger.info(checkpath)
88-
89-
if checkpath is True:
90-
logger.info("Diff current operator list with previous file")
91-
diff = opdiff(open(previouspath).readlines(), open(currentfile).readlines())
92-
diffstring = "".join(diff)
93-
logger.info(diffstring)
94-
95-
logger.info("Write diff to file")
96-
sourceFile = open("operator_diffs_hub.log", "w")
97-
print(diffstring, file=sourceFile)
98-
sourceFile.close()
99-
else:
100-
logger.info("Skipping operator diff - previous file not found")
101-
102-
if missing_subs or unhealthy_subs or missing_installplans or upgrades_pending:
103-
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:
10426
logger.error(f"FAIL: {err_msg}")
10527
assert False, err_msg
10628
else:
107-
# Only push the new operarator list if the test passed
108-
# and we are not testing a pre-release operator nor
109-
# running externally
110-
if os.getenv("EXTERNAL_TEST") != "true":
111-
if checkpath is True and not os.environ["INDEX_IMAGE"]:
112-
os.remove(previouspath)
113-
os.rename(currentfile, previouspath)
114-
115-
cwd = os.getcwd() + "/operator-versions"
116-
logger.info(f"CWD: {cwd}")
117-
118-
logger.info("Push new operator list")
119-
subprocess.run(["git", "add", previousfile], cwd=cwd)
120-
subprocess.run(
121-
["git", "commit", "-m", "Update operator versions list"],
122-
cwd=cwd,
123-
)
124-
subprocess.run(["git", "push"], cwd=cwd)
125-
12629
logger.info("PASS: Subscription status check passed")
127-
128-
129-
def opdiff(*args):
130-
return filter(lambda x: not x.startswith(" "), difflib.ndiff(*args))

tests/interop/test_validate_hub_site_components.py

Lines changed: 8 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,6 @@
1111

1212
oc = os.environ["HOME"] + "/oc_client/oc"
1313

14-
"""
15-
Validate following manuela components pods and endpoints
16-
on hub site (central server):
17-
18-
19-
1) argocd
20-
2) openshift-operators
21-
3) pods
22-
4) applications health (Applications deployed through argocd)
23-
"""
24-
2514

2615
@pytest.mark.test_validate_hub_site_components
2716
def test_validate_hub_site_components(openshift_dyn_client):
@@ -40,19 +29,8 @@ def test_validate_hub_site_components(openshift_dyn_client):
4029
@pytest.mark.validate_hub_site_reachable
4130
def test_validate_hub_site_reachable(kube_config, openshift_dyn_client):
4231
logger.info("Check if hub site API end point is reachable")
43-
namespace = "openshift-gitops"
44-
sub_string = "argocd-dex-server-token"
45-
try:
46-
hub_api_url = application.get_site_api_url(kube_config)
47-
hub_api_response = application.get_site_api_response(
48-
openshift_dyn_client, hub_api_url, namespace, sub_string
49-
)
50-
except AssertionError as e:
51-
logger.error(f"FAIL: {e}")
52-
assert False, e
53-
54-
if hub_api_response.status_code != 200:
55-
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:
5634
logger.error(f"FAIL: {err_msg}")
5735
assert False, err_msg
5836
else:
@@ -62,11 +40,6 @@ def test_validate_hub_site_reachable(kube_config, openshift_dyn_client):
6240
@pytest.mark.check_pod_status_hub
6341
def test_check_pod_status(openshift_dyn_client):
6442
logger.info("Checking pod status")
65-
66-
err_msg = []
67-
failed_pods = []
68-
missing_pods = []
69-
missing_projects = []
7043
projects = [
7144
"openshift-operators",
7245
"ansible-automation-platform",
@@ -75,27 +48,7 @@ def test_check_pod_status(openshift_dyn_client):
7548
"edge-gitops-vms",
7649
"vault",
7750
]
78-
79-
missing_projects = components.check_project_absense(openshift_dyn_client, projects)
80-
missing_pods = []
81-
failed_pods = []
82-
83-
for project in projects:
84-
logger.info(f"Checking pods in namespace '{project}'")
85-
missing_pods += components.check_pod_absence(openshift_dyn_client, project)
86-
failed_pods += components.check_pod_status(openshift_dyn_client, projects)
87-
88-
if missing_projects:
89-
err_msg.append(f"The following namespaces are missing: {missing_projects}")
90-
91-
if missing_pods:
92-
err_msg.append(
93-
f"The following namespaces have no pods deployed: {missing_pods}"
94-
)
95-
96-
if failed_pods:
97-
err_msg.append(f"The following pods are failed: {failed_pods}")
98-
51+
err_msg = components.check_pod_status(openshift_dyn_client, projects)
9952
if err_msg:
10053
logger.error(f"FAIL: {err_msg}")
10154
assert False, err_msg
@@ -105,30 +58,9 @@ def test_check_pod_status(openshift_dyn_client):
10558

10659
@pytest.mark.validate_argocd_reachable_hub_site
10760
def test_validate_argocd_reachable_hub_site(openshift_dyn_client):
108-
namespace = "openshift-gitops"
109-
name = "openshift-gitops-server"
110-
sub_string = "argocd-dex-server-token"
11161
logger.info("Check if argocd route/url on hub site is reachable")
112-
try:
113-
argocd_route_url = application.get_argocd_route_url(
114-
openshift_dyn_client, namespace, name
115-
)
116-
argocd_route_response = application.get_site_api_response(
117-
openshift_dyn_client, argocd_route_url, namespace, sub_string
118-
)
119-
except StopIteration:
120-
err_msg = "Argocd url/route is missing in open-cluster-management namespace"
121-
logger.error(f"FAIL: {err_msg}")
122-
assert False, err_msg
123-
except AssertionError:
124-
err_msg = "Bearer token is missing for argocd-dex-server"
125-
logger.error(f"FAIL: {err_msg}")
126-
assert False, err_msg
127-
128-
logger.info(f"Argocd route response : {argocd_route_response}")
129-
130-
if argocd_route_response.status_code != 200:
131-
err_msg = "Argocd is not reachable. Please check the deployment"
62+
err_msg = components.validate_argocd_reachable(openshift_dyn_client)
63+
if err_msg:
13264
logger.error(f"FAIL: {err_msg}")
13365
assert False, err_msg
13466
else:
@@ -137,13 +69,11 @@ def test_validate_argocd_reachable_hub_site(openshift_dyn_client):
13769

13870
@pytest.mark.validate_argocd_applications_health_hub_site
13971
def test_validate_argocd_applications_health_hub_site(openshift_dyn_client):
140-
unhealthy_apps = []
14172
logger.info("Get all applications deployed by argocd on hub site")
14273
projects = ["openshift-gitops", "ansible-edge-gitops-hub"]
143-
for project in projects:
144-
unhealthy_apps += application.get_argocd_application_status(
145-
openshift_dyn_client, project
146-
)
74+
unhealthy_apps = application.get_argocd_application_status(
75+
openshift_dyn_client, projects
76+
)
14777
if unhealthy_apps:
14878
err_msg = "Some or all applications deployed on hub site are unhealthy"
14979
logger.error(f"FAIL: {err_msg}:\n{unhealthy_apps}")

0 commit comments

Comments
 (0)