From b5045414d4d8a3e818d35b84b63c1c9dd6f144e9 Mon Sep 17 00:00:00 2001 From: Milosz Wasilewski Date: Wed, 7 Dec 2016 11:27:10 +0000 Subject: plans: added support for generatig CSV from test plans Change-Id: If3066f32d52e6c6e253832faa3911629ccaa54c7 Signed-off-by: Milosz Wasilewski --- plans/testplan2html.py | 124 +++++++++++++++++++++++++++++++++++-------------- 1 file changed, 90 insertions(+), 34 deletions(-) diff --git a/plans/testplan2html.py b/plans/testplan2html.py index ab9b0bb..bfd427f 100644 --- a/plans/testplan2html.py +++ b/plans/testplan2html.py @@ -2,6 +2,7 @@ import os import subprocess import yaml from argparse import ArgumentParser +from csv import DictWriter from jinja2 import Environment, FileSystemLoader @@ -34,7 +35,7 @@ def clone_repository(repository_url, base_path, ignore=False): if path_suffix.endswith(".git"): path_suffix = path_suffix[:-4] - path = os.path.abspath(os.path.join(base_path, path_suffix)) + path = os.path.abspath(os.path.join(base_path, path_suffix)) if os.path.exists(path) and ignore: return(repository_url, path) # git clone repository_url @@ -43,7 +44,7 @@ def clone_repository(repository_url, base_path, ignore=False): return (repository_url, path) -def test_exists(test, repositories): +def test_exists(test, repositories, args): test_file_path = os.path.join( repositories[test['repository']], test['path'] @@ -77,58 +78,113 @@ def test_exists(test, repositories): print params_string test_name = "{0}_{1}.html".format(test_yaml['metadata']['name'], params_string) test['filename'] = test_name - render(test_yaml, template="test.html", name=test_name) + test_path = os.path.join(os.path.abspath(args.output), test_name) + render(test_yaml, template="test.html", name=test_path) return not test['missing'] -def check_coverage(requirement, repositories): +def add_csv_row(requirement, test, args, manual=False): + fieldnames = [ + "req_name", + "req_owner", + "req_category", + "path", + "repository", + "revision", + "parameters", + "mandatory", + "kind", + ] + csv_file_path = os.path.join(os.path.abspath(args.output), args.csv_name) + has_header = False + if os.path.isfile(csv_file_path): + has_header = True + with open(csv_file_path, "ab+") as csv_file: + csvdict = DictWriter(csv_file, fieldnames=fieldnames) + if not has_header: + csvdict.writeheader() + csvdict.writerow( + { + "req_name": requirement.get('name'), + "req_owner": requirement.get('owner'), + "req_category": requirement.get('category'), + "path": test.get('path'), + "repository": test.get('repository'), + "revision": test.get('revision'), + "parameters": test.get('parameters'), + "mandatory": test.get('mandatory'), + "kind": "manual" if manual else "automated", + } + ) + + +def check_coverage(requirement, repositories, args): requirement['covered'] = False - if not 'tests' in requirement.keys() or requirement['tests'] is None: + if 'tests' not in requirement.keys() or requirement['tests'] is None: return if 'manual' in requirement['tests'].keys() and requirement['tests']['manual'] is not None: for test in requirement['tests']['manual']: - if test_exists(test, repositories) : + if test_exists(test, repositories, args): requirement['covered'] = True + if args.csv_name: + add_csv_row(requirement, test, args, True) if 'automated' in requirement['tests'].keys() and requirement['tests']['automated'] is not None: for test in requirement['tests']['automated']: - if test_exists(test, repositories): + if test_exists(test, repositories, args): requirement['covered'] = True + if args.csv_name: + add_csv_row(requirement, test, args) def main(): parser = ArgumentParser() parser.add_argument("-f", - "--file", - dest="testplan", - required=True, - help="Test plan file to be used") + "--file", + dest="testplan_list", + required=True, + nargs="+", + help="Test plan file to be used") parser.add_argument("-r", - "--repositories", - dest="repository_path", - default="repositories", - help="Test plan file to be used") + "--repositories", + dest="repository_path", + default="repositories", + help="Test plan file to be used") + parser.add_argument("-o", + "--output", + dest="output", + default="output", + help="Destination directory for generated files") parser.add_argument("-i", - "--ignore-clone", - dest="ignore_clone", - action="store_true", - default=False, - help="Ignore cloning repositories and use previously cloned") + "--ignore-clone", + dest="ignore_clone", + action="store_true", + default=False, + help="Ignore cloning repositories and use previously cloned") + parser.add_argument("-c", + "--csv", + dest="csv_name", + required=False, + help="Name of CSV to store overall list of requirements and test. If name is absent, the file will not be generated") args = parser.parse_args() - print args.ignore_clone - if os.path.exists(args.testplan) and os.path.isfile(args.testplan): - testplan = open(args.testplan, "r") - tp_obj = yaml.load(testplan.read()) - repo_list = repository_list(tp_obj) - repositories = {} - for repo in repo_list: - repo_url, repo_path = clone_repository(repo, args.repository_path, args.ignore_clone) - repositories.update({repo_url: repo_path}) - # ToDo: check test plan structure - for requirement in tp_obj['requirements']: - check_coverage(requirement, repositories) - render(tp_obj) - testplan.close() + if not os.path.exists(os.path.abspath(args.output)): + os.makedirs(os.path.abspath(args.output), 0755) + for testplan in args.testplan_list: + if os.path.exists(testplan) and os.path.isfile(testplan): + testplan_file = open(testplan, "r") + tp_obj = yaml.load(testplan_file.read()) + repo_list = repository_list(tp_obj) + repositories = {} + for repo in repo_list: + repo_url, repo_path = clone_repository(repo, args.repository_path, args.ignore_clone) + repositories.update({repo_url: repo_path}) + # ToDo: check test plan structure + for requirement in tp_obj['requirements']: + check_coverage(requirement, repositories, args) + tp_name = tp_obj['metadata']['name'] + ".html" + tp_file_name = os.path.join(os.path.abspath(args.output), tp_name) + render(tp_obj, name=tp_file_name) + testplan_file.close() # go through requiremets and for each test: # - if file exists render test as separate html file # - if file is missing, indicate missing test (red) -- cgit v1.2.3