Merge "Add qtip job to pod zte-virtual6"
[releng.git] / utils / test / dashboard / dashboard / functest / format.py
1 #! /usr/bin/env python
2
3
4 def _convert_value(value):
5     return value if value != '' else 0
6
7
8 def _convert_duration(duration):
9     if ((isinstance(duration, str) or
10             isinstance(duration, unicode)) and ':' in duration):
11         hours, minutes, seconds = duration.split(":")
12         hours = _convert_value(hours)
13         minutes = _convert_value(minutes)
14         seconds = _convert_value(seconds)
15         int_duration = 3600 * int(hours) + 60 * int(minutes) + float(seconds)
16     else:
17         int_duration = duration
18     return int_duration
19
20
21 def format_normal(testcase):
22     """
23     Look for these and leave any of those:
24         details.duration
25         details.tests
26         details.failures
27
28     If none are present, then return False
29     """
30     found = False
31     testcase_details = testcase['details']
32     fields = ['duration', 'tests', 'failures']
33     if isinstance(testcase_details, dict):
34         for key, value in testcase_details.items():
35             if key in fields:
36                 found = True
37                 if key == 'duration':
38                     testcase_details[key] = _convert_duration(value)
39             else:
40                 del testcase_details[key]
41
42     if 'tests' in testcase_details and 'failures' in testcase_details:
43         testcase_tests = float(testcase_details['tests'])
44         testcase_failures = float(testcase_details['failures'])
45         if testcase_tests != 0:
46             testcase_details['success_percentage'] = 100 * \
47                 (testcase_tests - testcase_failures) / testcase_tests
48         else:
49             testcase_details['success_percentage'] = 0
50
51     return found
52
53
54 def format_rally(testcase):
55     """
56     Structure:
57         details.[{summary.duration}]
58         details.[{summary.nb success}]
59         details.[{summary.nb tests}]
60
61     Find data for these fields
62         -> details.duration
63         -> details.tests
64         -> details.success_percentage
65     """
66     details = testcase['details']
67     summary = None
68     for item in details:
69         if 'summary' in item:
70             summary = item['summary']
71
72     if not summary:
73         return False
74
75     testcase['details'] = {
76         'duration': summary['duration'],
77         'tests': summary['nb tests'],
78         'success_percentage': summary['nb success']
79     }
80     return True
81
82
83 def _get_statistics(orig_data, stat_fields, stat_values=None):
84     test_results = {}
85     for stat_data in orig_data:
86         for field in stat_fields:
87             stat_value = stat_data[field]
88             if stat_value in test_results:
89                 test_results[stat_value] += 1
90             else:
91                 test_results[stat_value] = 1
92
93     if stat_values is not None:
94         for stat_value in stat_values:
95             if stat_value not in test_results:
96                 test_results[stat_value] = 0
97
98     return test_results
99
100
101 def format_onos(testcase):
102     """
103     Structure:
104         details.FUNCvirNet.duration
105         details.FUNCvirNet.status.[{Case result}]
106         details.FUNCvirNetL3.duration
107         details.FUNCvirNetL3.status.[{Case result}]
108
109     Find data for these fields
110         -> details.FUNCvirNet.duration
111         -> details.FUNCvirNet.tests
112         -> details.FUNCvirNet.failures
113         -> details.FUNCvirNetL3.duration
114         -> details.FUNCvirNetL3.tests
115         -> details.FUNCvirNetL3.failures
116     """
117     testcase_details = testcase['details']
118
119     if ('FUNCvirNet' not in testcase_details or
120             'FUNCvirNetL3' not in testcase_details):
121         return False
122
123     funcvirnet_details = testcase_details['FUNCvirNet']['status']
124     funcvirnet_stats = _get_statistics(
125         funcvirnet_details, ('Case result',), ('PASS', 'FAIL'))
126     funcvirnet_passed = funcvirnet_stats['PASS']
127     funcvirnet_failed = funcvirnet_stats['FAIL']
128     funcvirnet_all = funcvirnet_passed + funcvirnet_failed
129
130     funcvirnetl3_details = testcase_details['FUNCvirNetL3']['status']
131     funcvirnetl3_stats = _get_statistics(
132         funcvirnetl3_details, ('Case result',), ('PASS', 'FAIL'))
133     funcvirnetl3_passed = funcvirnetl3_stats['PASS']
134     funcvirnetl3_failed = funcvirnetl3_stats['FAIL']
135     funcvirnetl3_all = funcvirnetl3_passed + funcvirnetl3_failed
136
137     testcase_details['FUNCvirNet'] = {
138         'duration':
139         _convert_duration(testcase_details['FUNCvirNet']['duration']),
140         'tests': funcvirnet_all,
141         'failures': funcvirnet_failed
142     }
143     testcase_details['FUNCvirNetL3'] = {
144         'duration':
145         _convert_duration(testcase_details['FUNCvirNetL3']['duration']),
146         'tests': funcvirnetl3_all,
147         'failures': funcvirnetl3_failed
148     }
149     return True
150
151
152 def format_vims(testcase):
153     """
154     Structure:
155         details.sig_test.result.[{result}]
156         details.sig_test.duration
157         details.vIMS.duration
158         details.orchestrator.duration
159
160     Find data for these fields
161         -> details.sig_test.duration
162         -> details.sig_test.tests
163         -> details.sig_test.failures
164         -> details.sig_test.passed
165         -> details.sig_test.skipped
166         -> details.vIMS.duration
167         -> details.orchestrator.duration
168     """
169     testcase_details = testcase['details']
170     test_results = _get_statistics(testcase_details['sig_test']['result'],
171                                    ('result',),
172                                    ('Passed', 'Skipped', 'Failed'))
173     passed = test_results['Passed']
174     skipped = test_results['Skipped']
175     failures = test_results['Failed']
176     all_tests = passed + skipped + failures
177     testcase['details'] = {
178         'sig_test': {
179             'duration': testcase_details['sig_test']['duration'],
180             'tests': all_tests,
181             'failures': failures,
182             'passed': passed,
183             'skipped': skipped
184         },
185         'vIMS': {
186             'duration': testcase_details['vIMS']['duration']
187         },
188         'orchestrator': {
189             'duration': testcase_details['orchestrator']['duration']
190         }
191     }
192     return True