Fix security issues of eval-s in testapi
[releng.git] / utils / test / result_collection_api / opnfv_testapi / dashboard / yardstick2Dashboard.py
1 #!/usr/bin/python
2 #
3 ##############################################################################
4 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and other.
5 #
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Apache License, Version 2.0
8 # which accompanies this distribution, and is available at
9 # http://www.apache.org/licenses/LICENSE-2.0
10 ##############################################################################
11 #
12 # This script is used to build dashboard ready json results
13 # It may be used for all the test case of the Yardstick project
14 # a new method format_<Test_case>_for_dashboard(results)
15 # v0.1: basic example with methods for Ping, Iperf, Netperf, Pktgen,
16 #       Fio, Lmbench, Perf, Cyclictest.
17 #
18
19 def get_yardstick_cases():
20     """
21     get the list of the supported test cases
22     TODO: update the list when adding a new test case for the dashboard
23     """
24     return ["Ping", "Iperf", "Netperf", "Pktgen", "Fio", "Lmbench",
25             "Perf", "Cyclictest"]
26
27
28 def format_yardstick_for_dashboard(case, results):
29     """
30     generic method calling the method corresponding to the test case
31     check that the testcase is properly declared first
32     then build the call to the specific method
33     """
34     if check_yardstick_case_exist(case):
35         cmd = "format_" + case + "_for_dashboard"
36         res = globals()[cmd](results)
37     else:
38         res = []
39         print "Test cases not declared"
40     return res
41
42
43 def check_yardstick_case_exist(case):
44     """
45     check if the testcase exists
46     if the test case is not defined or not declared in the list
47     return False
48     """
49     yardstick_cases = get_yardstick_cases()
50
51     if (case is None or case not in yardstick_cases):
52         return False
53     else:
54         return True
55
56
57 def _get_test_status_bar(results):
58     nbTest = 0
59     nbTestOk = 0
60
61     for data in results:
62         nbTest += 1
63         records = [record for record in data['details']
64                           if "benchmark" in record
65                              and record["benchmark"]["errors"] != ""]
66         if len(records) == 0:
67             nbTestOk += 1
68     return nbTest, nbTestOk
69
70
71 def format_Ping_for_dashboard(results):
72     """
73     Post processing for the Ping test case
74     """
75     test_data = [{'description': 'Ping results for Dashboard'}]
76
77     # Graph 1: Test_Duration = f(time)
78     # ********************************
79     new_element = []
80     for data in results:
81         records = [record["benchmark"]["data"]["rtt"]
82                     for record in data['details']
83                         if "benchmark" in record]
84
85         avg_rtt = sum(records) / len(records)
86         new_element.append({'x': data['start_date'],
87                             'y': avg_rtt})
88
89     test_data.append({'name': "ping duration",
90                       'info': {'type': "graph",
91                                'xlabel': 'time',
92                                'ylabel': 'duration (s)'},
93                       'data_set': new_element})
94
95     # Graph 2: bar
96     # ************
97     nbTest, nbTestOk = _get_test_status_bar(results)
98
99     test_data.append({'name': "ping status",
100                       'info': {"type": "bar"},
101                       'data_set': [{'Nb tests': nbTest,
102                                     'Nb Success': nbTestOk}]})
103
104     return test_data
105
106
107 def format_iperf_for_dashboard(results):
108     """
109     Post processing for the Iperf test case
110     """
111     test_data = [{'description': 'Iperf results for Dashboard'}]
112     return test_data
113
114
115 def format_netperf_for_dashboard(results):
116     """
117     Post processing for the Netperf test case
118     """
119     test_data = [{'description': 'Netperf results for Dashboard'}]
120     return test_data
121
122
123 def format_pktgen_for_dashboard(results):
124     """
125     Post processing for the Pktgen test case
126     """
127     test_data = [{'description': 'Pktgen results for Dashboard'}]
128     return test_data
129
130
131 def format_fio_for_dashboard(results):
132     """
133     Post processing for the Fio test case
134     """
135     test_data = [{'description': 'Fio results for Dashboard'}]
136     return test_data
137
138
139 def format_lmbench_for_dashboard(results):
140     """
141     Post processing for the Lmbench test case
142     """
143     test_data = [{'description': 'Lmbench results for Dashboard'}]
144     return test_data
145
146
147 def format_perf_for_dashboard(results):
148     """
149     Post processing for the Perf test case
150     """
151     test_data = [{'description': 'Perf results for Dashboard'}]
152     return test_data
153
154
155 def format_cyclictest_for_dashboard(results):
156     """
157     Post processing for the Cyclictest test case
158     """
159     test_data = [{'description': 'Cyclictest results for Dashboard'}]
160     return test_data
161
162
163 ############################  For local test  ################################
164 import json
165 import os
166 import requests
167
168 def _read_sample_output(filename):
169     curr_path = os.path.dirname(os.path.abspath(__file__))
170     output = os.path.join(curr_path, filename)
171     with open(output) as f:
172         sample_output = f.read()
173
174     result = json.loads(sample_output)
175     return result
176
177 # Copy form functest/testcases/Dashboard/dashboard_utils.py
178 # and did some minor modification for local test.
179 def _get_results(db_url, test_criteria):
180
181     test_project = test_criteria["project"]
182     testcase = test_criteria["testcase"]
183
184     # Build headers
185     headers = {'Content-Type': 'application/json'}
186
187     # build the request
188     # if criteria is all => remove criteria
189     url = db_url + "/results?project=" + test_project + "&case=" + testcase
190
191     # Send Request to Test DB
192     myData = requests.get(url, headers=headers)
193
194     # Get result as a json object
195     myNewData = json.loads(myData.text)
196
197     # Get results
198     myDataResults = myNewData['test_results']
199
200     return myDataResults
201
202 def _test():
203     db_url = "http://213.77.62.197"
204     result = _get_results(db_url,
205         {"project": "yardstick", "testcase": "Ping"})
206     print format_ping_for_dashboard(result)
207
208 if __name__ == '__main__':
209     _test()