From b3dcaa337fa91b3123fa08e8b8dd2e81184a91dd Mon Sep 17 00:00:00 2001 From: dongwenjuan Date: Mon, 9 Oct 2017 15:25:33 +0800 Subject: fix to get logfile Change-Id: I44cc6c5108eb48191ca36cbcb44f787ea542f3ce Signed-off-by: dongwenjuan --- doctor_tests/common/utils.py | 5 +++++ doctor_tests/installer/apex.py | 12 +++++++----- doctor_tests/logger.py | 11 ++++++++--- doctor_tests/main.py | 18 ++++++++++-------- doctor_tests/scenario/network_failure.py | 14 ++++++++++---- 5 files changed, 40 insertions(+), 20 deletions(-) diff --git a/doctor_tests/common/utils.py b/doctor_tests/common/utils.py index 2e823acb..d2962a8a 100644 --- a/doctor_tests/common/utils.py +++ b/doctor_tests/common/utils.py @@ -47,6 +47,11 @@ def match_rep_in_file(regex, full_path): return None +def get_doctor_test_root_dir(): + current_dir = os.path.split(os.path.realpath(__file__))[0] + return os.path.dirname(current_dir) + + class SSHClient(object): def __init__(self, ip, username, password=None, pkey=None, key_filename=None, log=None, look_for_keys=False, diff --git a/doctor_tests/installer/apex.py b/doctor_tests/installer/apex.py index 2a1ce94b..b64a3662 100644 --- a/doctor_tests/installer/apex.py +++ b/doctor_tests/installer/apex.py @@ -13,6 +13,7 @@ import pwd import stat import subprocess +from doctor_tests.common.utils import get_doctor_test_root_dir from doctor_tests.common.utils import SSHClient from doctor_tests.installer.base import BaseInstaller @@ -31,6 +32,7 @@ class ApexInstaller(BaseInstaller): self.controllers = list() self.controller_clients = list() self.servers = list() + self.test_dir = get_doctor_test_root_dir() def setup(self): self.log.info('Setup Apex installer start......') @@ -52,14 +54,14 @@ class ApexInstaller(BaseInstaller): self.log.info('Already have SSH keys from Apex installer......') return self.key_file - self.client.scp('/home/stack/.ssh/id_rsa', './instack_key', method='get') + ssh_key = '{0}/{1}'.format(self.test_dir, 'instack_key') + self.client.scp('/home/stack/.ssh/id_rsa', ssh_key, method='get') user = getpass.getuser() uid = pwd.getpwnam(user).pw_uid gid = grp.getgrnam(user).gr_gid - os.chown('./instack_key', uid, gid) - os.chmod('./instack_key', stat.S_IREAD) - current_dir = os.curdir - self.key_file = '{0}/{1}'.format(current_dir, 'instack_key') + os.chown(ssh_key, uid, gid) + os.chmod(ssh_key, stat.S_IREAD) + self.key_file = ssh_key return self.key_file def get_controller_ips(self): diff --git a/doctor_tests/logger.py b/doctor_tests/logger.py index 2da2c0de..e6689467 100644 --- a/doctor_tests/logger.py +++ b/doctor_tests/logger.py @@ -15,6 +15,8 @@ import logging import os +from doctor_tests.common.utils import get_doctor_test_root_dir + class Logger(object): def __init__(self, logger_name): @@ -36,12 +38,15 @@ class Logger(object): ch.setLevel(logging.INFO) self.logger.addHandler(ch) - filename = '%s.log' % logger_name - file_handler = logging.FileHandler(filename, mode='w') + test_dir = get_doctor_test_root_dir() + self.filename = '{0}/{1}.log'.format(test_dir, logger_name) + file_handler = logging.FileHandler(self.filename, mode='w') file_handler.setFormatter(formatter) file_handler.setLevel(logging.DEBUG) - self.filename = file_handler.baseFilename self.logger.addHandler(file_handler) def getLogger(self): return self.logger + + def getLogFilename(self): + return self.filename diff --git a/doctor_tests/main.py b/doctor_tests/main.py index 111c93d6..b9a34430 100644 --- a/doctor_tests/main.py +++ b/doctor_tests/main.py @@ -33,7 +33,9 @@ from doctor_tests.scenario.network_failure import NetworkFault from doctor_tests.user import User -LOG = doctor_log.Logger('doctor').getLogger() +Logger = doctor_log.Logger('doctor') +LOG = Logger.getLogger() +LogFile = Logger.getLogFilename() class DoctorTest(object): @@ -106,7 +108,7 @@ class DoctorTest(object): # NOTE (umar) copy remote monitor.log file when monitor=collectd self.check_host_status(self.down_host.name, 'down') - notification_time = calculate_notification_time(LOG.filename) + notification_time = calculate_notification_time(LogFile) if notification_time < 1 and notification_time > 0: LOG.info('doctor test successfully, notification_time=%s' % notification_time) else: @@ -156,22 +158,22 @@ class DoctorTest(object): self.fault.get_disable_network_log() def run_profiler(self): - test_dir = os.path.split(os.path.realpath(__file__))[0] + net_down_log_file = self.fault.get_disable_network_log() reg = '(?<=doctor set link down at )\d+.\d+' - linkdown = float(match_rep_in_file(reg, LOG.filename).group(0)) + linkdown = float(match_rep_in_file(reg, net_down_log_file).group(0)) reg = '(.* doctor mark vm.* error at )(\d+.\d+)' - vmdown = float(match_rep_in_file(reg, LOG.filename).group(2)) + vmdown = float(match_rep_in_file(reg, LogFile).group(2)) reg = '(.* doctor mark host.* down at )(\d+.\d+)' - hostdown = float(match_rep_in_file(reg, LOG.filename).group(2)) + hostdown = float(match_rep_in_file(reg, LogFile).group(2)) reg = '(?<=doctor monitor detected at )\d+.\d+' - detected = float(match_rep_in_file(reg, LOG.filename).group(0)) + detected = float(match_rep_in_file(reg, LogFile).group(0)) reg = '(?<=doctor consumer notified at )\d+.\d+' - notified = float(match_rep_in_file(reg, LOG.filename).group(0)) + notified = float(match_rep_in_file(reg, LogFile).group(0)) # TODO(yujunz) check the actual delay to verify time sync status # expected ~1s delay from $trigger to $linkdown diff --git a/doctor_tests/scenario/network_failure.py b/doctor_tests/scenario/network_failure.py index b94a622d..b2309435 100644 --- a/doctor_tests/scenario/network_failure.py +++ b/doctor_tests/scenario/network_failure.py @@ -8,6 +8,7 @@ ############################################################################## from doctor_tests.identity_auth import get_session from doctor_tests.os_clients import nova_client +from doctor_tests.common.utils import get_doctor_test_root_dir from doctor_tests.common.utils import SSHClient LINK_DOWN_SCRIPT = """ @@ -29,8 +30,10 @@ class NetworkFault(object): self.log = log self.installer = installer self.nova = nova_client(self.conf.nova_version, get_session()) + self.test_dir = get_doctor_test_root_dir() self.host = None self.GetLog = False + self.disable_network_log = None def start(self, host): self.log.info('fault inject start......') @@ -45,20 +48,23 @@ class NetworkFault(object): def get_disable_network_log(self): if self.GetLog: self.log.info('Already get the disable_netork.log from down_host......') - return + return self.disable_network_log if self.host is not None: client = SSHClient(self.host.ip, self.installer.node_user_name, key_filename=self.installer.get_ssh_key_from_installer(), look_for_keys=True, log=self.log) - client.scp('disable_network.log', './disable_network.log', method='get') + + self.disable_network_log = '{0}/{1}'.format(self.test_dir, 'disable_network.log') + client.scp('disable_network.log', self.disable_network_log, method='get') self.log.info('Get the disable_netork.log from down_host(host_name:%s, host_ip:%s)' % (self.host.name, self.host.ip)) self.GetLog = True + return self.disable_network_log def _set_link_down(self, compute_ip): - file_name = './disable_network.sh' + file_name = '{0}/{1}'.format(self.test_dir, 'disable_network.sh') with open(file_name, 'w') as file: file.write(LINK_DOWN_SCRIPT.format(compute_ip=compute_ip)) client = SSHClient(compute_ip, @@ -66,6 +72,6 @@ class NetworkFault(object): key_filename=self.installer.get_ssh_key_from_installer(), look_for_keys=True, log=self.log) - client.scp('./disable_network.sh', 'disable_network.sh') + client.scp(file_name, 'disable_network.sh') command = 'bash disable_network.sh > disable_network.log 2>&1 &' client.ssh(command) -- cgit 1.2.3-korg