monkey/envs/monkey_zoo/blackbox/log_handlers/test_logs_handler.py

52 lines
1.7 KiB
Python
Raw Normal View History

import logging
2019-09-13 21:12:58 +08:00
import os
import shutil
from envs.monkey_zoo.blackbox.log_handlers.monkey_log_parser import \
MonkeyLogParser
from envs.monkey_zoo.blackbox.log_handlers.monkey_logs_downloader import \
MonkeyLogsDownloader
2019-09-13 21:12:58 +08:00
LOG_DIR_NAME = 'logs'
2019-10-01 21:11:53 +08:00
LOGGER = logging.getLogger(__name__)
2019-09-13 21:12:58 +08:00
class TestLogsHandler(object):
2019-10-01 15:42:51 +08:00
def __init__(self, test_name, island_client, log_dir_path):
2019-09-13 21:12:58 +08:00
self.test_name = test_name
self.island_client = island_client
2019-10-01 15:42:51 +08:00
self.log_dir_path = os.path.join(log_dir_path, self.test_name)
2019-09-13 21:12:58 +08:00
def parse_test_logs(self):
log_paths = self.download_logs()
if not log_paths:
2019-10-01 21:11:53 +08:00
LOGGER.error("No logs were downloaded. Maybe no monkeys were ran "
"or early exception prevented log download?")
return
TestLogsHandler.parse_logs(log_paths)
2019-09-13 21:12:58 +08:00
def download_logs(self):
self.try_create_log_dir_for_test()
2019-10-01 15:42:51 +08:00
downloader = MonkeyLogsDownloader(self.island_client, self.log_dir_path)
downloader.download_monkey_logs()
return downloader.monkey_log_paths
2019-09-13 21:12:58 +08:00
def try_create_log_dir_for_test(self):
try:
os.mkdir(self.log_dir_path)
except Exception as e:
2019-10-01 21:11:53 +08:00
LOGGER.error("Can't create a dir for test logs: {}".format(e))
2019-09-13 21:12:58 +08:00
@staticmethod
2019-10-01 15:42:51 +08:00
def delete_log_folder_contents(log_dir_path):
shutil.rmtree(log_dir_path, ignore_errors=True)
os.mkdir(log_dir_path)
@staticmethod
def parse_logs(log_paths):
for log_path in log_paths:
LOGGER.info("Info from log at {}".format(log_path))
2019-10-01 15:42:51 +08:00
log_parser = MonkeyLogParser(log_path)
log_parser.print_errors()
log_parser.print_warnings()