blob: 48190c291115ad8d404b5e7044af866c15774328 [file] [log] [blame]
Roman Bubyr3c8300e2023-06-01 13:08:29 +02001#!/usr/bin/env python3
2
Roman Bubyr303820c2022-12-20 14:49:05 +02003import subprocess
4import sys
5import os
6import yaml
Roman Bubyr2a5be092023-03-17 11:34:54 +02007from os import path
Roman Bubyr3c8300e2023-06-01 13:08:29 +02008import config
Roman Bubyr2a5be092023-03-17 11:34:54 +02009
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:
16 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):
Roman Bubyr3c8300e2023-06-01 13:08:29 +020021 """ Get all log lines related to resource-id
Roman Bubyr2a5be092023-03-17 11:34:54 +020022 :param resource_id: ID resource, e.g. request-id, server-id
23 :param sub_resource: name of sub_resource log file, e.g subnet.log for neutron resource
Roman Bubyr3c8300e2023-06-01 13:08:29 +020024 :param log_level: substring for resource_id log: e.g. get only ERROR log level messages, optional
Roman Bubyr303820c2022-12-20 14:49:05 +020025 """
26 try:
Roman Bubyr3c8300e2023-06-01 13:08:29 +020027 directory = os.walk(config.LOG_DIR)
Roman Bubyr303820c2022-12-20 14:49:05 +020028 except IndexError:
Roman Bubyr3c8300e2023-06-01 13:08:29 +020029 print('Parameter <LOG_DIR> is not provided')
30 raise ValueError('Parameter <LOG_DIR> is not provided')
Roman Bubyr303820c2022-12-20 14:49:05 +020031
Roman Bubyr2a5be092023-03-17 11:34:54 +020032 if param_is_yaml:
33 for dirs in directory:
Roman Bubyr3c8300e2023-06-01 13:08:29 +020034 run_cmd = f"grep -a {resource_id} {dirs[0]}/* >> {config.RESULTS_DIR}/{sub_resource}"
Roman Bubyr2a5be092023-03-17 11:34:54 +020035 subprocess.run(run_cmd, shell=True)
36
37 else:
38 for dirs in directory:
39 if log_level:
Roman Bubyr3c8300e2023-06-01 13:08:29 +020040 run_cmd = f"grep -lE '{resource_id}.*{log_level}|{log_level}.*{resource_id}' {dirs[0]}/* >> '{config.RESULTS_DIR}/tmp.log'"
Roman Bubyr2a5be092023-03-17 11:34:54 +020041 else:
Roman Bubyr3c8300e2023-06-01 13:08:29 +020042 run_cmd = f"grep -l {resource_id} {dirs[0]}/* >> '{config.RESULTS_DIR}/tmp.log'"
Roman Bubyr2a5be092023-03-17 11:34:54 +020043 subprocess.run(run_cmd, shell=True)
44
Roman Bubyr3c8300e2023-06-01 13:08:29 +020045 with open(config.RESULTS_DIR + '/tmp.log') as f:
Roman Bubyr2a5be092023-03-17 11:34:54 +020046 files = f.readlines()
47
48 for file in files:
49 subd = file.split("/")
50 log_dir = subd[-4] + "." + subd[-3] + "." + subd[-2]
51 log_name = subd[-1].replace('\n', '')
Roman Bubyr3c8300e2023-06-01 13:08:29 +020052 os.makedirs(os.path.join(config.RESULTS_DIR, sys.argv[1], log_dir), exist_ok=True)
53 path = os.path.join(config.RESULTS_DIR, sys.argv[1], log_dir, log_name)
Roman Bubyr2a5be092023-03-17 11:34:54 +020054 if log_level:
55 run_cmd = f"grep -aE '{resource_id}.*{log_level}|{log_level}.*{resource_id}' {file} >> {path}"
56 else:
57 run_cmd = f"grep -a {resource_id} {file} >> {path}"
58 subprocess.run(run_cmd.replace('\n', ''), shell=True)
59
Roman Bubyr3c8300e2023-06-01 13:08:29 +020060 os.remove(config.RESULTS_DIR + '/tmp.log')
Roman Bubyr303820c2022-12-20 14:49:05 +020061
62
Roman Bubyr2a5be092023-03-17 11:34:54 +020063if param_is_yaml:
64 print('Find all the failed tempest tests from YAML file')
Roman Bubyr3c8300e2023-06-01 13:08:29 +020065 with open(config.TEMPEST_REPORT_YAML) as f:
Roman Bubyr2a5be092023-03-17 11:34:54 +020066 test_resources = yaml.safe_load(f)
Roman Bubyr303820c2022-12-20 14:49:05 +020067
Roman Bubyr2a5be092023-03-17 11:34:54 +020068 for test in test_resources.items():
69 # Find all the failed tempest tests from YAML file and gather logs for
70 # related resources in corresponded folders
71 if test[1]['status'] == 'failure':
72 print('Collecting logs for ' + test[0])
Roman Bubyr3c8300e2023-06-01 13:08:29 +020073 os.makedirs(os.path.join(config.RESULTS_DIR, test[0]), exist_ok=True)
Roman Bubyr2a5be092023-03-17 11:34:54 +020074 for resource in test[1]['resources']:
Roman Bubyr3c8300e2023-06-01 13:08:29 +020075 os.makedirs(os.path.join(config.RESULTS_DIR, test[0], resource), exist_ok=True)
Roman Bubyr2a5be092023-03-17 11:34:54 +020076 for sub_resource in test[1]['resources'][resource]:
77 log_gather(list(test[1]['resources'][resource][sub_resource])[0],
78 os.path.join(test[0], resource, sub_resource + '.' + 'log'))
79
80else:
81 print('Find all the related log for one specific test or id with error')
Roman Bubyr3c8300e2023-06-01 13:08:29 +020082 os.makedirs(os.path.join(config.RESULTS_DIR, sys.argv[1]), exist_ok=True)
83 if len(sys.argv) == 3:
84 log_gather(sys.argv[1], os.path.join(sys.argv[1], 'test' + '.' + 'log'), log_level=sys.argv[2])
Roman Bubyr2a5be092023-03-17 11:34:54 +020085 else:
Roman Bubyr3c8300e2023-06-01 13:08:29 +020086 log_gather(sys.argv[1], os.path.join(sys.argv[1], 'test' + '.' + 'log'))
Roman Bubyr303820c2022-12-20 14:49:05 +020087
88print('The logger is finished')