~pwlars/ubuntu-test-cases/krillin-recovery

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
#!/usr/bin/env python3

"""
We always run the system-settle test before and after an autopilot test. This
script takes the results of the before/after results and combines them in with
the junit xml results from the autopilot test so we have one unified report.
"""


import os
import sys

from xml.etree import ElementTree


def _build_node(classname, name, rcfile, stdout):
    e = ElementTree.Element('testcase')
    e.attrib['classname'] = classname
    e.attrib['name'] = 'settle_%s' % name
    rc = int(open(rcfile).read())
    if rc != 0:
        f = ElementTree.Element('failure')
        e.append(f)
        f.attrib['type'] = 'testtools.testresult.real._StringException'
        f.text = open(stdout).read()
    return e, rc != 0


def _get_results(apfile):
    try:
        tree = ElementTree.parse(apfile)
    except Exception as ex:
        e = ElementTree.Element('testsuite')
        tree = ElementTree.ElementTree(e)
        e.attrib['errors'] = '1'
        e.attrib['failures'] = '0'
        e.attrib['tests'] = '1'

        t = ElementTree.Element('testcase')
        e.append(t)
        t.attrib['classname'] = 'phablet-tools'
        t.attrib['name'] = 'phablet-test-run'

        f = ElementTree.Element('failure')
        t.append(f)
        f.attrib['type'] = 'testtools.testresult.real._StringException'
        f.text = str(ex)

    return tree


def _get_classname(results):
    return results[0].attrib.get('classname', '???').split('.')[0]


def combine(resdir):
    ap_file = os.path.join(resdir, 'test_results.xml')
    tree = _get_results(ap_file)
    ap_results = tree.getroot()

    errors = int(ap_results.attrib['errors'])

    classname = _get_classname(ap_results)

    rc = os.path.join(resdir, 'settle_before.rc')
    log = os.path.join(resdir, 'settle_before.log')
    node, failed = _build_node(classname, 'before', rc, log)
    ap_results.insert(0, node)
    if failed:
        errors += 1

    rc = os.path.join(resdir, 'settle_after.rc')
    log = os.path.join(resdir, 'settle_after.log')
    node, failed = _build_node(classname, 'after', rc, log)
    ap_results.append(node)
    if failed:
        errors += 1

    num = int(ap_results.attrib['tests']) + 2
    ap_results.attrib['tests'] = str(num)
    ap_results.attrib['errors'] = str(errors)

    tree.write(ap_file)


if __name__ == '__main__':
    if len(sys.argv) != 2:
        print('usage: {} <results directory>'.format(sys.argv[0]))
        sys.exit(1)

    combine(sys.argv[1])