Roman Bubyr | 303820c | 2022-12-20 14:49:05 +0200 | [diff] [blame^] | 1 | import subprocess |
| 2 | import sys |
| 3 | import os |
| 4 | import yaml |
| 5 | |
| 6 | |
| 7 | def log_gather(resource_id, sub_resource): |
| 8 | """ Get all log lines related to req-id |
| 9 | :param resource_id: ID of Request |
| 10 | :param: name of sub_resource log file, e.g subnet.log for neutron resource |
| 11 | """ |
| 12 | try: |
| 13 | directory = os.walk(sys.argv[1]) |
| 14 | except IndexError: |
| 15 | print('Argument <folder-with-logs> is not provided') |
| 16 | raise ValueError('Argument <folder-with-logs> is not provided') |
| 17 | |
| 18 | for dirs in directory: |
| 19 | run_cmd = f"grep {resource_id} {dirs[0]}/* >> {sub_resource}" |
| 20 | subprocess.run(run_cmd, shell=True) |
| 21 | |
| 22 | |
| 23 | with open(sys.argv[2]) as f: |
| 24 | test_resources = yaml.safe_load(f) |
| 25 | |
| 26 | for test in test_resources.items(): |
| 27 | # Find all the failed tempest test from YAML file and gather logs for |
| 28 | # related resources in corresponded folders |
| 29 | if test[1]['status'] == 'failure': |
| 30 | print('Collecting logs for ' + test[0]) |
| 31 | os.makedirs(test[0], exist_ok=True) |
| 32 | for resource in test[1]['resources']: |
| 33 | os.makedirs(os.path.join(test[0], resource), exist_ok=True) |
| 34 | for sub_resource in test[1]['resources'][resource]: |
| 35 | log_gather(list(test[1]['resources'][resource][sub_resource])[0], |
| 36 | os.path.join(test[0], resource, sub_resource + '.' + 'log')) |
| 37 | |
| 38 | print('The logger is finished') |
| 39 | |