Roman Bubyr | 3c8300e | 2023-06-01 13:08:29 +0200 | [diff] [blame^] | 1 | #!/usr/bin/env python3 |
| 2 | |
Roman Bubyr | 303820c | 2022-12-20 14:49:05 +0200 | [diff] [blame] | 3 | import subprocess |
| 4 | import sys |
| 5 | import os |
| 6 | import yaml |
Roman Bubyr | 2a5be09 | 2023-03-17 11:34:54 +0200 | [diff] [blame] | 7 | from os import path |
Roman Bubyr | 3c8300e | 2023-06-01 13:08:29 +0200 | [diff] [blame^] | 8 | import config |
Roman Bubyr | 2a5be09 | 2023-03-17 11:34:54 +0200 | [diff] [blame] | 9 | |
Roman Bubyr | 3c8300e | 2023-06-01 13:08:29 +0200 | [diff] [blame^] | 10 | |
| 11 | param_is_yaml = False |
| 12 | |
| 13 | if 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 Bubyr | 303820c | 2022-12-20 14:49:05 +0200 | [diff] [blame] | 18 | |
| 19 | |
Roman Bubyr | 2a5be09 | 2023-03-17 11:34:54 +0200 | [diff] [blame] | 20 | def log_gather(resource_id, sub_resource, log_level=None): |
Roman Bubyr | 3c8300e | 2023-06-01 13:08:29 +0200 | [diff] [blame^] | 21 | """ Get all log lines related to resource-id |
Roman Bubyr | 2a5be09 | 2023-03-17 11:34:54 +0200 | [diff] [blame] | 22 | :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 Bubyr | 3c8300e | 2023-06-01 13:08:29 +0200 | [diff] [blame^] | 24 | :param log_level: substring for resource_id log: e.g. get only ERROR log level messages, optional |
Roman Bubyr | 303820c | 2022-12-20 14:49:05 +0200 | [diff] [blame] | 25 | """ |
| 26 | try: |
Roman Bubyr | 3c8300e | 2023-06-01 13:08:29 +0200 | [diff] [blame^] | 27 | directory = os.walk(config.LOG_DIR) |
Roman Bubyr | 303820c | 2022-12-20 14:49:05 +0200 | [diff] [blame] | 28 | except IndexError: |
Roman Bubyr | 3c8300e | 2023-06-01 13:08:29 +0200 | [diff] [blame^] | 29 | print('Parameter <LOG_DIR> is not provided') |
| 30 | raise ValueError('Parameter <LOG_DIR> is not provided') |
Roman Bubyr | 303820c | 2022-12-20 14:49:05 +0200 | [diff] [blame] | 31 | |
Roman Bubyr | 2a5be09 | 2023-03-17 11:34:54 +0200 | [diff] [blame] | 32 | if param_is_yaml: |
| 33 | for dirs in directory: |
Roman Bubyr | 3c8300e | 2023-06-01 13:08:29 +0200 | [diff] [blame^] | 34 | run_cmd = f"grep -a {resource_id} {dirs[0]}/* >> {config.RESULTS_DIR}/{sub_resource}" |
Roman Bubyr | 2a5be09 | 2023-03-17 11:34:54 +0200 | [diff] [blame] | 35 | subprocess.run(run_cmd, shell=True) |
| 36 | |
| 37 | else: |
| 38 | for dirs in directory: |
| 39 | if log_level: |
Roman Bubyr | 3c8300e | 2023-06-01 13:08:29 +0200 | [diff] [blame^] | 40 | run_cmd = f"grep -lE '{resource_id}.*{log_level}|{log_level}.*{resource_id}' {dirs[0]}/* >> '{config.RESULTS_DIR}/tmp.log'" |
Roman Bubyr | 2a5be09 | 2023-03-17 11:34:54 +0200 | [diff] [blame] | 41 | else: |
Roman Bubyr | 3c8300e | 2023-06-01 13:08:29 +0200 | [diff] [blame^] | 42 | run_cmd = f"grep -l {resource_id} {dirs[0]}/* >> '{config.RESULTS_DIR}/tmp.log'" |
Roman Bubyr | 2a5be09 | 2023-03-17 11:34:54 +0200 | [diff] [blame] | 43 | subprocess.run(run_cmd, shell=True) |
| 44 | |
Roman Bubyr | 3c8300e | 2023-06-01 13:08:29 +0200 | [diff] [blame^] | 45 | with open(config.RESULTS_DIR + '/tmp.log') as f: |
Roman Bubyr | 2a5be09 | 2023-03-17 11:34:54 +0200 | [diff] [blame] | 46 | 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 Bubyr | 3c8300e | 2023-06-01 13:08:29 +0200 | [diff] [blame^] | 52 | 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 Bubyr | 2a5be09 | 2023-03-17 11:34:54 +0200 | [diff] [blame] | 54 | 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 Bubyr | 3c8300e | 2023-06-01 13:08:29 +0200 | [diff] [blame^] | 60 | os.remove(config.RESULTS_DIR + '/tmp.log') |
Roman Bubyr | 303820c | 2022-12-20 14:49:05 +0200 | [diff] [blame] | 61 | |
| 62 | |
Roman Bubyr | 2a5be09 | 2023-03-17 11:34:54 +0200 | [diff] [blame] | 63 | if param_is_yaml: |
| 64 | print('Find all the failed tempest tests from YAML file') |
Roman Bubyr | 3c8300e | 2023-06-01 13:08:29 +0200 | [diff] [blame^] | 65 | with open(config.TEMPEST_REPORT_YAML) as f: |
Roman Bubyr | 2a5be09 | 2023-03-17 11:34:54 +0200 | [diff] [blame] | 66 | test_resources = yaml.safe_load(f) |
Roman Bubyr | 303820c | 2022-12-20 14:49:05 +0200 | [diff] [blame] | 67 | |
Roman Bubyr | 2a5be09 | 2023-03-17 11:34:54 +0200 | [diff] [blame] | 68 | 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 Bubyr | 3c8300e | 2023-06-01 13:08:29 +0200 | [diff] [blame^] | 73 | os.makedirs(os.path.join(config.RESULTS_DIR, test[0]), exist_ok=True) |
Roman Bubyr | 2a5be09 | 2023-03-17 11:34:54 +0200 | [diff] [blame] | 74 | for resource in test[1]['resources']: |
Roman Bubyr | 3c8300e | 2023-06-01 13:08:29 +0200 | [diff] [blame^] | 75 | os.makedirs(os.path.join(config.RESULTS_DIR, test[0], resource), exist_ok=True) |
Roman Bubyr | 2a5be09 | 2023-03-17 11:34:54 +0200 | [diff] [blame] | 76 | 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 | |
| 80 | else: |
| 81 | print('Find all the related log for one specific test or id with error') |
Roman Bubyr | 3c8300e | 2023-06-01 13:08:29 +0200 | [diff] [blame^] | 82 | 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 Bubyr | 2a5be09 | 2023-03-17 11:34:54 +0200 | [diff] [blame] | 85 | else: |
Roman Bubyr | 3c8300e | 2023-06-01 13:08:29 +0200 | [diff] [blame^] | 86 | log_gather(sys.argv[1], os.path.join(sys.argv[1], 'test' + '.' + 'log')) |
Roman Bubyr | 303820c | 2022-12-20 14:49:05 +0200 | [diff] [blame] | 87 | |
| 88 | print('The logger is finished') |