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