Roman Bubyr | 303820c | 2022-12-20 14:49:05 +0200 | [diff] [blame] | 1 | import subprocess |
| 2 | import sys |
| 3 | import os |
| 4 | import yaml |
Roman Bubyr | 2a5be09 | 2023-03-17 11:34:54 +0200 | [diff] [blame^] | 5 | from os import path |
| 6 | |
| 7 | try: |
| 8 | param_is_yaml = path.isfile(sys.argv[2]) |
| 9 | except IndexError: |
| 10 | print('Second argument <tempest-report-yaml>/<resource-ID> is not provided') |
| 11 | raise ValueError('ASecond argument <tempest-report-yaml>/<resource-ID> is not provided') |
Roman Bubyr | 303820c | 2022-12-20 14:49:05 +0200 | [diff] [blame] | 12 | |
| 13 | |
Roman Bubyr | 2a5be09 | 2023-03-17 11:34:54 +0200 | [diff] [blame^] | 14 | def log_gather(resource_id, sub_resource, log_level=None): |
| 15 | """ Get all log lines related to resource-id with error |
| 16 | :param resource_id: ID resource, e.g. request-id, server-id |
| 17 | :param sub_resource: name of sub_resource log file, e.g subnet.log for neutron resource |
| 18 | :param log_level: substring for resource_id log: e.g. get only ERROR log level, optional |
Roman Bubyr | 303820c | 2022-12-20 14:49:05 +0200 | [diff] [blame] | 19 | """ |
| 20 | try: |
| 21 | directory = os.walk(sys.argv[1]) |
| 22 | except IndexError: |
| 23 | print('Argument <folder-with-logs> is not provided') |
| 24 | raise ValueError('Argument <folder-with-logs> is not provided') |
| 25 | |
Roman Bubyr | 2a5be09 | 2023-03-17 11:34:54 +0200 | [diff] [blame^] | 26 | if param_is_yaml: |
| 27 | for dirs in directory: |
| 28 | run_cmd = f"grep -a {resource_id} {dirs[0]}/* >> {sub_resource}" |
| 29 | subprocess.run(run_cmd, shell=True) |
| 30 | |
| 31 | else: |
| 32 | for dirs in directory: |
| 33 | if log_level: |
| 34 | run_cmd = f"grep -lE '{resource_id}.*{log_level}|{log_level}.*{resource_id}' {dirs[0]}/* >> 'tmp.log'" |
| 35 | else: |
| 36 | run_cmd = f"grep -l {resource_id} {dirs[0]}/* >> 'tmp.log'" |
| 37 | subprocess.run(run_cmd, shell=True) |
| 38 | |
| 39 | with open('tmp.log') as f: |
| 40 | files = f.readlines() |
| 41 | |
| 42 | for file in files: |
| 43 | subd = file.split("/") |
| 44 | log_dir = subd[-4] + "." + subd[-3] + "." + subd[-2] |
| 45 | log_name = subd[-1].replace('\n', '') |
| 46 | os.makedirs(os.path.join(sys.argv[2], log_dir), exist_ok=True) |
| 47 | path = os.path.join(sys.argv[2], log_dir, log_name) |
| 48 | if log_level: |
| 49 | run_cmd = f"grep -aE '{resource_id}.*{log_level}|{log_level}.*{resource_id}' {file} >> {path}" |
| 50 | else: |
| 51 | run_cmd = f"grep -a {resource_id} {file} >> {path}" |
| 52 | subprocess.run(run_cmd.replace('\n', ''), shell=True) |
| 53 | |
| 54 | os.remove('tmp.log') |
Roman Bubyr | 303820c | 2022-12-20 14:49:05 +0200 | [diff] [blame] | 55 | |
| 56 | |
Roman Bubyr | 2a5be09 | 2023-03-17 11:34:54 +0200 | [diff] [blame^] | 57 | if param_is_yaml: |
| 58 | print('Find all the failed tempest tests from YAML file') |
| 59 | with open(sys.argv[2]) as f: |
| 60 | test_resources = yaml.safe_load(f) |
Roman Bubyr | 303820c | 2022-12-20 14:49:05 +0200 | [diff] [blame] | 61 | |
Roman Bubyr | 2a5be09 | 2023-03-17 11:34:54 +0200 | [diff] [blame^] | 62 | for test in test_resources.items(): |
| 63 | # Find all the failed tempest tests from YAML file and gather logs for |
| 64 | # related resources in corresponded folders |
| 65 | if test[1]['status'] == 'failure': |
| 66 | print('Collecting logs for ' + test[0]) |
| 67 | os.makedirs(test[0], exist_ok=True) |
| 68 | for resource in test[1]['resources']: |
| 69 | os.makedirs(os.path.join(test[0], resource), exist_ok=True) |
| 70 | for sub_resource in test[1]['resources'][resource]: |
| 71 | log_gather(list(test[1]['resources'][resource][sub_resource])[0], |
| 72 | os.path.join(test[0], resource, sub_resource + '.' + 'log')) |
| 73 | |
| 74 | else: |
| 75 | print('Find all the related log for one specific test or id with error') |
| 76 | os.makedirs(sys.argv[2], exist_ok=True) |
| 77 | if len(sys.argv) == 4: |
| 78 | log_gather(sys.argv[2], os.path.join(sys.argv[2], 'test' + '.' + 'log'), log_level=sys.argv[3]) |
| 79 | else: |
| 80 | log_gather(sys.argv[2], os.path.join(sys.argv[2], 'test' + '.' + 'log')) |
Roman Bubyr | 303820c | 2022-12-20 14:49:05 +0200 | [diff] [blame] | 81 | |
| 82 | print('The logger is finished') |
| 83 | |