From 95720ae350260753788dbc92b7d35482165ceb48 Mon Sep 17 00:00:00 2001 From: xudan Date: Wed, 9 Nov 2016 06:26:11 +0000 Subject: dovetail tool: reorganize dovetail_config.py and modify related files 1. move all sentencs and functions into class DovetailConfig 2. delete dict container_config for it has been replaced by dovetail_config 2. modify files that import dovetail_config, including container.py, parser.py, report.py, run.py, dovetail_logger.py and testcase.py JIRA: DOVETAIL-55 Change-Id: Ib5ca3cadd751075c879f39e8cb2eb2e0adcef6aa Signed-off-by: xudan --- dovetail/conf/dovetail_config.py | 99 +++++++++++++++++++-------------------- dovetail/container.py | 7 +-- dovetail/parser.py | 4 +- dovetail/report.py | 18 ++++--- dovetail/run.py | 24 +++++----- dovetail/testcase.py | 15 +++--- dovetail/utils/dovetail_logger.py | 6 +-- 7 files changed, 86 insertions(+), 87 deletions(-) diff --git a/dovetail/conf/dovetail_config.py b/dovetail/conf/dovetail_config.py index 03d0bb78..5ac23c43 100644 --- a/dovetail/conf/dovetail_config.py +++ b/dovetail/conf/dovetail_config.py @@ -11,55 +11,52 @@ import yaml import os import re -CERT_PATH = './cert/' -TESTCASE_PATH = './testcase/' -SCENARIO_NAMING_FMT = 'certification_%s' -curr_path = os.path.dirname(os.path.abspath(__file__)) -with open(os.path.join(curr_path, 'dovetail_config.yml')) as f: - dovetail_config = yaml.safe_load(f) - -for extra_config_file in dovetail_config['include_config']: - with open(os.path.join(curr_path, extra_config_file)) as f: - extra_config = yaml.safe_load(f) - dovetail_config.update(extra_config) - -container_config = {} - -container_config['functest'] = dovetail_config['functest'] -container_config['yardstick'] = dovetail_config['yardstick'] - - -with open(os.path.join(curr_path, dovetail_config['cli_file_name'])) as f: - cmd_yml = yaml.safe_load(f) - dovetail_config['cli'] = cmd_yml[cmd_yml.keys()[0]] - - -def cmd_name_trans(cmd_name): - key = cmd_name.upper() - if key == 'SUT_TYPE': - key = 'INSTALLER_TYPE' - if key == 'SUT_IP': - key = 'INSTALLER_IP' - return key - - -def update_envs(options): - for item in options: - if options[item] is not None: - key = cmd_name_trans(item) - os.environ[key] = options[item] - update_config_envs('functest', key) - update_config_envs('yardstick', key) - - -def update_config_envs(script_type, key): - old_value = re.findall(r'\s+%s=(.*?)(\s+|$)' % key, - dovetail_config[script_type]['envs']) - if old_value == []: - dovetail_config[script_type]['envs'] += \ - ' -e ' + key + '=' + os.environ[key] - else: - dovetail_config[script_type]['envs'] = \ - dovetail_config[script_type]['envs'].replace(old_value[0][0], - os.environ[key]) +class DovetailConfig: + + CERT_PATH = './cert/' + TESTCASE_PATH = './testcase/' + SCENARIO_NAMING_FMT = 'certification_%s' + + curr_path = os.path.dirname(os.path.abspath(__file__)) + + with open(os.path.join(curr_path, 'dovetail_config.yml')) as f: + dovetail_config = yaml.safe_load(f) + + for extra_config_file in dovetail_config['include_config']: + with open(os.path.join(curr_path, extra_config_file)) as f: + extra_config = yaml.safe_load(f) + dovetail_config.update(extra_config) + + with open(os.path.join(curr_path, dovetail_config['cli_file_name'])) as f: + cmd_yml = yaml.safe_load(f) + dovetail_config['cli'] = cmd_yml[cmd_yml.keys()[0]] + + @classmethod + def cmd_name_trans(cls, cmd_name): + key = cmd_name.upper() + if key == 'SUT_TYPE': + key = 'INSTALLER_TYPE' + if key == 'SUT_IP': + key = 'INSTALLER_IP' + return key + + @classmethod + def update_envs(cls, options): + for item in options: + if options[item] is not None: + key = cls.cmd_name_trans(item) + os.environ[key] = options[item] + cls.update_config_envs('functest', key) + cls.update_config_envs('yardstick', key) + + @classmethod + def update_config_envs(cls, script_type, key): + envs = cls.dovetail_config[script_type]['envs'] + old_value = re.findall(r'\s+%s=(.*?)(\s+|$)' % key, envs) + if old_value == []: + envs += ' -e ' + key + '=' + os.environ[key] + else: + envs = envs.replace(old_value[0][0], os.environ[key]) + cls.dovetail_config[script_type]['envs'] = envs + return envs diff --git a/dovetail/container.py b/dovetail/container.py index 57ce2144..15ccc800 100644 --- a/dovetail/container.py +++ b/dovetail/container.py @@ -10,7 +10,7 @@ import utils.dovetail_logger as dt_logger import utils.dovetail_utils as dt_utils -from conf.dovetail_config import dovetail_config +from conf.dovetail_config import DovetailConfig as dt_config logger = dt_logger.Logger('container.py').getLogger() @@ -31,12 +31,13 @@ class Container: @staticmethod def get_docker_image(type): - return '%s:%s' % (dovetail_config[type]['image_name'], - dovetail_config[type]['docker_tag']) + return '%s:%s' % (dt_config.dovetail_config[type]['image_name'], + dt_config.dovetail_config[type]['docker_tag']) @classmethod def create(cls, type): # sshkey="-v /root/.ssh/id_rsa:/root/.ssh/id_rsa " + dovetail_config = dt_config.dovetail_config docker_image = cls.get_docker_image(type) envs = dovetail_config[type]['envs'] opts = dovetail_config[type]['opts'] diff --git a/dovetail/parser.py b/dovetail/parser.py index 621d20a3..1740944a 100644 --- a/dovetail/parser.py +++ b/dovetail/parser.py @@ -12,7 +12,7 @@ import jinja2 import utils.dovetail_logger as dt_logger import utils.dovetail_utils as dt_utils -from conf.dovetail_config import dovetail_config +from conf.dovetail_config import DovetailConfig as dt_config logger = dt_logger.Logger('parser.py').getLogger() @@ -26,7 +26,7 @@ class Parser: try: template = jinja2.Template(cmd, undefined=jinja2.StrictUndefined) kwargs = {} - for arg in dovetail_config['parameters']: + for arg in dt_config.dovetail_config['parameters']: path = eval(arg['path']) logger.debug('name: %s, eval path: %s ' % (arg['name'], path)) kwargs[arg['name']] = \ diff --git a/dovetail/report.py b/dovetail/report.py index a828d4f3..7dd8ba2d 100644 --- a/dovetail/report.py +++ b/dovetail/report.py @@ -13,7 +13,7 @@ import os import utils.dovetail_logger as dt_logger -from conf.dovetail_config import dovetail_config +from conf.dovetail_config import DovetailConfig as dt_config from testcase import Testcase logger = dt_logger.Logger('report.py').getLogger() @@ -94,9 +94,9 @@ class Report: # save to disk as default @staticmethod def save(report): - report_file_name = dovetail_config['report_file'] + report_file_name = dt_config.dovetail_config['report_file'] try: - with open(os.path.join(dovetail_config['result_dir'], + with open(os.path.join(dt_config.dovetail_config['result_dir'], report_file_name), 'w') as report_file: report_file.write(report) logger.info('save report to %s' % report_file_name) @@ -144,7 +144,8 @@ class FunctestCrawler: self.type = 'functest' def crawl(self, testcase=None): - store_type = dovetail_config[self.type]['result']['store_type'] + store_type = \ + dt_config.dovetail_config[self.type]['result']['store_type'] if store_type == 'file': return self.crawl_from_file(testcase) @@ -152,6 +153,7 @@ class FunctestCrawler: return self.crawl_from_url(testcase) def crawl_from_file(self, testcase=None): + dovetail_config = dt_config.dovetail_config file_path = \ os.path.join(dovetail_config['result_dir'], dovetail_config[self.type]['result']['file_path']) @@ -186,7 +188,8 @@ class FunctestCrawler: return None def crawl_from_url(self, testcase=None): - url = dovetail_config[self.type]['result']['db_url'] % testcase + url = \ + dt_config.dovetail_config[self.type]['result']['db_url'] % testcase logger.debug("Query to rest api: %s" % url) try: data = json.load(urllib2.urlopen(url)) @@ -203,7 +206,8 @@ class YardstickCrawler: self.type = 'yardstick' def crawl(self, testcase=None): - store_type = dovetail_config[self.type]['result']['store_type'] + store_type = \ + dt_config.dovetail_config[self.type]['result']['store_type'] if store_type == 'file': return self.crawl_from_file(testcase) @@ -211,7 +215,7 @@ class YardstickCrawler: return self.crawl_from_url(testcase) def crawl_from_file(self, testcase=None): - file_path = os.path.join(dovetail_config['result_dir'], + file_path = os.path.join(dt_config.dovetail_config['result_dir'], testcase+'.out') if not os.path.exists(file_path): logger.info('result file not found: %s' % file_path) diff --git a/dovetail/run.py b/dovetail/run.py index 0ebae734..86194389 100755 --- a/dovetail/run.py +++ b/dovetail/run.py @@ -18,16 +18,14 @@ from container import Container from testcase import Testcase from testcase import Scenario from report import Report -from conf.dovetail_config import SCENARIO_NAMING_FMT -from conf.dovetail_config import dovetail_config -from conf.dovetail_config import update_envs +from conf.dovetail_config import DovetailConfig as dt_config logger = dt_logger.Logger('run.py').getLogger() def load_scenario(scenario): Scenario.load() - return Scenario.get(SCENARIO_NAMING_FMT % scenario) + return Scenario.get(dt_config.SCENARIO_NAMING_FMT % scenario) def set_container_tags(option_str): @@ -35,7 +33,7 @@ def set_container_tags(option_str): option_str = script_tag_opt.split(':') script_type = option_str[0].strip() script_tag = option_str[1].strip() - dovetail_config[script_type]['docker_tag'] = script_tag + dt_config.dovetail_config[script_type]['docker_tag'] = script_tag def load_testcase(): @@ -98,7 +96,7 @@ def filter_env_options(input_dict): envs_options = {} for key, value in input_dict.items(): key = key.upper() - if key in dovetail_config['cli']['options']['envs']: + if key in dt_config.dovetail_config['cli']['options']['envs']: envs_options[key] = value return envs_options @@ -110,11 +108,11 @@ def main(*args, **kwargs): logger.info('=======================================') validate_options(kwargs) envs_options = filter_env_options(kwargs) - update_envs(envs_options) + dt_config.update_envs(envs_options) logger.info('Your new envs for functest: %s' % - dovetail_config['functest']['envs']) + dt_config.dovetail_config['functest']['envs']) logger.info('Your new envs for yardstick: %s' % - dovetail_config['yardstick']['envs']) + dt_config.dovetail_config['yardstick']['envs']) load_testcase() scenario_yaml = load_scenario(kwargs['scenario']) if 'tag' in kwargs and kwargs['tag'] is not None: @@ -124,15 +122,15 @@ def main(*args, **kwargs): CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help']) -if dovetail_config['cli']['options'] is not None: - for key, value in dovetail_config['cli']['options'].items(): +if dt_config.dovetail_config['cli']['options'] is not None: + for key, value in dt_config.dovetail_config['cli']['options'].items(): if value is not None: for k, v in value.items(): flags = v['flags'] del v['flags'] main = click.option(*flags, **v)(main) -if dovetail_config['cli']['arguments'] is not None: - for key, value in dovetail_config['cli']['arguments'].items(): +if dt_config.dovetail_config['cli']['arguments'] is not None: + for key, value in dt_config.dovetail_config['cli']['arguments'].items(): if value is not None: for k, v in value.items(): flags = v['flags'] diff --git a/dovetail/testcase.py b/dovetail/testcase.py index b1c3b621..6f0552b6 100644 --- a/dovetail/testcase.py +++ b/dovetail/testcase.py @@ -13,9 +13,7 @@ import yaml import utils.dovetail_logger as dt_logger from parser import Parser -from conf.dovetail_config import CERT_PATH -from conf.dovetail_config import TESTCASE_PATH -from conf.dovetail_config import dovetail_config +from conf.dovetail_config import DovetailConfig as dt_config logger = dt_logger.Logger('testcase.py').getLogger() @@ -31,7 +29,8 @@ class Testcase: self.script_testcase()) def prepare_cmd(self): - for cmd in dovetail_config[self.script_type()]['testcase']['cmds']: + script_type = self.script_type() + for cmd in dt_config.dovetail_config[script_type]['testcase']['cmds']: cmd_lines = Parser.parse_cmd(cmd, self) if not cmd_lines: return False @@ -108,11 +107,11 @@ class Testcase: @staticmethod def pre_condition_cls(script_type): - return dovetail_config[script_type]['pre_condition'] + return dt_config.dovetail_config[script_type]['pre_condition'] @staticmethod def post_condition_cls(script_type): - return dovetail_config[script_type]['post_condition'] + return dt_config.dovetail_config[script_type]['post_condition'] @classmethod def update_script_testcase(cls, script_type, script_testcase): @@ -141,7 +140,7 @@ class Testcase: @classmethod def load(cls): - for root, dirs, files in os.walk(TESTCASE_PATH): + for root, dirs, files in os.walk(dt_config.TESTCASE_PATH): for testcase_file in files: with open(os.path.join(root, testcase_file)) as f: testcase_yaml = yaml.safe_load(f) @@ -171,7 +170,7 @@ class Scenario: @classmethod def load(cls): - for root, dirs, files in os.walk(CERT_PATH): + for root, dirs, files in os.walk(dt_config.CERT_PATH): for scenario_yaml in files: with open(os.path.join(root, scenario_yaml)) as f: scenario_yaml = yaml.safe_load(f) diff --git a/dovetail/utils/dovetail_logger.py b/dovetail/utils/dovetail_logger.py index 8be9c93f..6a2d38d5 100644 --- a/dovetail/utils/dovetail_logger.py +++ b/dovetail/utils/dovetail_logger.py @@ -24,12 +24,12 @@ import logging import os -from conf.dovetail_config import dovetail_config +from conf.dovetail_config import DovetailConfig as dt_config import dovetail_utils as dt_utils def clean_results_dir(): - result_path = dovetail_config['result_dir'] + result_path = dt_config.dovetail_config['result_dir'] if os.path.exists(result_path): cmd = 'sudo rm -rf %s/*' % (result_path) dt_utils.exec_cmd(cmd, exit_on_error=False) @@ -56,7 +56,7 @@ class Logger: ch.setLevel(logging.INFO) self.logger.addHandler(ch) - result_path = dovetail_config['result_dir'] + result_path = dt_config.dovetail_config['result_dir'] if not os.path.exists(result_path): os.makedirs(result_path) hdlr = logging.FileHandler(os.path.join(result_path, 'dovetail.log')) -- cgit 1.2.3-korg