Merge "functest compass usage document"
[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 # 0.1: This script boots the VM1 and allocates IP address from Nova
18 # Later, the VM2 boots then execute cloud-init to ping VM1.
19 # After successful ping, both the VMs are deleted.
20 # 0.2: measure test duration and publish results under json format
21 # 0.3: adapt push 2 DB after Test API refacroting
22 #
23 #
24
25 import getopt
26 import json
27 import sys
28 import time
29 import xmltodict
30
31 import functest.utils.functest_utils as functest_utils
32
33
34 def usage():
35     print """Usage:
36     python odlreport2db.py --xml=<output.xml> --help
37     -x, --xml   xml file generated by robot test
38     -h, --help  this message
39     """
40     sys.exit(2)
41
42
43 def populate_detail(test):
44     detail = {}
45     detail['test_name'] = test['@name']
46     detail['test_status'] = test['status']
47     detail['test_doc'] = test['doc']
48     return detail
49
50
51 def parse_test(tests, details):
52     try:
53         for test in tests:
54             details.append(populate_detail(test))
55     except TypeError:
56         # tests is not iterable
57         details.append(populate_detail(tests))
58     return details
59
60
61 def parse_suites(suites):
62     data = {}
63     details = []
64     for suite in suites:
65         a = suite['suite']
66         if type(a) == list:
67             for b in a:
68                 data['details'] = parse_test(b['test'], details)
69         else:
70             data['details'] = parse_test(a['test'], details)
71
72         # data['details'] = parse_test(suite['test'], details)
73     # suites is not iterable
74     return data
75
76
77 def main(argv):
78     xml_file = None
79     try:
80         opts, args = getopt.getopt(argv,
81                                    'x:h',
82                                    ['xml=', 'help'])
83     except getopt.GetoptError:
84         usage()
85
86     for opt, arg in opts:
87         if opt in ('-x', '--xml'):
88             xml_file = arg
89         else:
90             usage()
91
92     if xml_file is None:
93         usage()
94
95     with open(xml_file, "r") as myfile:
96         xml_input = myfile.read().replace('\n', '')
97
98     # dictionary populated with data from xml file
99     all_data = xmltodict.parse(xml_input)['robot']
100
101     try:
102         data = parse_suites(all_data['suite']['suite'])
103         data['description'] = all_data['suite']['@name']
104         data['version'] = all_data['@generator']
105         data['test_project'] = "functest"
106         data['case_name'] = "odl"
107
108         json.dumps(data, indent=4, separators=(',', ': '))
109
110         # success criteria for ODL = 100% of tests OK
111         status = "FAIL"
112         # TODO as part of the tests are executed before in the bash
113         # start and stoptime have no real meaning
114         start_time = time.time()
115         stop_time = start_time
116         tests_passed = 0
117         tests_failed = 0
118         for v in data['details']:
119             if v['test_status']['@status'] == "PASS":
120                 tests_passed += 1
121             else:
122                 tests_failed += 1
123
124         if (tests_failed < 1):
125             status = "PASS"
126
127         functest_utils.push_results_to_db(data['test_project'],
128                                           data['case_name'],
129                                           None,
130                                           start_time,
131                                           stop_time,
132                                           status,
133                                           data)
134
135     except:
136         print("Error pushing ODL results into DB '%s'" % sys.exc_info()[0])
137
138
139 if __name__ == "__main__":
140     main(sys.argv[1:])