blob: 8e47a9aeba1d40b0f2ef0f5c367d6998299924cb [file] [log] [blame]
Roman Bubyr3c8300e2023-06-01 13:08:29 +02001#!/usr/bin/env python3
2
stavrovska28772bc2024-05-22 09:33:50 +02003import os
Roman Bubyr303820c2022-12-20 14:49:05 +02004import subprocess
5import sys
Roman Bubyr2a5be092023-03-17 11:34:54 +02006from os import path
7
stavrovska28772bc2024-05-22 09:33:50 +02008import config
9import yaml
Roman Bubyr3c8300e2023-06-01 13:08:29 +020010
11param_is_yaml = False
12
13if len(sys.argv) == 1:
14 param_is_yaml = path.isfile(config.TEMPEST_REPORT_YAML)
15 if param_is_yaml is False:
stavrovska28772bc2024-05-22 09:33:50 +020016 print("TEMPEST_REPORT_YAML config parameter is not a file")
17 raise Exception("TEMPEST_REPORT_YAML config parameter is not a file")
Roman Bubyr303820c2022-12-20 14:49:05 +020018
19
Roman Bubyr2a5be092023-03-17 11:34:54 +020020def log_gather(resource_id, sub_resource, log_level=None):
stavrovska28772bc2024-05-22 09:33:50 +020021 """Get all log lines related to resource-id
22 :param resource_id: ID resource, e.g. request-id, server-id
23 :param sub_resource: name of sub_resource log file,
24 e.g subnet.log for neutron resource
25 :param log_level: substring for resource_id log: e.g. get only
26 ERROR log level messages, optional
27 """
Roman Bubyr303820c2022-12-20 14:49:05 +020028 try:
Roman Bubyr3c8300e2023-06-01 13:08:29 +020029 directory = os.walk(config.LOG_DIR)
Roman Bubyr303820c2022-12-20 14:49:05 +020030 except IndexError:
stavrovska28772bc2024-05-22 09:33:50 +020031 print("Parameter <LOG_DIR> is not provided")
32 raise ValueError("Parameter <LOG_DIR> is not provided")
Roman Bubyr303820c2022-12-20 14:49:05 +020033
Roman Bubyr2a5be092023-03-17 11:34:54 +020034 if param_is_yaml:
35 for dirs in directory:
stavrovska28772bc2024-05-22 09:33:50 +020036 run_cmd = (
37 f"grep -a {resource_id} {dirs[0]}/* >> "
38 f"{config.RESULTS_DIR}/{sub_resource}"
39 )
Roman Bubyr2a5be092023-03-17 11:34:54 +020040 subprocess.run(run_cmd, shell=True)
41
42 else:
43 for dirs in directory:
44 if log_level:
stavrovska28772bc2024-05-22 09:33:50 +020045 run_cmd = (
46 f"grep -lE '{resource_id}.*{log_level}|{log_level}"
47 f".*{resource_id}' {dirs[0]}/* >> "
48 f"'{config.RESULTS_DIR}/tmp.log'"
49 )
Roman Bubyr2a5be092023-03-17 11:34:54 +020050 else:
stavrovska28772bc2024-05-22 09:33:50 +020051 run_cmd = (
52 f"grep -l {resource_id} {dirs[0]}/* >> "
53 f"'{config.RESULTS_DIR}/tmp.log'"
54 )
Roman Bubyr2a5be092023-03-17 11:34:54 +020055 subprocess.run(run_cmd, shell=True)
56
stavrovska28772bc2024-05-22 09:33:50 +020057 with open(f"{config.RESULTS_DIR}/tmp.log") as f:
Roman Bubyr2a5be092023-03-17 11:34:54 +020058 files = f.readlines()
59
60 for file in files:
61 subd = file.split("/")
stavrovska28772bc2024-05-22 09:33:50 +020062 log_dir = f"{subd[-4]}.{subd[-3]}.{subd[-2]}"
63 log_name = subd[-1].replace("\n", "")
64 os.makedirs(
65 os.path.join(config.RESULTS_DIR, sys.argv[1], log_dir),
66 exist_ok=True,
67 )
68 path = os.path.join(
69 config.RESULTS_DIR, sys.argv[1], log_dir, log_name
70 )
Roman Bubyr2a5be092023-03-17 11:34:54 +020071 if log_level:
stavrovska28772bc2024-05-22 09:33:50 +020072 run_cmd = (
73 f"grep -aE '{resource_id}.*{log_level}|{log_level}"
74 f".*{resource_id}' {file} >> {path}"
75 )
Roman Bubyr2a5be092023-03-17 11:34:54 +020076 else:
77 run_cmd = f"grep -a {resource_id} {file} >> {path}"
stavrovska28772bc2024-05-22 09:33:50 +020078 subprocess.run(run_cmd.replace("\n", ""), shell=True)
Roman Bubyr2a5be092023-03-17 11:34:54 +020079
stavrovska28772bc2024-05-22 09:33:50 +020080 os.remove(f"{config.RESULTS_DIR}/tmp.log")
Roman Bubyr303820c2022-12-20 14:49:05 +020081
82
Roman Bubyr2a5be092023-03-17 11:34:54 +020083if param_is_yaml:
stavrovska28772bc2024-05-22 09:33:50 +020084 print("Find all the failed tempest tests from YAML file")
Roman Bubyr3c8300e2023-06-01 13:08:29 +020085 with open(config.TEMPEST_REPORT_YAML) as f:
Roman Bubyr2a5be092023-03-17 11:34:54 +020086 test_resources = yaml.safe_load(f)
Roman Bubyr303820c2022-12-20 14:49:05 +020087
Roman Bubyr2a5be092023-03-17 11:34:54 +020088 for test in test_resources.items():
89 # Find all the failed tempest tests from YAML file and gather logs for
90 # related resources in corresponded folders
stavrovska28772bc2024-05-22 09:33:50 +020091 if test[1]["status"] == "failure":
92 print(f"Collecting logs for {test[0]}")
93 os.makedirs(
94 os.path.join(config.RESULTS_DIR, test[0]), exist_ok=True
95 )
96 for resource in test[1]["resources"]:
97 os.makedirs(
98 os.path.join(config.RESULTS_DIR, test[0], resource),
99 exist_ok=True,
100 )
101 for sub_resource in test[1]["resources"][resource]:
102 log_gather(
103 list(test[1]["resources"][resource][sub_resource])[0],
104 os.path.join(
105 test[0], resource, sub_resource + "." + "log"
106 ),
107 )
Roman Bubyr2a5be092023-03-17 11:34:54 +0200108
109else:
stavrovska28772bc2024-05-22 09:33:50 +0200110 print("Find all the related log for one specific test or id with error")
Roman Bubyr3c8300e2023-06-01 13:08:29 +0200111 os.makedirs(os.path.join(config.RESULTS_DIR, sys.argv[1]), exist_ok=True)
112 if len(sys.argv) == 3:
stavrovska28772bc2024-05-22 09:33:50 +0200113 log_gather(
114 sys.argv[1],
115 os.path.join(sys.argv[1], "test" + "." + "log"),
116 log_level=sys.argv[2],
117 )
Roman Bubyr2a5be092023-03-17 11:34:54 +0200118 else:
stavrovska28772bc2024-05-22 09:33:50 +0200119 log_gather(
120 sys.argv[1], os.path.join(sys.argv[1], "test" + "." + "log")
121 )
Roman Bubyr303820c2022-12-20 14:49:05 +0200122
stavrovska28772bc2024-05-22 09:33:50 +0200123print("The logger is finished")