Roman Bubyr | 3c8300e | 2023-06-01 13:08:29 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
stavrovska | 28772bc | 2024-05-22 09:33:50 +0200 | [diff] [blame^] | 3 | import os |
Roman Bubyr | 303820c | 2022-12-20 14:49:05 +0200 | [diff] [blame] | 4 | import subprocess |
| 5 | import sys |
Roman Bubyr | 2a5be09 | 2023-03-17 11:34:54 +0200 | [diff] [blame] | 6 | from os import path |
| 7 | |
stavrovska | 28772bc | 2024-05-22 09:33:50 +0200 | [diff] [blame^] | 8 | import config |
| 9 | import yaml |
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: |
stavrovska | 28772bc | 2024-05-22 09:33:50 +0200 | [diff] [blame^] | 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): |
stavrovska | 28772bc | 2024-05-22 09:33:50 +0200 | [diff] [blame^] | 21 | """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 Bubyr | 303820c | 2022-12-20 14:49:05 +0200 | [diff] [blame] | 28 | try: |
Roman Bubyr | 3c8300e | 2023-06-01 13:08:29 +0200 | [diff] [blame] | 29 | directory = os.walk(config.LOG_DIR) |
Roman Bubyr | 303820c | 2022-12-20 14:49:05 +0200 | [diff] [blame] | 30 | except IndexError: |
stavrovska | 28772bc | 2024-05-22 09:33:50 +0200 | [diff] [blame^] | 31 | print("Parameter <LOG_DIR> is not provided") |
| 32 | raise ValueError("Parameter <LOG_DIR> is not provided") |
Roman Bubyr | 303820c | 2022-12-20 14:49:05 +0200 | [diff] [blame] | 33 | |
Roman Bubyr | 2a5be09 | 2023-03-17 11:34:54 +0200 | [diff] [blame] | 34 | if param_is_yaml: |
| 35 | for dirs in directory: |
stavrovska | 28772bc | 2024-05-22 09:33:50 +0200 | [diff] [blame^] | 36 | run_cmd = ( |
| 37 | f"grep -a {resource_id} {dirs[0]}/* >> " |
| 38 | f"{config.RESULTS_DIR}/{sub_resource}" |
| 39 | ) |
Roman Bubyr | 2a5be09 | 2023-03-17 11:34:54 +0200 | [diff] [blame] | 40 | subprocess.run(run_cmd, shell=True) |
| 41 | |
| 42 | else: |
| 43 | for dirs in directory: |
| 44 | if log_level: |
stavrovska | 28772bc | 2024-05-22 09:33:50 +0200 | [diff] [blame^] | 45 | 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 Bubyr | 2a5be09 | 2023-03-17 11:34:54 +0200 | [diff] [blame] | 50 | else: |
stavrovska | 28772bc | 2024-05-22 09:33:50 +0200 | [diff] [blame^] | 51 | run_cmd = ( |
| 52 | f"grep -l {resource_id} {dirs[0]}/* >> " |
| 53 | f"'{config.RESULTS_DIR}/tmp.log'" |
| 54 | ) |
Roman Bubyr | 2a5be09 | 2023-03-17 11:34:54 +0200 | [diff] [blame] | 55 | subprocess.run(run_cmd, shell=True) |
| 56 | |
stavrovska | 28772bc | 2024-05-22 09:33:50 +0200 | [diff] [blame^] | 57 | with open(f"{config.RESULTS_DIR}/tmp.log") as f: |
Roman Bubyr | 2a5be09 | 2023-03-17 11:34:54 +0200 | [diff] [blame] | 58 | files = f.readlines() |
| 59 | |
| 60 | for file in files: |
| 61 | subd = file.split("/") |
stavrovska | 28772bc | 2024-05-22 09:33:50 +0200 | [diff] [blame^] | 62 | 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 Bubyr | 2a5be09 | 2023-03-17 11:34:54 +0200 | [diff] [blame] | 71 | if log_level: |
stavrovska | 28772bc | 2024-05-22 09:33:50 +0200 | [diff] [blame^] | 72 | run_cmd = ( |
| 73 | f"grep -aE '{resource_id}.*{log_level}|{log_level}" |
| 74 | f".*{resource_id}' {file} >> {path}" |
| 75 | ) |
Roman Bubyr | 2a5be09 | 2023-03-17 11:34:54 +0200 | [diff] [blame] | 76 | else: |
| 77 | run_cmd = f"grep -a {resource_id} {file} >> {path}" |
stavrovska | 28772bc | 2024-05-22 09:33:50 +0200 | [diff] [blame^] | 78 | subprocess.run(run_cmd.replace("\n", ""), shell=True) |
Roman Bubyr | 2a5be09 | 2023-03-17 11:34:54 +0200 | [diff] [blame] | 79 | |
stavrovska | 28772bc | 2024-05-22 09:33:50 +0200 | [diff] [blame^] | 80 | os.remove(f"{config.RESULTS_DIR}/tmp.log") |
Roman Bubyr | 303820c | 2022-12-20 14:49:05 +0200 | [diff] [blame] | 81 | |
| 82 | |
Roman Bubyr | 2a5be09 | 2023-03-17 11:34:54 +0200 | [diff] [blame] | 83 | if param_is_yaml: |
stavrovska | 28772bc | 2024-05-22 09:33:50 +0200 | [diff] [blame^] | 84 | print("Find all the failed tempest tests from YAML file") |
Roman Bubyr | 3c8300e | 2023-06-01 13:08:29 +0200 | [diff] [blame] | 85 | with open(config.TEMPEST_REPORT_YAML) as f: |
Roman Bubyr | 2a5be09 | 2023-03-17 11:34:54 +0200 | [diff] [blame] | 86 | test_resources = yaml.safe_load(f) |
Roman Bubyr | 303820c | 2022-12-20 14:49:05 +0200 | [diff] [blame] | 87 | |
Roman Bubyr | 2a5be09 | 2023-03-17 11:34:54 +0200 | [diff] [blame] | 88 | 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 |
stavrovska | 28772bc | 2024-05-22 09:33:50 +0200 | [diff] [blame^] | 91 | 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 Bubyr | 2a5be09 | 2023-03-17 11:34:54 +0200 | [diff] [blame] | 108 | |
| 109 | else: |
stavrovska | 28772bc | 2024-05-22 09:33:50 +0200 | [diff] [blame^] | 110 | 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] | 111 | os.makedirs(os.path.join(config.RESULTS_DIR, sys.argv[1]), exist_ok=True) |
| 112 | if len(sys.argv) == 3: |
stavrovska | 28772bc | 2024-05-22 09:33:50 +0200 | [diff] [blame^] | 113 | log_gather( |
| 114 | sys.argv[1], |
| 115 | os.path.join(sys.argv[1], "test" + "." + "log"), |
| 116 | log_level=sys.argv[2], |
| 117 | ) |
Roman Bubyr | 2a5be09 | 2023-03-17 11:34:54 +0200 | [diff] [blame] | 118 | else: |
stavrovska | 28772bc | 2024-05-22 09:33:50 +0200 | [diff] [blame^] | 119 | log_gather( |
| 120 | sys.argv[1], os.path.join(sys.argv[1], "test" + "." + "log") |
| 121 | ) |
Roman Bubyr | 303820c | 2022-12-20 14:49:05 +0200 | [diff] [blame] | 122 | |
stavrovska | 28772bc | 2024-05-22 09:33:50 +0200 | [diff] [blame^] | 123 | print("The logger is finished") |