bugfix: _convert_duration error when encounter ':00:00.078149'
[releng.git] / utils / test / scripts / mongo_to_elasticsearch.py
1 #! /usr/bin/env python
2 import logging
3 import argparse
4 import shared_utils
5 import json
6 import urlparse
7 import uuid
8 import os
9 import subprocess
10 import datetime
11
12 logger = logging.getLogger('mongo_to_elasticsearch')
13 logger.setLevel(logging.DEBUG)
14 file_handler = logging.FileHandler('/var/log/{}.log'.format(__name__))
15 file_handler.setFormatter(logging.Formatter('%(asctime)s %(levelname)s: %(message)s'))
16 logger.addHandler(file_handler)
17
18
19 def _get_dicts_from_list(dict_list, keys):
20     dicts = []
21     for dictionary in dict_list:
22         # iterate over dictionaries in input list
23         if keys == set(dictionary.keys()):
24             # check the dictionary structure
25             dicts.append(dictionary)
26     return dicts
27
28
29 def _get_results_from_list_of_dicts(list_of_dict_statuses, dict_indexes, expected_results=None):
30     test_results = {}
31     for test_status in list_of_dict_statuses:
32         status = test_status
33         for index in dict_indexes:
34             status = status[index]
35         if status in test_results:
36             test_results[status] += 1
37         else:
38             test_results[status] = 1
39
40     if expected_results is not None:
41         for expected_result in expected_results:
42             if expected_result not in test_results:
43                 test_results[expected_result] = 0
44
45     return test_results
46
47
48 def _convert_value(value):
49     return value if value != '' else 0
50
51
52 def _convert_duration(duration):
53     if (isinstance(duration, str) or isinstance(duration, unicode)) and ':' in duration:
54         hours, minutes, seconds = duration.split(":")
55         hours = _convert_value(hours)
56         minutes = _convert_value(minutes)
57         seconds = _convert_value(seconds)
58         int_duration = 3600 * int(hours) + 60 * int(minutes) + float(seconds)
59     else:
60         int_duration = duration
61     return int_duration
62
63
64 def modify_functest_tempest(testcase):
65     if modify_default_entry(testcase):
66         testcase_details = testcase['details']
67         testcase_tests = float(testcase_details['tests'])
68         testcase_failures = float(testcase_details['failures'])
69         if testcase_tests != 0:
70             testcase_details['success_percentage'] = 100 * (testcase_tests - testcase_failures) / testcase_tests
71         else:
72             testcase_details['success_percentage'] = 0
73         return True
74     else:
75         return False
76
77
78 def modify_functest_vims(testcase):
79     """
80     Structure:
81         details.sig_test.result.[{result}]
82         details.sig_test.duration
83         details.vIMS.duration
84         details.orchestrator.duration
85
86     Find data for these fields
87         -> details.sig_test.duration
88         -> details.sig_test.tests
89         -> details.sig_test.failures
90         -> details.sig_test.passed
91         -> details.sig_test.skipped
92         -> details.vIMS.duration
93         -> details.orchestrator.duration
94     """
95     testcase_details = testcase['details']
96     sig_test_results = _get_dicts_from_list(testcase_details['sig_test']['result'],
97                                             {'duration', 'result', 'name', 'error'})
98     if len(sig_test_results) < 1:
99         logger.info("No 'result' from 'sig_test' found in vIMS details, skipping")
100         return False
101     else:
102         test_results = _get_results_from_list_of_dicts(sig_test_results, ('result',), ('Passed', 'Skipped', 'Failed'))
103         passed = test_results['Passed']
104         skipped = test_results['Skipped']
105         failures = test_results['Failed']
106         all_tests = passed + skipped + failures
107         testcase['details'] = {
108             'sig_test': {
109                 'duration': testcase_details['sig_test']['duration'],
110                 'tests': all_tests,
111                 'failures': failures,
112                 'passed': passed,
113                 'skipped': skipped
114             },
115             'vIMS': {
116                 'duration': testcase_details['vIMS']['duration']
117             },
118             'orchestrator': {
119                 'duration': testcase_details['orchestrator']['duration']
120             }
121         }
122         return True
123
124
125 def modify_functest_onos(testcase):
126     """
127     Structure:
128         details.FUNCvirNet.duration
129         details.FUNCvirNet.status.[{Case result}]
130         details.FUNCvirNetL3.duration
131         details.FUNCvirNetL3.status.[{Case result}]
132
133     Find data for these fields
134         -> details.FUNCvirNet.duration
135         -> details.FUNCvirNet.tests
136         -> details.FUNCvirNet.failures
137         -> details.FUNCvirNetL3.duration
138         -> details.FUNCvirNetL3.tests
139         -> details.FUNCvirNetL3.failures
140     """
141     testcase_details = testcase['details']
142
143     funcvirnet_details = testcase_details['FUNCvirNet']['status']
144     funcvirnet_statuses = _get_dicts_from_list(funcvirnet_details, {'Case result', 'Case name:'})
145
146     funcvirnetl3_details = testcase_details['FUNCvirNetL3']['status']
147     funcvirnetl3_statuses = _get_dicts_from_list(funcvirnetl3_details, {'Case result', 'Case name:'})
148
149     if len(funcvirnet_statuses) < 0:
150         logger.info("No results found in 'FUNCvirNet' part of ONOS results")
151         return False
152     elif len(funcvirnetl3_statuses) < 0:
153         logger.info("No results found in 'FUNCvirNetL3' part of ONOS results")
154         return False
155     else:
156         funcvirnet_results = _get_results_from_list_of_dicts(funcvirnet_statuses,
157                                                              ('Case result',), ('PASS', 'FAIL'))
158         funcvirnetl3_results = _get_results_from_list_of_dicts(funcvirnetl3_statuses,
159                                                                ('Case result',), ('PASS', 'FAIL'))
160
161         funcvirnet_passed = funcvirnet_results['PASS']
162         funcvirnet_failed = funcvirnet_results['FAIL']
163         funcvirnet_all = funcvirnet_passed + funcvirnet_failed
164
165         funcvirnetl3_passed = funcvirnetl3_results['PASS']
166         funcvirnetl3_failed = funcvirnetl3_results['FAIL']
167         funcvirnetl3_all = funcvirnetl3_passed + funcvirnetl3_failed
168
169         testcase_details['FUNCvirNet'] = {
170             'duration': _convert_duration(testcase_details['FUNCvirNet']['duration']),
171             'tests': funcvirnet_all,
172             'failures': funcvirnet_failed
173         }
174
175         testcase_details['FUNCvirNetL3'] = {
176             'duration': _convert_duration(testcase_details['FUNCvirNetL3']['duration']),
177             'tests': funcvirnetl3_all,
178             'failures': funcvirnetl3_failed
179         }
180
181         return True
182
183
184 def modify_functest_rally(testcase):
185     """
186     Structure:
187         details.[{summary.duration}]
188         details.[{summary.nb success}]
189         details.[{summary.nb tests}]
190
191     Find data for these fields
192         -> details.duration
193         -> details.tests
194         -> details.success_percentage
195     """
196     summaries = _get_dicts_from_list(testcase['details'], {'summary'})
197
198     if len(summaries) != 1:
199         logger.info("Found zero or more than one 'summaries' in Rally details, skipping")
200         return False
201     else:
202         summary = summaries[0]['summary']
203         testcase['details'] = {
204             'duration': summary['duration'],
205             'tests': summary['nb tests'],
206             'success_percentage': summary['nb success']
207         }
208         return True
209
210
211 def modify_functest_odl(testcase):
212     """
213     Structure:
214         details.details.[{test_status.@status}]
215
216     Find data for these fields
217         -> details.tests
218         -> details.failures
219         -> details.success_percentage?
220     """
221     test_statuses = _get_dicts_from_list(testcase['details']['details'], {'test_status', 'test_doc', 'test_name'})
222     if len(test_statuses) < 1:
223         logger.info("No 'test_status' found in ODL details, skipping")
224         return False
225     else:
226         test_results = _get_results_from_list_of_dicts(test_statuses, ('test_status', '@status'), ('PASS', 'FAIL'))
227
228         passed_tests = test_results['PASS']
229         failed_tests = test_results['FAIL']
230         all_tests = passed_tests + failed_tests
231
232         testcase['details'] = {
233             'tests': all_tests,
234             'failures': failed_tests,
235             'success_percentage': 100 * passed_tests / float(all_tests)
236         }
237         return True
238
239
240 def modify_default_entry(testcase):
241     """
242     Look for these and leave any of those:
243         details.duration
244         details.tests
245         details.failures
246
247     If none are present, then return False
248     """
249     found = False
250     testcase_details = testcase['details']
251     fields = ['duration', 'tests', 'failures']
252     if isinstance(testcase_details, dict):
253         for key, value in testcase_details.items():
254             if key in fields:
255                 found = True
256                 if key == 'duration':
257                     testcase_details[key] = _convert_duration(value)
258             else:
259                 del testcase_details[key]
260
261     return found
262
263
264 def _fix_date(date_string):
265     if isinstance(date_string, dict):
266         return date_string['$date']
267     else:
268         return date_string[:-3].replace(' ', 'T') + 'Z'
269
270
271 def verify_mongo_entry(testcase):
272     """
273     Mandatory fields:
274         installer
275         pod_name
276         version
277         case_name
278         date
279         project
280         details
281
282         these fields must be present and must NOT be None
283
284     Optional fields:
285         description
286
287         these fields will be preserved if the are NOT None
288     """
289     mandatory_fields = ['installer',
290                         'pod_name',
291                         'version',
292                         'case_name',
293                         'project_name',
294                         'details']
295     mandatory_fields_to_modify = {'creation_date': _fix_date}
296     if '_id' in testcase:
297         mongo_id = testcase['_id']
298     else:
299         mongo_id = None
300     optional_fields = ['description']
301     for key, value in testcase.items():
302         if key in mandatory_fields:
303             if value is None:
304                 # empty mandatory field, invalid input
305                 logger.info("Skipping testcase with mongo _id '{}' because the testcase was missing value"
306                             " for mandatory field '{}'".format(mongo_id, key))
307                 return False
308             else:
309                 mandatory_fields.remove(key)
310         elif key in mandatory_fields_to_modify:
311             if value is None:
312                 # empty mandatory field, invalid input
313                 logger.info("Skipping testcase with mongo _id '{}' because the testcase was missing value"
314                             " for mandatory field '{}'".format(mongo_id, key))
315                 return False
316             else:
317                 testcase[key] = mandatory_fields_to_modify[key](value)
318                 del mandatory_fields_to_modify[key]
319         elif key in optional_fields:
320             if value is None:
321                 # empty optional field, remove
322                 del testcase[key]
323             optional_fields.remove(key)
324         else:
325             # unknown field
326             del testcase[key]
327
328     if len(mandatory_fields) > 0:
329         # some mandatory fields are missing
330         logger.info("Skipping testcase with mongo _id '{}' because the testcase was missing"
331                     " mandatory field(s) '{}'".format(mongo_id, mandatory_fields))
332         return False
333     else:
334         return True
335
336
337 def modify_mongo_entry(testcase):
338     # 1. verify and identify the testcase
339     # 2. if modification is implemented, then use that
340     # 3. if not, try to use default
341     # 4. if 2 or 3 is successful, return True, otherwise return False
342     if verify_mongo_entry(testcase):
343         project = testcase['project_name']
344         case_name = testcase['case_name']
345         if project == 'functest':
346             if case_name == 'Rally':
347                 return modify_functest_rally(testcase)
348             elif case_name == 'ODL':
349                 return modify_functest_odl(testcase)
350             elif case_name == 'ONOS':
351                 return modify_functest_onos(testcase)
352             elif case_name == 'vIMS':
353                 return modify_functest_vims(testcase)
354             elif case_name == 'Tempest':
355                 return modify_functest_tempest(testcase)
356         return modify_default_entry(testcase)
357     else:
358         return False
359
360
361 def publish_mongo_data(output_destination):
362     tmp_filename = 'mongo-{}.log'.format(uuid.uuid4())
363     try:
364         subprocess.check_call(['mongoexport', '--db', 'test_results_collection', '-c', 'test_results', '--out',
365                                tmp_filename])
366         with open(tmp_filename) as fobj:
367             for mongo_json_line in fobj:
368                 test_result = json.loads(mongo_json_line)
369                 if modify_mongo_entry(test_result):
370                     shared_utils.publish_json(test_result, output_destination, es_user, es_passwd)
371     finally:
372         if os.path.exists(tmp_filename):
373             os.remove(tmp_filename)
374
375
376 def get_mongo_data(days):
377     past_time = datetime.datetime.today() - datetime.timedelta(days=days)
378     mongo_json_lines = subprocess.check_output(['mongoexport', '--db', 'test_results_collection', '-c', 'test_results',
379                                                 '--query', '{{"creation_date":{{$gt:"{}"}}}}'
380                                                .format(past_time)]).splitlines()
381
382     mongo_data = []
383     for mongo_json_line in mongo_json_lines:
384         test_result = json.loads(mongo_json_line)
385         if modify_mongo_entry(test_result):
386             # if the modification could be applied, append the modified result
387             mongo_data.append(test_result)
388     return mongo_data
389
390
391 def publish_difference(mongo_data, elastic_data, output_destination, es_user, es_passwd):
392     for elastic_entry in elastic_data:
393         if elastic_entry in mongo_data:
394             mongo_data.remove(elastic_entry)
395
396     logger.info('number of parsed test results: {}'.format(len(mongo_data)))
397
398     for parsed_test_result in mongo_data:
399         shared_utils.publish_json(parsed_test_result, es_user, es_passwd, output_destination)
400
401
402 if __name__ == '__main__':
403     parser = argparse.ArgumentParser(description='Modify and filter mongo json data for elasticsearch')
404     parser.add_argument('-od', '--output-destination',
405                         default='elasticsearch',
406                         choices=('elasticsearch', 'stdout'),
407                         help='defaults to elasticsearch')
408
409     parser.add_argument('-ml', '--merge-latest', default=0, type=int, metavar='N',
410                         help='get entries old at most N days from mongodb and'
411                              ' parse those that are not already in elasticsearch.'
412                              ' If not present, will get everything from mongodb, which is the default')
413
414     parser.add_argument('-e', '--elasticsearch-url', default='http://localhost:9200',
415                         help='the url of elasticsearch, defaults to http://localhost:9200')
416
417     parser.add_argument('-u', '--elasticsearch-username',
418                         help='the username for elasticsearch')
419
420     parser.add_argument('-p', '--elasticsearch-password',
421                         help='the password for elasticsearch')
422
423     parser.add_argument('-m', '--mongodb-url', default='http://localhost:8082',
424                         help='the url of mongodb, defaults to http://localhost:8082')
425
426     args = parser.parse_args()
427     base_elastic_url = urlparse.urljoin(args.elasticsearch_url, '/test_results/mongo2elastic')
428     output_destination = args.output_destination
429     days = args.merge_latest
430     es_user = args.elasticsearch_username
431     es_passwd = args.elasticsearch_password
432
433     if output_destination == 'elasticsearch':
434         output_destination = base_elastic_url
435
436     # parsed_test_results will be printed/sent to elasticsearch
437     if days == 0:
438         # TODO get everything from mongo
439         publish_mongo_data(output_destination)
440     elif days > 0:
441         body = '''{{
442     "query" : {{
443         "range" : {{
444             "creation_date" : {{
445                 "gte" : "now-{}d"
446             }}
447         }}
448     }}
449 }}'''.format(days)
450         elastic_data = shared_utils.get_elastic_data(base_elastic_url, es_user, es_passwd, body)
451         logger.info('number of hits in elasticsearch for now-{}d: {}'.format(days, len(elastic_data)))
452         mongo_data = get_mongo_data(days)
453         publish_difference(mongo_data, elastic_data, output_destination, es_user, es_passwd)
454     else:
455         raise Exception('Update must be non-negative')