From 9b23320f77639f07d2156a4add18fb2f99771520 Mon Sep 17 00:00:00 2001 From: "zhifeng.jiang" Date: Sat, 20 Aug 2016 16:29:26 +0800 Subject: Code refactoring cli.py so that it can be reused by restful server. modification: Move function in cli.py to args_handler.py Add unit test for args_handler.py Add unit test for cli.py Delete print in driver.py to pass unit test JIRA:QTIP-99 Change-Id: Ib670d7dff494f3e04cdbe1de5c247d382b1052c1 Signed-off-by: zhifeng.jiang --- func/args_handler.py | 59 ++++++++++++++++++++++++++++++++++++++ func/cli.py | 81 +++++++++------------------------------------------- func/driver.py | 1 - 3 files changed, 72 insertions(+), 69 deletions(-) create mode 100644 func/args_handler.py (limited to 'func') diff --git a/func/args_handler.py b/func/args_handler.py new file mode 100644 index 00000000..f2726eb8 --- /dev/null +++ b/func/args_handler.py @@ -0,0 +1,59 @@ +############################################################################## +# Copyright (c) 2016 ZTE Corp and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## +import os +from func.env_setup import Env_setup +from func.spawn_vm import SpawnVM +from func.driver import Driver + + +def get_files_in_test_list(suit_name): + with open('test_list/' + suit_name, 'r') as fin_put: + benchmark_list = fin_put.readlines() + return map(lambda x: x.rstrip(), benchmark_list) + + +def get_files_in_test_case(lab, suit_name): + return os.listdir('./test_cases/{0}/{1}'.format(lab, suit_name)) + + +def get_benchmark_path(lab, suit, benchmark): + return './test_cases/{0}/{1}/{2}'.format(lab, suit, benchmark) + + +def check_suit_in_test_list(suit_name): + return True if os.path.isfile('test_list/' + suit_name) else False + + +def check_lab_name(lab_name): + return True if os.path.isdir('test_cases/' + lab_name) else False + + +def _get_f_name(test_case_path): + return test_case_path.split('/')[-1] + + +def prepare_ansible_env(benchmark_test_case): + env_setup = Env_setup() + [benchmark, vm_info, benchmark_details, proxy_info] = env_setup.parse(benchmark_test_case) + SpawnVM(vm_info) if len(vm_info) else None + env_setup.call_ping_test() + env_setup.call_ssh_test() + env_setup.update_ansible() + return benchmark, benchmark_details, proxy_info, env_setup + + +def run_benchmark(benchmark, benchmark_details, proxy_info, env_setup, benchmark_test_case): + driver = Driver() + driver.drive_bench(benchmark, env_setup.roles_dict.items(), _get_f_name(benchmark_test_case), + benchmark_details, env_setup.ip_pw_dict.items(), proxy_info) + + +def prepare_and_run_benchmark(benchmark_test_case): + benchmark, benchmark_details, proxy_info, env_setup = prepare_ansible_env(benchmark_test_case) + run_benchmark(benchmark, benchmark_details, proxy_info, env_setup, benchmark_test_case) diff --git a/func/cli.py b/func/cli.py index 4613b507..01694a9b 100644 --- a/func/cli.py +++ b/func/cli.py @@ -8,49 +8,12 @@ ############################################################################## import sys -import os -from func.env_setup import Env_setup -from func.driver import Driver -from func.spawn_vm import SpawnVM +import args_handler import argparse class cli: - @staticmethod - def _getfile(file_path): - with open('test_list/' + file_path, 'r') as fin_put: - _benchmarks = fin_put.readlines() - for items in range(len(_benchmarks)): - _benchmarks[items] = _benchmarks[items].rstrip() - return _benchmarks - - @staticmethod - def _getsuite(file_path): - - return file_path - - @staticmethod - def _check_test_list(filename): - - if os.path.isfile('test_list/' + filename): - return True - else: - return False - - @staticmethod - def _check_lab_name(lab_name): - - if os.path.isdir('test_cases/' + lab_name): - return True - else: - return False - - @staticmethod - def _get_f_name(file_name): - - return file_name[0: file_name.find('.')] - @staticmethod def _parse_args(args): parser = argparse.ArgumentParser() @@ -72,40 +35,22 @@ class cli: def __init__(self, args=sys.argv[1:]): - suite = [] args = self._parse_args(args) - - if not self._check_test_list(args.file): + if not args_handler.check_suit_in_test_list(args.file): print '\n\n ERROR: Test File Does not exist in test_list/ please enter correct file \n\n' sys.exit(0) - if not self._check_lab_name(args.lab): - print '\n\n You have specified a lab that is not present in test_cases/ please enter correct \ - file. If unsure how to proceed, use -l default.\n\n' + if not args_handler.check_lab_name(args.lab): + print '\n\n You have specified a lab that is not present in test_cases/ please enter \ + correct file. If unsure how to proceed, use -l default.\n\n' sys.exit(0) + suite = args.file + benchmarks = args_handler.get_files_in_test_list(suite) + test_cases = args_handler.get_files_in_test_case(args.lab, suite) + benchmarks_list = filter(lambda x: x in test_cases, benchmarks) - benchmarks = self._getfile(args.file) - suite.append(args.file) - suite = self._getsuite(suite) - for items in range(len(benchmarks)): - if suite and benchmarks: - obj = Env_setup() - if os.path.isfile('./test_cases/' + args.lab.lower() + '/' + suite[0] + '/' + benchmarks[items]): - [benchmark, vm_info, benchmark_details, proxy_info] = \ - obj.parse('./test_cases/' + args.lab.lower() + '/' + suite[0] + '/' + benchmarks[items]) + map(lambda x: args_handler.prepare_and_run_benchmark( + args_handler.get_benchmark_path(args.lab.lower(), suite, x)), benchmarks_list) - if len(vm_info) != 0: - SpawnVM(vm_info) - obj.call_ping_test() - obj.call_ssh_test() - obj.update_ansible() - dvr = Driver() - dvr.drive_bench(benchmark, - obj.roles_dict.items(), - self._get_f_name(benchmarks[items]), - benchmark_details, - obj.ip_pw_dict.items(), - proxy_info) - else: - print (benchmarks[items], ' is not a Template in the Directory - \ - Enter a Valid file name. or use qtip.py -h for list') + print('{0} is not a Template in the Directory Enter a Valid file name.' + 'or use qtip.py -h for list'.format(filter(lambda x: x not in test_cases, benchmarks))) diff --git a/func/driver.py b/func/driver.py index 291591b1..f26f63db 100644 --- a/func/driver.py +++ b/func/driver.py @@ -16,7 +16,6 @@ class Driver: def __init__(self): logging.info("Class driver initialized\n") - logging.info(os.environ['PWD']) self.installer_username = {'fuel': 'root', 'joid': 'ubuntu', 'apex': 'heat-admin'} -- cgit 1.2.3-korg