Skip to content

Commit 3076e0d

Browse files
Adding report generator from the must-gather logs collected
Signed-off-by: Shivam Durgbuns <sdurgbun@redhat.com>
1 parent 77cd806 commit 3076e0d

File tree

5 files changed

+111
-2
lines changed

5 files changed

+111
-2
lines changed

.secrets.baseline

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -559,15 +559,15 @@
559559
"hashed_secret": "72cb70dbbafe97e5ea13ad88acd65d08389439b0",
560560
"is_secret": false,
561561
"is_verified": false,
562-
"line_number": 331,
562+
"line_number": 330,
563563
"type": "Secret Keyword",
564564
"verified_result": null
565565
},
566566
{
567567
"hashed_secret": "de19c40310ad831a71524cd279e6f5f0577fb09a",
568568
"is_secret": false,
569569
"is_verified": false,
570-
"line_number": 377,
570+
"line_number": 376,
571571
"type": "Secret Keyword",
572572
"verified_result": null
573573
}

ocs_ci/deployment/deployment.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@
144144
is_acm_cluster,
145145
is_recovery_cluster,
146146
)
147+
from ocs_ci.ocs.must_gather.report_generator_hook import (
148+
trigger_reports_after_collect_ocs_logs,
149+
)
147150
from ocs_ci.utility.deployment import (
148151
create_external_secret,
149152
get_and_apply_idms_from_catalog,
@@ -560,6 +563,13 @@ def do_deploy_ocs(self):
560563
if config.REPORTING["collect_logs_on_success_run"]:
561564
try:
562565
collect_ocs_logs("deployment", ocp=False, status_failure=False)
566+
trigger_reports_after_collect_ocs_logs(
567+
dir_name="deployment",
568+
status_failure=False,
569+
cluster_configs=(
570+
config.clusters if config.multicluster else [config]
571+
),
572+
)
563573
except Exception as e:
564574
logger.error(
565575
f"Failed to collect OCS logs: {e}, but ignoring it as deployment is successful"
@@ -585,6 +595,13 @@ def do_deploy_ocs(self):
585595
ocp=False,
586596
timeout=defaults.MUST_GATHER_TIMEOUT,
587597
)
598+
trigger_reports_after_collect_ocs_logs(
599+
dir_name="deployment",
600+
status_failure=True,
601+
cluster_configs=(
602+
config.clusters if config.multicluster else [config]
603+
),
604+
)
588605
except Exception as e:
589606
logger.error(f"Failed to collect OCS logs: {e}")
590607
raise

ocs_ci/framework/conf/default_config.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ REPORTING:
156156
max_mg_fail_attempts: 3
157157
tarball_mg_logs: true
158158
delete_packed_mg_logs: true
159+
# If True, run scripts/python/must_gather_report_generator/main.py after OCS must-gather is collected
160+
generate_must_gather_report: false
159161

160162
# This is the default information about environment.
161163
ENV_DATA:

ocs_ci/framework/pytest_customization/ocscilib.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
from ocs_ci.ocs.cluster import check_clusters
3838
from ocs_ci.ocs.resources.ocs import get_version_info
3939
from ocs_ci.ocs import utils
40+
from ocs_ci.ocs.must_gather.report_generator_hook import (
41+
trigger_reports_after_collect_ocs_logs,
42+
)
4043
from ocs_ci.utility.utils import (
4144
dump_config_to_file,
4245
get_ceph_version,
@@ -999,6 +1002,13 @@ def pytest_runtest_makereport(item, call):
9991002
timeout=timeout,
10001003
since_time=since_time_str,
10011004
)
1005+
# Must-gather (OCS) is complete when collect_ocs_logs returns
1006+
if ocs_logs_collection:
1007+
trigger_reports_after_collect_ocs_logs(
1008+
dir_name=test_case_name,
1009+
status_failure=True,
1010+
cluster_configs=ocsci_config.clusters,
1011+
)
10021012
except Exception:
10031013
log.exception("Failed to collect OCS logs")
10041014

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import os
2+
import re
3+
import subprocess
4+
from pathlib import Path
5+
6+
from ocs_ci.framework import config
7+
8+
9+
def _repo_root():
10+
# ocs_ci/ocs/must_gather/report_generator_hook.py -> parents[4] is repo root
11+
return Path(__file__).resolve().parents[4]
12+
13+
14+
def run_report_generator(mg_dir_path: str, report_dir: str, *, prefix: str) -> None:
15+
"""
16+
Run scripts/python/must_gather_report_generator/main.py after must-gather is complete.
17+
The report generator script itself is responsible for handling tarballs,
18+
symlinks, etc. This hook only triggers the script after log collection.
19+
"""
20+
if not config.REPORTING.get("generate_must_gather_report", False):
21+
return
22+
23+
mg_dir_path = os.path.abspath(mg_dir_path)
24+
tarball_path = f"{mg_dir_path}.tar.gz"
25+
if not (os.path.isdir(mg_dir_path) or os.path.isfile(tarball_path)):
26+
return
27+
28+
os.makedirs(report_dir, exist_ok=True)
29+
30+
script_path = _repo_root() / "scripts/python/must_gather_report_generator/main.py"
31+
if not script_path.is_file():
32+
return
33+
34+
safe_prefix = re.sub(r"[^A-Za-z0-9_.-]+", "_", prefix)
35+
text_out = os.path.join(report_dir, f"{safe_prefix}_mg_analysis.txt")
36+
xml_out = os.path.join(report_dir, f"{safe_prefix}_mg_analysis.xml")
37+
38+
cmd = [
39+
"python",
40+
str(script_path),
41+
mg_dir_path,
42+
"--output-file",
43+
text_out,
44+
"--xml-output",
45+
xml_out,
46+
]
47+
subprocess.run(cmd, check=False)
48+
49+
50+
def trigger_reports_after_collect_ocs_logs(
51+
*,
52+
dir_name: str,
53+
status_failure: bool,
54+
cluster_configs,
55+
) -> None:
56+
"""
57+
Trigger report generation for the OCS must-gather directory per cluster.
58+
Should be called only AFTER collect_ocs_logs() returns.
59+
"""
60+
if not config.REPORTING.get("generate_must_gather_report", False):
61+
return
62+
63+
for cluster in cluster_configs:
64+
if status_failure:
65+
base = os.path.join(
66+
os.path.expanduser(cluster.RUN["log_dir"]),
67+
f"failed_testcase_ocs_logs_{cluster.RUN['run_id']}",
68+
f"{dir_name}_ocs_logs",
69+
f"{cluster.ENV_DATA['cluster_name']}",
70+
)
71+
else:
72+
base = os.path.join(
73+
os.path.expanduser(cluster.RUN["log_dir"]),
74+
f"{dir_name}_{cluster.RUN['run_id']}",
75+
f"{cluster.ENV_DATA['cluster_name']}",
76+
)
77+
78+
mg_dir = os.path.join(base, "ocs_must_gather")
79+
report_dir = os.path.join(base, "must_gather_report")
80+
run_report_generator(mg_dir, report_dir, prefix=dir_name)

0 commit comments

Comments
 (0)