Try/except ODL push to DB until ODL tests are refactored
[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> --pod=<pod name>
37                            --installer=<installer> --database=<database url>
38                            --scenario=<scenario>
39     -x, --xml   xml file generated by robot test
40     -p, --pod   POD name where the test come from
41     -i, --installer
42     -s, --scenario
43     -h, --help  this message
44     """
45     sys.exit(2)
46
47
48 def populate_detail(test):
49     detail = {}
50     detail['test_name'] = test['@name']
51     detail['test_status'] = test['status']
52     detail['test_doc'] = test['doc']
53     return detail
54
55
56 def parse_test(tests, details):
57     try:
58         for test in tests:
59             details.append(populate_detail(test))
60     except TypeError:
61         # tests is not iterable
62         details.append(populate_detail(tests))
63     return details
64
65
66 def parse_suites(suites):
67     data = {}
68     details = []
69     try:
70         for suite in suites:
71             data['details'] = parse_test(suite['test'], details)
72     except TypeError:
73         # suites is not iterable
74         data['details'] = parse_test(suites['test'], details)
75     return data
76
77
78 def main(argv):
79     (xml_file, pod, installer, scenario) = None, None, None, None
80     try:
81         opts, args = getopt.getopt(argv,
82                                    'x:p:i:s:h',
83                                    ['xml=', 'pod=',
84                                     'installer=',
85                                     'scenario=',
86                                     'help'])
87     except getopt.GetoptError:
88         usage()
89
90     for opt, arg in opts:
91         if opt in ('-h', '--help'):
92             usage()
93         elif opt in ('-x', '--xml'):
94             xml_file = arg
95         elif opt in ('-p', '--pod'):
96             pod = arg
97         elif opt in ('-i', '--installer'):
98             installer = arg
99         elif opt in ('-s', '--scenario'):
100             scenario = arg
101         else:
102             usage()
103
104     if not all(x is not None for x in (xml_file, pod, installer, scenario)):
105         usage()
106
107     with open(xml_file, "r") as myfile:
108         xml_input = myfile.read().replace('\n', '')
109
110     # dictionary populated with data from xml file
111     all_data = xmltodict.parse(xml_input)['robot']
112
113     try:
114         data = parse_suites(all_data['suite']['suite'])
115         data['description'] = all_data['suite']['@name']
116         data['version'] = all_data['@generator']
117         data['test_project'] = "functest"
118         data['case_name'] = "ODL"
119         data['pod_name'] = pod
120         data['installer'] = installer
121
122         json.dumps(data, indent=4, separators=(',', ': '))
123
124         # example:
125         # python odlreport2db.py -x ~/Pictures/Perso/odl/output3.xml
126         #                        -i fuel
127         #                        -p opnfv-jump-2
128         #                        -s os-odl_l2-ha
129
130         # success criteria for ODL = 100% of tests OK
131         status = "FAIL"
132         # TODO as part of the tests are executed before in the bash
133         # start and stoptime have no real meaning
134         start_time = time.time()
135         stop_time = start_time
136         tests_passed = 0
137         tests_failed = 0
138         for v in data['details']:
139             if v['test_status']['@status'] == "PASS":
140                 tests_passed += 1
141             else:
142                 tests_failed += 1
143
144         if (tests_failed < 1):
145             status = "PASS"
146
147         functest_utils.push_results_to_db("functest",
148                                           data['case_name'],
149                                           None,
150                                           start_time,
151                                           stop_time,
152                                           status,
153                                           data)
154
155     except:
156         print("Error pushing ODL results into DB '%s'" % sys.exc_info()[0])
157
158
159 if __name__ == "__main__":
160     main(sys.argv[1:])