Merge "BGPVPN test case 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 #
22 #
23
24 import getopt
25 import json
26 import os
27 import sys
28 import xmltodict
29 import yaml
30
31 import functest.utils.functest_utils as functest_utils
32
33
34 def usage():
35     print """Usage:
36     get-json-from-robot.py --xml=<output.xml> --pod=<pod_name>
37                            --installer=<installer> --database=<Database URL>
38                            --scenaro=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     try:
80         opts, args = getopt.getopt(argv,
81                                    'x:p:i:s:h',
82                                    ['xml=', 'pod=',
83                                     'installer=',
84                                     'scenario=',
85                                     'help'])
86     except getopt.GetoptError:
87         usage()
88
89     for opt, arg in opts:
90         if opt in ('-h', '--help'):
91             usage()
92         elif opt in ('-x', '--xml'):
93             xml_file = arg
94         elif opt in ('-p', '--pod'):
95             pod = arg
96         elif opt in ('-i', '--installer'):
97             installer = arg
98         elif opt in ('-s', '--scenario'):
99             scenario = arg
100         else:
101             usage()
102
103     with open(xml_file, "r") as myfile:
104         xml_input = myfile.read().replace('\n', '')
105
106     # dictionary populated with data from xml file
107     all_data = xmltodict.parse(xml_input)['robot']
108
109     data = parse_suites(all_data['suite']['suite'])
110     data['description'] = all_data['suite']['@name']
111     data['version'] = all_data['@generator']
112     data['test_project'] = "functest"
113     data['case_name'] = "ODL"
114     data['pod_name'] = pod
115     data['installer'] = installer
116
117     json.dumps(data, indent=4, separators=(',', ': '))
118
119     # Only used from container, we can set up absolute path
120     with open(os.environ["CONFIG_FUNCTEST_YAML"]) as f:
121         functest_yaml = yaml.safe_load(f)
122         f.close()
123
124     database = functest_yaml.get("results").get("test_db_url")
125     build_tag = functest_utils.get_build_tag()
126
127     try:
128         # example:
129         # python odlreport2db.py -x ~/Pictures/Perso/odl/output3.xml
130         #                        -i fuel
131         #                        -p opnfv-jump-2
132         #                        -s os-odl_l2-ha
133         version = functest_utils.get_version()
134
135         # success criteria for ODL = 100% of tests OK
136         status = "failed"
137         try:
138             tests_passed = 0
139             tests_failed = 0
140             for v in data['details']:
141                 if v['test_status']['@status'] == "PASS":
142                     tests_passed += 1
143                 else:
144                     tests_failed += 1
145
146             if (tests_failed < 1):
147                 status = "passed"
148         except:
149             print("Unable to set criteria" % sys.exc_info()[0])
150         functest_utils.push_results_to_db(database,
151                                           "functest",
152                                           data['case_name'],
153                                           None,
154                                           data['pod_name'],
155                                           version,
156                                           scenario,
157                                           status,
158                                           build_tag,
159                                           data)
160     except:
161         print("Error pushing results into Database '%s'" % sys.exc_info()[0])
162
163
164 if __name__ == "__main__":
165     main(sys.argv[1:])