blob: 4b4e180b58ee41948918c59098c8824801992189 [file] [log] [blame]
Milosz Wasilewskif5ccdbd2016-10-25 18:49:20 +01001import os
2import subprocess
3import yaml
4from argparse import ArgumentParser
Milosz Wasilewskib5045412016-12-07 11:27:10 +00005from csv import DictWriter
Milosz Wasilewskif5ccdbd2016-10-25 18:49:20 +01006from jinja2 import Environment, FileSystemLoader
7
8
9def render(obj, template="testplan.html", name=None):
10 if name is None:
11 name = template
Milosz Wasilewskif761ab92018-02-13 11:21:18 +000012 templates_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "templates")
13 _env = Environment(loader=FileSystemLoader(templates_dir))
Milosz Wasilewskif5ccdbd2016-10-25 18:49:20 +010014 _template = _env.get_template(template)
15 _obj = _template.render(obj=obj)
16 with open("{}".format(name), "wb") as _file:
17 _file.write(_obj.encode('utf-8'))
18
19
20# get list of repositories and cache them
21def repository_list(testplan):
22 repositories = set()
Milosz Wasilewskid5e1dd92018-02-12 12:08:44 +000023 tp_version = testplan['metadata']['format']
24 if tp_version == "Linaro Test Plan v2":
25 if 'manual' in testplan['tests'].keys() and testplan['tests']['manual'] is not None:
26 for test in testplan['tests']['manual']:
27 repositories.add(test['repository'])
28
29 if 'automated' in testplan['tests'].keys() and testplan['tests']['automated'] is not None:
30 for test in testplan['tests']['automated']:
31 repositories.add(test['repository'])
32 if tp_version == "Linaro Test Plan v1":
33 for req in testplan['requirements']:
34 if 'tests' in req.keys() and req['tests'] is not None:
35 if 'manual' in req['tests'].keys() and req['tests']['manual'] is not None:
36 for test in req['tests']['manual']:
37 repositories.add(test['repository'])
38 if 'automated' in req['tests'].keys() and req['tests']['automated'] is not None:
39 for test in req['tests']['automated']:
40 repositories.add(test['repository'])
Milosz Wasilewskif5ccdbd2016-10-25 18:49:20 +010041 return repositories
42
43
44def clone_repository(repository_url, base_path, ignore=False):
45 path_suffix = repository_url.rsplit("/", 1)[1]
46 if path_suffix.endswith(".git"):
47 path_suffix = path_suffix[:-4]
48
Milosz Wasilewskib5045412016-12-07 11:27:10 +000049 path = os.path.abspath(os.path.join(base_path, path_suffix))
Milosz Wasilewskif5ccdbd2016-10-25 18:49:20 +010050 if os.path.exists(path) and ignore:
51 return(repository_url, path)
52 # git clone repository_url
53 subprocess.call(['git', 'clone', repository_url, path])
54 # return tuple (repository_url, system_path)
55 return (repository_url, path)
56
57
Milosz Wasilewskib5045412016-12-07 11:27:10 +000058def test_exists(test, repositories, args):
Milosz Wasilewskif5ccdbd2016-10-25 18:49:20 +010059 test_file_path = os.path.join(
60 repositories[test['repository']],
61 test['path']
62 )
63 current_dir = os.getcwd()
64 print current_dir
65 os.chdir(repositories[test['repository']])
66 if 'revision' in test.keys():
67 subprocess.call(['git', 'checkout', test['revision']])
68 else:
69 # if no revision is specified, use current HEAD
70 output = subprocess.check_output(['git', 'rev-parse', 'HEAD'])
71 test['revision'] = output
72
73 if not os.path.exists(test_file_path) or not os.path.isfile(test_file_path):
74 test['missing'] = True
75 os.chdir(current_dir)
76 return not test['missing']
77 test['missing'] = False
78 # open the file and render the test
79 subprocess.call(['git', 'checkout', 'master'])
80 print current_dir
81 os.chdir(current_dir)
82 print os.getcwd()
83 test_file = open(test_file_path, "r")
84 test_yaml = yaml.load(test_file.read())
85 params_string = ""
86 if 'parameters' in test.keys():
87 params_string = "_".join(["{0}-{1}".format(param_name, param_value).replace("/", "").replace(" ", "") for param_name, param_value in test['parameters'].iteritems()])
88 test_yaml['params'].update(test['parameters'])
89 print params_string
90 test_name = "{0}_{1}.html".format(test_yaml['metadata']['name'], params_string)
91 test['filename'] = test_name
Milosz Wasilewskib5045412016-12-07 11:27:10 +000092 test_path = os.path.join(os.path.abspath(args.output), test_name)
93 render(test_yaml, template="test.html", name=test_path)
Milosz Wasilewskif5ccdbd2016-10-25 18:49:20 +010094 return not test['missing']
95
96
Milosz Wasilewskib5045412016-12-07 11:27:10 +000097def add_csv_row(requirement, test, args, manual=False):
98 fieldnames = [
99 "req_name",
100 "req_owner",
101 "req_category",
102 "path",
103 "repository",
104 "revision",
105 "parameters",
106 "mandatory",
107 "kind",
108 ]
109 csv_file_path = os.path.join(os.path.abspath(args.output), args.csv_name)
110 has_header = False
111 if os.path.isfile(csv_file_path):
112 has_header = True
113 with open(csv_file_path, "ab+") as csv_file:
114 csvdict = DictWriter(csv_file, fieldnames=fieldnames)
115 if not has_header:
116 csvdict.writeheader()
117 csvdict.writerow(
118 {
119 "req_name": requirement.get('name'),
120 "req_owner": requirement.get('owner'),
121 "req_category": requirement.get('category'),
122 "path": test.get('path'),
123 "repository": test.get('repository'),
124 "revision": test.get('revision'),
125 "parameters": test.get('parameters'),
126 "mandatory": test.get('mandatory'),
127 "kind": "manual" if manual else "automated",
128 }
129 )
130
131
132def check_coverage(requirement, repositories, args):
Milosz Wasilewskif5ccdbd2016-10-25 18:49:20 +0100133 requirement['covered'] = False
Milosz Wasilewskib5045412016-12-07 11:27:10 +0000134 if 'tests' not in requirement.keys() or requirement['tests'] is None:
Milosz Wasilewskif5ccdbd2016-10-25 18:49:20 +0100135 return
136 if 'manual' in requirement['tests'].keys() and requirement['tests']['manual'] is not None:
137 for test in requirement['tests']['manual']:
Milosz Wasilewskib5045412016-12-07 11:27:10 +0000138 if test_exists(test, repositories, args):
Milosz Wasilewskif5ccdbd2016-10-25 18:49:20 +0100139 requirement['covered'] = True
Milosz Wasilewskib5045412016-12-07 11:27:10 +0000140 if args.csv_name:
141 add_csv_row(requirement, test, args, True)
Milosz Wasilewskif5ccdbd2016-10-25 18:49:20 +0100142 if 'automated' in requirement['tests'].keys() and requirement['tests']['automated'] is not None:
143 for test in requirement['tests']['automated']:
Milosz Wasilewskib5045412016-12-07 11:27:10 +0000144 if test_exists(test, repositories, args):
Milosz Wasilewskif5ccdbd2016-10-25 18:49:20 +0100145 requirement['covered'] = True
Milosz Wasilewskib5045412016-12-07 11:27:10 +0000146 if args.csv_name:
147 add_csv_row(requirement, test, args)
Milosz Wasilewskif5ccdbd2016-10-25 18:49:20 +0100148
149
150def main():
151 parser = ArgumentParser()
152 parser.add_argument("-f",
Milosz Wasilewskib5045412016-12-07 11:27:10 +0000153 "--file",
154 dest="testplan_list",
155 required=True,
156 nargs="+",
157 help="Test plan file to be used")
Milosz Wasilewskif5ccdbd2016-10-25 18:49:20 +0100158 parser.add_argument("-r",
Milosz Wasilewskib5045412016-12-07 11:27:10 +0000159 "--repositories",
160 dest="repository_path",
161 default="repositories",
162 help="Test plan file to be used")
163 parser.add_argument("-o",
164 "--output",
165 dest="output",
166 default="output",
167 help="Destination directory for generated files")
Milosz Wasilewskif5ccdbd2016-10-25 18:49:20 +0100168 parser.add_argument("-i",
Milosz Wasilewskib5045412016-12-07 11:27:10 +0000169 "--ignore-clone",
170 dest="ignore_clone",
171 action="store_true",
172 default=False,
173 help="Ignore cloning repositories and use previously cloned")
174 parser.add_argument("-c",
175 "--csv",
176 dest="csv_name",
177 required=False,
178 help="Name of CSV to store overall list of requirements and test. If name is absent, the file will not be generated")
Milosz Wasilewskif5ccdbd2016-10-25 18:49:20 +0100179
180 args = parser.parse_args()
Milosz Wasilewskib5045412016-12-07 11:27:10 +0000181 if not os.path.exists(os.path.abspath(args.output)):
182 os.makedirs(os.path.abspath(args.output), 0755)
183 for testplan in args.testplan_list:
184 if os.path.exists(testplan) and os.path.isfile(testplan):
185 testplan_file = open(testplan, "r")
186 tp_obj = yaml.load(testplan_file.read())
187 repo_list = repository_list(tp_obj)
188 repositories = {}
189 for repo in repo_list:
190 repo_url, repo_path = clone_repository(repo, args.repository_path, args.ignore_clone)
191 repositories.update({repo_url: repo_path})
192 # ToDo: check test plan structure
Milosz Wasilewskid5e1dd92018-02-12 12:08:44 +0000193
194 tp_version = tp_obj['metadata']['format']
195 if tp_version == "Linaro Test Plan v1":
196 for requirement in tp_obj['requirements']:
197 check_coverage(requirement, repositories, args)
198 if tp_version == "Linaro Test Plan v2":
199 if 'manual' in tp_obj['tests'].keys() and tp_obj['tests']['manual'] is not None:
200 for test in tp_obj['tests']['manual']:
201 test_exists(test, repositories, args)
202 if 'automated' in tp_obj['tests'].keys() and tp_obj['tests']['automated'] is not None:
203 for test in tp_obj['tests']['automated']:
204 test_exists(test, repositories, args)
Milosz Wasilewskib5045412016-12-07 11:27:10 +0000205 tp_name = tp_obj['metadata']['name'] + ".html"
206 tp_file_name = os.path.join(os.path.abspath(args.output), tp_name)
Milosz Wasilewskid5e1dd92018-02-12 12:08:44 +0000207 if tp_version == "Linaro Test Plan v1":
208 render(tp_obj, name=tp_file_name)
209 if tp_version == "Linaro Test Plan v2":
210 render(tp_obj, name=tp_file_name, template="testplan_v2.html")
Milosz Wasilewskib5045412016-12-07 11:27:10 +0000211 testplan_file.close()
Milosz Wasilewskif5ccdbd2016-10-25 18:49:20 +0100212# go through requiremets and for each test:
213# - if file exists render test as separate html file
214# - if file is missing, indicate missing test (red)
215# render test plan with links to test files
216# add option to render as single file (for pdf generation)
217
218if __name__ == "__main__":
219 main()