File: generate_html_report.py

package info (click to toggle)
murano-dashboard 1%3A10.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 2,620 kB
  • sloc: python: 15,632; javascript: 1,627; sh: 106; makefile: 38
file content (162 lines) | stat: -rw-r--r-- 4,967 bytes parent folder | download | duplicates (2)
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/python
# Copyright (c) 2015 Mirantis, Inc.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.
#
import jinja2
import lxml.etree as et
import uuid
import sys
import os
import re

if not __name__ == "__main__":
    sys.exit(1)
if not len(sys.argv) >= 3:
    sys.exit(1)
if not os.path.exists(sys.argv[1]):
    sys.exit(1)

LOG_LINE_PATTERN = "^(?P<date>20[0-9]{2}\-[0-9]{2}\-[0-9]{2}) (?P<time>[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]+) (?P<pid>[0-9]+) (?P<level>[A-Z]+) (?P<package>.*?) \[\-\](?P<log>.*?)$"


def get_attr(element, attr):
    return element.attrib[attr] if attr in element.attrib.keys() else None


def parse_log_file(log_file_path):
    LOG_RECORDS = []
    LAST_LOG_ENTRY = None

    with open(log_file_path, 'r') as log_file:
        for line in log_file.readlines():
            match = re.match(LOG_LINE_PATTERN, line, re.S)
            if match:
                LAST_LOG_ENTRY = {
                    'date': match.group('date'),
                    'time': match.group('time'),
                    'pid': match.group('pid'),
                    'level': match.group('level'),
                    'package': match.group('package'),
                    'log': match.group('log')
                }

                LOG_RECORDS.append(LAST_LOG_ENTRY)
            elif LAST_LOG_ENTRY is not None:
                LAST_LOG_ENTRY['log'] += line

    return [log_record for log_record in LOG_RECORDS if log_record['level'] == "ERROR"]


STATS = {
    'total': 0,
    'success': 0,
    'skip': 0,
    'error': 0,
    'failure': 0,
}

REPORT = {}

et.set_default_parser(et.XMLParser(huge_tree=True))

tree = et.parse(sys.argv[1])
root = tree.getroot()

STATS['total'] = int(root.attrib['tests'])
STATS['failure'] = int(root.attrib['failures'])
STATS['error'] = int(root.attrib['errors'])
STATS['skip'] = int(root.attrib['skip'])
STATS['unsuccess'] = STATS['failure'] + STATS['error'] + STATS['skip']
STATS['success'] = STATS['total'] - STATS['unsuccess']

for case in root:
    class_name = case.attrib['classname']

    screenshot_file = 'logs/artifacts/screenshots/%s.png' % case.attrib['name']
    screenshot_path = os.path.join(
        os.environ.get('WORKSPACE'),
        screenshot_file
    )

    test = {
        'name': case.attrib['name'],
        'time': case.attrib['time'],
        'result': 'success',
        'exc_type': None,
        'exc_message': None,
        'traceback': None,
        'output': case.text,
        'uuid': str(uuid.uuid1()),
        'screenshot': None
    }

    for child in case:
        test['exc_type'] = get_attr(child, 'type')
        test['exc_message'] = get_attr(child, 'message')
        test['traceback'] = child.text
        if child.tag == 'error':
            test['result'] = 'error'
            if os.path.exists(screenshot_path):
                test['screenshot'] = screenshot_file
        elif child.tag == 'failure':
            test['result'] = 'failure'
            if os.path.exists(screenshot_path):
                test['screenshot'] = screenshot_file
        elif child.tag == 'skipped':
            test['result'] = 'skip'

    if class_name not in REPORT.keys():
        REPORT[class_name] = {
            'tests': [],
            'stats': {
                'total': 0,
                'failure': 0,
                'error': 0,
                'skip': 0,
                'success': 0,
            },
            'result': 'success',
            'uuid': str(uuid.uuid1()),
        }

    REPORT[class_name]['tests'].append(test)
    REPORT[class_name]['stats']['total'] += 1
    REPORT[class_name]['stats'][test['result']] += 1

    TOTAL = REPORT[class_name]['stats']['total']

    for class_name in REPORT.keys():
        if REPORT[class_name]['stats']['failure'] > 0:
            REPORT[class_name]['result'] = 'failure'
        elif REPORT[class_name]['stats']['error'] > 0:
            REPORT[class_name]['result'] = 'failure'
        elif REPORT[class_name]['stats']['skip'] == TOTAL:
            REPORT[class_name]['result'] = 'skip'
        else:
            REPORT[class_name]['result'] = 'success'

jinja = jinja2.Environment(
    loader=jinja2.FileSystemLoader(os.path.join(
        os.path.dirname(__file__), 'templates')
    )
)

with open(sys.argv[2], 'w') as report_file:
    report_file.write(jinja.get_template(
        os.path.basename('report.template')
    ).render(
        report=REPORT,
        stats=STATS,
        coverage=False,
    ))