summaryrefslogtreecommitdiffstats
path: root/testcases/Controllers/ODL/odlreport2db.py
blob: 6b3fb913db856fb98c9e0320d3610da927293f57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/python
#
# Authors:
# - peter.bandzi@cisco.com
# - morgan.richomme@orange.com
#
# src: Peter Bandzi
# https://github.com/pbandzi/parse-robot/blob/master/convert_robot_to_json.py
#
# Copyright (c) 2015 All rights reserved
# This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
#
# http://www.apache.org/licenses/LICENSE-2.0
#

import getopt
import json
import sys
import time
import xmltodict

import functest.utils.functest_utils as functest_utils


def usage():
    print """Usage:
    python odlreport2db.py --xml=<output.xml> --help
    -x, --xml   xml file generated by robot test
    -h, --help  this message
    """
    sys.exit(2)


def populate_detail(test):
    detail = {}
    detail['name'] = test['@name']
    for x in ['status', 'critical', 'starttime', 'endtime']:
        detail[x] = test['status']['@' + x]
    if '#text' in test['status']:
        detail['text'] = test['status']['#text']
    return detail


def parse_test(tests, details):
    try:
        for test in tests:
            details.append(populate_detail(test))
    except TypeError:
        # tests is not iterable
        details.append(populate_detail(tests))
    return details


def parse_suites(suites):
    data = {}
    details = []
    for suite in suites:
        a = suite['suite']
        if type(a) == list:
            for b in a:
                data['tests'] = parse_test(b['test'], details)
        else:
            data['tests'] = parse_test(a['test'], details)

        # data['details'] = parse_test(suite['test'], details)
    # suites is not iterable
    return data


def main(argv):
    xml_file = None
    try:
        opts, args = getopt.getopt(argv,
                                   'x:h',
                                   ['xml=', 'help'])
    except getopt.GetoptError:
        usage()

    for opt, arg in opts:
        if opt in ('-x', '--xml'):
            xml_file = arg
        else:
            usage()

    if xml_file is None:
        usage()

    with open(xml_file, "r") as myfile:
        xml_input = myfile.read().replace('\n', '')

    # dictionary populated with data from xml file
    all_data = xmltodict.parse(xml_input)['robot']

    try:
        data = parse_suites(all_data['suite']['suite'])
        data['description'] = all_data['suite']['@name']
        data['generator'] = all_data['@generator']

        json.dumps(data, indent=4, separators=(',', ': '))

        # success criteria for ODL = 100% of tests OK
        status = "FAIL"
        # TODO as part of the tests are executed before in the bash
        # start and stoptime have no real meaning
        start_time = time.time()
        stop_time = start_time
        tests_passed = 0
        tests_failed = 0
        for v in data['tests']:
            if v['status'] == "PASS":
                tests_passed += 1
            else:
                tests_failed += 1

        if (tests_failed < 1):
            status = "PASS"

        functest_utils.push_results_to_db("functest",
                                          "odl",
                                          None,
                                          start_time,
                                          stop_time,
                                          status,
                                          data)

    except:
        print("Error pushing ODL results into DB '%s'" % sys.exc_info()[0])


if __name__ == "__main__":
    main(sys.argv[1:])