Add exec right
[functest.git] / testcases / Controllers / ODL / odlreport2db.py
1 #!/usr/bin/python
2 #
3 # Authors:
4 # - peter.bandzi@cisco.com
5 # - morgan.richomme@orange.com
6 #
7 # src: Peter Bandzi
8 # https://github.com/pbandzi/parse-robot/blob/master/convert_robot_to_json.py
9 #
10 # Copyright (c) 2015 All rights reserved
11 # This program and the accompanying materials
12 # are made available under the terms of the Apache License, Version 2.0
13 # which accompanies this distribution, and is available at
14 #
15 # http://www.apache.org/licenses/LICENSE-2.0
16 #
17
18 import getopt
19 import json
20 import sys
21 import time
22 import xmltodict
23
24 import functest.utils.functest_utils as functest_utils
25
26
27 def usage():
28     print """Usage:
29     python odlreport2db.py --xml=<output.xml> --help
30     -x, --xml   xml file generated by robot test
31     -h, --help  this message
32     """
33     sys.exit(2)
34
35
36 def populate_detail(test):
37     detail = {}
38     detail['name'] = test['@name']
39     for x in ['status', 'critical', 'starttime', 'endtime']:
40         detail[x] = test['status']['@' + x]
41     if '#text' in test['status']:
42         detail['text'] = test['status']['#text']
43     return detail
44
45
46 def parse_test(tests, details):
47     try:
48         for test in tests:
49             details.append(populate_detail(test))
50     except TypeError:
51         # tests is not iterable
52         details.append(populate_detail(tests))
53     return details
54
55
56 def parse_suites(suites):
57     data = {}
58     details = []
59     for suite in suites:
60         a = suite['suite']
61         if type(a) == list:
62             for b in a:
63                 data['tests'] = parse_test(b['test'], details)
64         else:
65             data['tests'] = parse_test(a['test'], details)
66
67         # data['details'] = parse_test(suite['test'], details)
68     # suites is not iterable
69     return data
70
71
72 def main(argv):
73     xml_file = None
74     try:
75         opts, args = getopt.getopt(argv,
76                                    'x:h',
77                                    ['xml=', 'help'])
78     except getopt.GetoptError:
79         usage()
80
81     for opt, arg in opts:
82         if opt in ('-x', '--xml'):
83             xml_file = arg
84         else:
85             usage()
86
87     if xml_file is None:
88         usage()
89
90     with open(xml_file, "r") as myfile:
91         xml_input = myfile.read().replace('\n', '')
92
93     # dictionary populated with data from xml file
94     all_data = xmltodict.parse(xml_input)['robot']
95
96     try:
97         data = parse_suites(all_data['suite']['suite'])
98         data['description'] = all_data['suite']['@name']
99         data['generator'] = all_data['@generator']
100
101         json.dumps(data, indent=4, separators=(',', ': '))
102
103         # success criteria for ODL = 100% of tests OK
104         status = "FAIL"
105         # TODO as part of the tests are executed before in the bash
106         # start and stoptime have no real meaning
107         start_time = time.time()
108         stop_time = start_time
109         tests_passed = 0
110         tests_failed = 0
111         for v in data['tests']:
112             if v['status'] == "PASS":
113                 tests_passed += 1
114             else:
115                 tests_failed += 1
116
117         if (tests_failed < 1):
118             status = "PASS"
119
120         functest_utils.push_results_to_db("functest",
121                                           "odl",
122                                           None,
123                                           start_time,
124                                           stop_time,
125                                           status,
126                                           data)
127
128     except:
129         print("Error pushing ODL results into DB '%s'" % sys.exc_info()[0])
130
131
132 if __name__ == "__main__":
133     main(sys.argv[1:])