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