1 # Copyright 2015 Intel Corporation.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
16 vSwitch Characterization Report Generation.
18 Generate reports in format defined by X.
27 from collections import OrderedDict
28 from core.results.results_constants import ResultsConstants
29 from conf import settings
30 from tools import systeminfo
32 _TEMPLATE_FILE = 'report.jinja'
33 _ROOT_DIR = os.path.normpath(os.path.dirname(os.path.realpath(__file__)))
38 Get system configuration.
40 :returns: Return a dictionary of the test environment.
41 The following is an example return value:
42 {'kernel': '3.10.0-229.4.2.el7.x86_64',
44 'cpu': ' CPU 2.30GHz',
45 'platform': '[2 sockets]',
51 'os': systeminfo.get_os(),
52 'kernel': systeminfo.get_kernel(),
53 'nic': systeminfo.get_nic(),
54 'cpu': systeminfo.get_cpu(),
55 'cpu_cores': systeminfo.get_cpu_cores(),
56 'memory' : systeminfo.get_memory(),
57 'platform': systeminfo.get_platform(),
63 def _get_results(results_file):
64 """Get results from tests.
66 Get test results from a CSV file and return it as a list
67 of dictionaries for each row of data.
69 :param results_file: Path of the CSV results file
71 :returns: List of test results
73 with open(results_file, 'r') as csvfile:
74 reader = csv.reader(csvfile, delimiter=',')
76 res_head = next(reader)
77 for res_row in reader:
78 result.append(OrderedDict(zip(list(res_head), list(res_row))))
83 def generate(testcases, input_file):
84 """Generate actual report.
86 Generate a Markdown-formatted file using results of tests and some
89 :param input_file: Path to CSV results file
91 :returns: Path to generated report
93 output_file = '.'.join([os.path.splitext(input_file)[0], 'md'])
95 template_loader = jinja2.FileSystemLoader(searchpath=_ROOT_DIR)
96 template_env = jinja2.Environment(loader=template_loader)
97 template = template_env.get_template(_TEMPLATE_FILE)
101 for result in _get_results(input_file):
103 for tc_conf in testcases:
104 if tc_conf['Name'] == result[ResultsConstants.ID]:
105 test_config = tc_conf
108 # remove id and deployment from results but store their values
109 tc_id = result[ResultsConstants.ID]
110 tc_deployment = result[ResultsConstants.DEPLOYMENT]
111 del result[ResultsConstants.ID]
112 del result[ResultsConstants.DEPLOYMENT]
114 # pass test results, env details and configuration to template
118 'deployment': tc_deployment,
128 output_text = template.render(template_vars)
129 with open(output_file, 'w') as file_:
130 file_.write(output_text)
131 logging.info('Test report written to "%s"', output_file)
134 logging.info("Report: Ignoring file (Wrongly defined columns): %s", (input_file))
139 if __name__ == '__main__':
140 settings.load_from_dir('conf')
141 OUT = generate(sys.argv[1])
142 print('Test report written to "%s"...' % OUT)