diff options
author | chenjiankun <chenjiankun1@huawei.com> | 2017-01-12 07:50:30 +0000 |
---|---|---|
committer | chenjiankun <chenjiankun1@huawei.com> | 2017-01-20 01:58:50 +0000 |
commit | 75f55c05789beb4fbe2391d33349058bba4ea5c0 (patch) | |
tree | 7372165d15f83de442fa2a5778ee540ae2ced7c0 /yardstick | |
parent | 8f4cc883d89e997320d68c653a12d59f8fba308b (diff) |
Create API to get a list of all test cases
JIRA: YARDSTICK-456
Currently we do not have a API to get a list of all test cases;
Currently the test case info is from the comment;
So I create a API to get a list of all test cases;
And create a 'description' attribute to record info of a test case;
And use the CLI call this API;
Change-Id: Ife800600446683664097835c7b9f11899c85771d
Signed-off-by: chenjiankun <chenjiankun1@huawei.com>
Diffstat (limited to 'yardstick')
-rw-r--r-- | yardstick/benchmark/core/testcase.py | 143 | ||||
-rw-r--r-- | yardstick/cmd/commands/__init__.py | 12 | ||||
-rw-r--r-- | yardstick/cmd/commands/testcase.py | 21 | ||||
-rw-r--r-- | yardstick/common/constants.py | 2 |
4 files changed, 97 insertions, 81 deletions
diff --git a/yardstick/benchmark/core/testcase.py b/yardstick/benchmark/core/testcase.py index 92198bd91..7b23b73aa 100644 --- a/yardstick/benchmark/core/testcase.py +++ b/yardstick/benchmark/core/testcase.py @@ -10,13 +10,15 @@ """ Handler for yardstick command 'testcase' """ from __future__ import absolute_import from __future__ import print_function + import os import yaml -import sys +import logging -from yardstick.benchmark.core import print_hbar from yardstick.common.task_template import TaskTemplate -from yardstick.definitions import YARDSTICK_ROOT_PATH +from yardstick.common import constants as consts + +LOG = logging.getLogger(__name__) class Testcase(object): @@ -25,91 +27,80 @@ class Testcase(object): Set of commands to discover and display test cases. """ - def __init__(self): - self.test_case_path = YARDSTICK_ROOT_PATH + 'tests/opnfv/test_cases/' - self.testcase_list = [] - def list_all(self, args): """List existing test cases""" - try: - testcase_files = os.listdir(self.test_case_path) - except Exception as e: - print("Failed to list dir:\n%(path)s\n%(err)s\n" - % {"path": self.test_case_path, "err": e}) - raise e - testcase_files.sort() - - for testcase_file in testcase_files: - record = self._get_record(testcase_file) - self.testcase_list.append(record) - - self._format_print(self.testcase_list) - return True + testcase_files = self._get_testcase_file_list() + testcase_list = [self._get_record(f) for f in testcase_files] - def show(self, args): - """Show details of a specific test case""" - testcase_name = args.casename[0] - testcase_path = self.test_case_path + testcase_name + ".yaml" + return testcase_list + + def _get_testcase_file_list(self): try: - with open(testcase_path) as f: - try: - testcase_info = f.read() - print(testcase_info) - - except Exception as e: - print("Failed to load test cases:" - "\n%(testcase_file)s\n%(err)s\n" - % {"testcase_file": testcase_path, "err": e}) - raise e - except IOError as ioerror: - sys.exit(ioerror) - return True + testcase_files = sorted(os.listdir(consts.TESTCASE_DIR)) + except OSError: + LOG.exception('Failed to list dir:\n%s\n', consts.TESTCASE_DIR) + raise + + return testcase_files def _get_record(self, testcase_file): - try: - with open(self.test_case_path + testcase_file) as f: - try: - testcase_info = f.read() - except Exception as e: - print("Failed to load test cases:" - "\n%(testcase_file)s\n%(err)s\n" - % {"testcase_file": testcase_file, "err": e}) - raise e - description, installer, deploy_scenarios = \ - self._parse_testcase(testcase_info) - - record = {'Name': testcase_file.split(".")[0], - 'Description': description, - 'installer': installer, - 'deploy_scenarios': deploy_scenarios} - return record - except IOError as ioerror: - sys.exit(ioerror) + file_path = os.path.join(consts.TESTCASE_DIR, testcase_file) + with open(file_path) as f: + try: + testcase_info = f.read() + except IOError: + LOG.exception('Failed to load test case:\n%s\n', testcase_file) + raise + + description, installer, deploy_scenarios = self._parse_testcase( + testcase_info) + + record = { + 'Name': testcase_file.split(".")[0], + 'Description': description, + 'installer': installer, + 'deploy_scenarios': deploy_scenarios + } + + return record def _parse_testcase(self, testcase_info): - kw = {} - rendered_testcase = TaskTemplate.render(testcase_info, **kw) + rendered_testcase = TaskTemplate.render(testcase_info) testcase_cfg = yaml.load(rendered_testcase) - test_precondition = testcase_cfg.get('precondition', None) - installer_type = 'all' - deploy_scenarios = 'all' - if test_precondition is not None: - installer_type = test_precondition.get('installer_type', 'all') - deploy_scenarios = test_precondition.get('deploy_scenarios', 'all') - - description = testcase_info.split("\n")[2][1:].strip() + + test_precondition = testcase_cfg.get('precondition', {}) + installer_type = test_precondition.get('installer_type', 'all') + deploy_scenarios = test_precondition.get('deploy_scenarios', 'all') + + description = self._get_description(testcase_cfg) + return description, installer_type, deploy_scenarios - def _format_print(self, testcase_list): - """format output""" + def _get_description(self, testcase_cfg): + try: + description_list = testcase_cfg['description'].split(';') + except KeyError: + return '' + else: + try: + return description_list[1].replace(os.linesep, '').strip() + except IndexError: + return description_list[0].replace(os.linesep, '').strip() - print_hbar(88) - print("| %-21s | %-60s" % ("Testcase Name", "Description")) - print_hbar(88) - for testcase_record in testcase_list: - print("| %-16s | %-60s" % (testcase_record['Name'], - testcase_record['Description'])) - print_hbar(88) + def show(self, args): + """Show details of a specific test case""" + testcase_name = args.casename[0] + testcase_path = os.path.join(consts.TESTCASE_DIR, + testcase_name + ".yaml") + with open(testcase_path) as f: + try: + testcase_info = f.read() + except IOError: + LOG.exception('Failed to load test case:\n%s\n', testcase_path) + raise + + print(testcase_info) + return True diff --git a/yardstick/cmd/commands/__init__.py b/yardstick/cmd/commands/__init__.py index 5c5356736..9bc0e9fac 100644 --- a/yardstick/cmd/commands/__init__.py +++ b/yardstick/cmd/commands/__init__.py @@ -1,10 +1,20 @@ from __future__ import absolute_import from yardstick.benchmark.core import Param +from api import client def change_osloobj_to_paras(args): param = Param({}) - for k in param.__dict__: + for k in vars(param): if hasattr(args, k): setattr(param, k, getattr(args, k)) return param + + +class Commands(object): + def __init__(self): + self.client = client + + def _change_to_dict(self, args): + p = Param({}) + return {k: getattr(args, k) for k in vars(p) if hasattr(args, k)} diff --git a/yardstick/cmd/commands/testcase.py b/yardstick/cmd/commands/testcase.py index bd17b1aa5..a151871b3 100644 --- a/yardstick/cmd/commands/testcase.py +++ b/yardstick/cmd/commands/testcase.py @@ -9,14 +9,16 @@ """ Handler for yardstick command 'testcase' """ from __future__ import print_function - from __future__ import absolute_import + from yardstick.benchmark.core.testcase import Testcase +from yardstick.benchmark.core import print_hbar from yardstick.common.utils import cliargs from yardstick.cmd.commands import change_osloobj_to_paras +from yardstick.cmd.commands import Commands -class TestcaseCommands(object): +class TestcaseCommands(Commands): """Testcase commands. Set of commands to discover and display test cases. @@ -24,11 +26,22 @@ class TestcaseCommands(object): def do_list(self, args): """List existing test cases""" - param = change_osloobj_to_paras(args) - Testcase().list_all(param) + testcase_list = self.client.get('/yardstick/testcases')['result'] + self._format_print(testcase_list) @cliargs("casename", type=str, help="test case name", nargs=1) def do_show(self, args): """Show details of a specific test case""" param = change_osloobj_to_paras(args) Testcase().show(param) + + def _format_print(self, testcase_list): + """format output""" + + print_hbar(88) + print("| %-21s | %-60s" % ("Testcase Name", "Description")) + print_hbar(88) + for testcase_record in testcase_list: + print("| %-16s | %-60s" % (testcase_record['Name'], + testcase_record['Description'])) + print_hbar(88) diff --git a/yardstick/common/constants.py b/yardstick/common/constants.py index ffca4b3e9..bf616a1f6 100644 --- a/yardstick/common/constants.py +++ b/yardstick/common/constants.py @@ -31,6 +31,8 @@ INSTALLERS = ['apex', 'compass', 'fuel', 'joid'] YARDSTICK_ROOT_PATH = dirname(dirname(dirname(abspath(__file__)))) + sep +TESTCASE_DIR = join(YARDSTICK_ROOT_PATH, 'tests/opnfv/test_cases/') + YARDSTICK_REPOS_DIR = '/home/opnfv/repos/yardstick' YARDSTICK_CONFIG_DIR = '/etc/yardstick/' |