Fix security issues of eval-s in testapi
[releng.git] / utils / test / result_collection_api / opnfv_testapi / dashboard / promise2Dashboard.py
1  #!/usr/bin/python
2 #
3 # Copyright (c) 2015 Orange
4 # morgan.richomme@orange.com
5 #
6 # 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 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # This script is used to build dashboard ready json results
13 # It may be used for all the test case of the Promise project
14 # a new method format_<Test_case>_for_dashboard(results)
15 # v0.1: basic example with methods for odl, Tempest, Rally and vPing
16 #
17
18 def get_promise_cases():
19     """
20     get the list of the supported test cases
21     TODO: update the list when adding a new test case for the dashboard
22     """
23     return ["promise"]
24
25
26 def format_promise_for_dashboard(case, results):
27     """
28     generic method calling the method corresponding to the test case
29     check that the testcase is properly declared first
30     then build the call to the specific method
31     """
32     if check_promise_case_exist(case):
33         # note we add _case because testcase and project had the same name
34         # TODO refactoring...looks fine at the beginning wit only 1 project
35         # not very ugly now and clearly not optimized...
36         cmd = "format_" + case + "_case_for_dashboard"
37         res = globals()[cmd](results)
38     else:
39         res = []
40         print "Test cases not declared"
41     return res
42
43
44 def check_promise_case_exist(case):
45     """
46     check if the testcase exists
47     if the test case is not defined or not declared in the list
48     return False
49     """
50     promise_cases = get_promise_cases()
51
52     if (case is None or case not in promise_cases):
53         return False
54     else:
55         return True
56
57
58
59
60
61 def format_promise_case_for_dashboard(results):
62     """
63     Post processing for the promise test case
64     """
65     test_data = [{'description': 'Promise results for Dashboard'}]
66     # Graph 1: (duration)=f(time)
67     # ***************************************
68     new_element = []
69
70     # default duration 0:00:08.999904
71     # consider only seconds => 09
72     for data in results:
73         t = data['details']['duration']
74         new_element.append({'x': data['creation_date'],
75                             'y': t})
76
77     test_data.append({'name': "Promise duration ",
78                       'info': {'type': "graph",
79                                'xlabel': 'time (s)',
80                                'ylabel': 'duration (s)'},
81                       'data_set': new_element})
82
83     # Graph 2: (Nb test, nb failure)=f(time)
84     # ***************************************
85     new_element = []
86
87     for data in results:
88         promise_results = data['details']
89         new_element.append({'x': data['creation_date'],
90                             'y1': promise_results['tests'],
91                             'y2': promise_results['failures']})
92
93     test_data.append({'name': "Promise nb tests/nb failures",
94                       'info': {'type': "graph",
95                                'xlabel': 'time',
96                                'y1label': 'Number of tests',
97                                'y2label': 'Number of failures'},
98                       'data_set': new_element})
99
100     return test_data