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)
19 def _get_dicts_from_list(dict_list, keys):
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)
29 def _get_results_from_list_of_dicts(list_of_dict_statuses, dict_indexes, expected_results=None):
31 for test_status in list_of_dict_statuses:
33 for index in dict_indexes:
34 status = status[index]
35 if status in test_results:
36 test_results[status] += 1
38 test_results[status] = 1
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
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)
53 int_duration = duration
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
65 testcase_details['success_percentage'] = 0
71 def modify_functest_vims(testcase):
74 details.sig_test.result.[{result}]
75 details.sig_test.duration
77 details.orchestrator.duration
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
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")
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'] = {
102 'duration': testcase_details['sig_test']['duration'],
104 'failures': failures,
109 'duration': testcase_details['vIMS']['duration']
112 'duration': testcase_details['orchestrator']['duration']
118 def modify_functest_onos(testcase):
121 details.FUNCvirNet.duration
122 details.FUNCvirNet.status.[{Case result}]
123 details.FUNCvirNetL3.duration
124 details.FUNCvirNetL3.status.[{Case result}]
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
134 testcase_details = testcase['details']
136 funcvirnet_details = testcase_details['FUNCvirNet']['status']
137 funcvirnet_statuses = _get_dicts_from_list(funcvirnet_details, {'Case result', 'Case name:'})
139 funcvirnetl3_details = testcase_details['FUNCvirNetL3']['status']
140 funcvirnetl3_statuses = _get_dicts_from_list(funcvirnetl3_details, {'Case result', 'Case name:'})
142 if len(funcvirnet_statuses) < 0:
143 logger.info("No results found in 'FUNCvirNet' part of ONOS results")
145 elif len(funcvirnetl3_statuses) < 0:
146 logger.info("No results found in 'FUNCvirNetL3' part of ONOS results")
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'))
154 funcvirnet_passed = funcvirnet_results['PASS']
155 funcvirnet_failed = funcvirnet_results['FAIL']
156 funcvirnet_all = funcvirnet_passed + funcvirnet_failed
158 funcvirnetl3_passed = funcvirnetl3_results['PASS']
159 funcvirnetl3_failed = funcvirnetl3_results['FAIL']
160 funcvirnetl3_all = funcvirnetl3_passed + funcvirnetl3_failed
162 testcase_details['FUNCvirNet'] = {
163 'duration': _convert_duration(testcase_details['FUNCvirNet']['duration']),
164 'tests': funcvirnet_all,
165 'failures': funcvirnet_failed
168 testcase_details['FUNCvirNetL3'] = {
169 'duration': _convert_duration(testcase_details['FUNCvirNetL3']['duration']),
170 'tests': funcvirnetl3_all,
171 'failures': funcvirnetl3_failed
177 def modify_functest_rally(testcase):
180 details.[{summary.duration}]
181 details.[{summary.nb success}]
182 details.[{summary.nb tests}]
184 Find data for these fields
187 -> details.success_percentage
189 summaries = _get_dicts_from_list(testcase['details'], {'summary'})
191 if len(summaries) != 1:
192 logger.info("Found zero or more than one 'summaries' in Rally details, skipping")
195 summary = summaries[0]['summary']
196 testcase['details'] = {
197 'duration': summary['duration'],
198 'tests': summary['nb tests'],
199 'success_percentage': summary['nb success']
204 def modify_functest_odl(testcase):
207 details.details.[{test_status.@status}]
209 Find data for these fields
212 -> details.success_percentage?
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")
219 test_results = _get_results_from_list_of_dicts(test_statuses, ('test_status', '@status'), ('PASS', 'FAIL'))
221 passed_tests = test_results['PASS']
222 failed_tests = test_results['FAIL']
223 all_tests = passed_tests + failed_tests
225 testcase['details'] = {
227 'failures': failed_tests,
228 'success_percentage': 100 * passed_tests / float(all_tests)
233 def modify_default_entry(testcase):
235 Look for these and leave any of those:
240 If none are present, then return 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():
249 if key == 'duration':
250 testcase_details[key] = _convert_duration(value)
252 del testcase_details[key]
257 def _fix_date(date_string):
258 if isinstance(date_string, dict):
259 return date_string['$date']
261 return date_string[:-3].replace(' ', 'T') + 'Z'
264 def verify_mongo_entry(testcase):
275 these fields must be present and must NOT be None
280 these fields will be preserved if the are NOT None
282 mandatory_fields = ['installer',
288 mandatory_fields_to_modify = {'creation_date': _fix_date}
289 if '_id' in testcase:
290 mongo_id = testcase['_id']
293 optional_fields = ['description']
294 for key, value in testcase.items():
295 if key in mandatory_fields:
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))
302 mandatory_fields.remove(key)
303 elif key in mandatory_fields_to_modify:
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))
310 testcase[key] = mandatory_fields_to_modify[key](value)
311 del mandatory_fields_to_modify[key]
312 elif key in optional_fields:
314 # empty optional field, remove
316 optional_fields.remove(key)
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))
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)
354 def publish_mongo_data(output_destination):
355 tmp_filename = 'mongo-{}.log'.format(uuid.uuid4())
357 subprocess.check_call(['mongoexport', '--db', 'test_results_collection', '-c', 'test_results', '--out',
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)
365 if os.path.exists(tmp_filename):
366 os.remove(tmp_filename)
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()
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)
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)
389 logger.info('number of parsed test results: {}'.format(len(mongo_data)))
391 for parsed_test_result in mongo_data:
392 shared_utils.publish_json(parsed_test_result, es_user, es_passwd, output_destination)
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')
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')
407 parser.add_argument('-e', '--elasticsearch-url', default='http://localhost:9200',
408 help='the url of elasticsearch, defaults to http://localhost:9200')
410 parser.add_argument('-u', '--elasticsearch-username',
411 help='the username for elasticsearch')
413 parser.add_argument('-p', '--elasticsearch-password',
414 help='the password for elasticsearch')
416 parser.add_argument('-m', '--mongodb-url', default='http://localhost:8082',
417 help='the url of mongodb, defaults to http://localhost:8082')
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
426 if output_destination == 'elasticsearch':
427 output_destination = base_elastic_url
429 # parsed_test_results will be printed/sent to elasticsearch
431 # TODO get everything from mongo
432 publish_mongo_data(output_destination)
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)
448 raise Exception('Update must be non-negative')