~3v1n0/autopilot/badwindow-errors-protect

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
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
#
# Autopilot Functional Test Tool
# Copyright (C) 2012-2013 Canonical
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#


"""Autopilot test result classes"""

import logging

from testtools import (
    ExtendedToOriginalDecorator,
    ExtendedToStreamDecorator,
    TestResultDecorator,
    TextTestResult,
    try_import,
)

from autopilot.globals import get_log_verbose
from autopilot.utilities import _raise_on_unknown_kwargs


class LoggedTestResultDecorator(TestResultDecorator):

    """A decorator that logs messages to python's logging system."""

    def _log(self, level, message):
        """Perform the actual message logging."""
        if get_log_verbose():
            logging.getLogger().log(level, message)

    def _log_details(self, level, test):
        """Log the relavent test details."""
        if hasattr(test, "getDetails"):
            details = test.getDetails()
            for detail in details:
                # Skip the test-log as it was logged while the test executed
                if detail == "test-log":
                    continue
                detail_content = details[detail]
                if detail_content.content_type.type == "text":
                    text = "%s: {{{\n%s}}}" % (
                        detail,
                        detail_content.as_text()
                    )
                else:
                    text = "Binary attachment: \"%s\" (%s)" % (
                        detail,
                        detail_content.content_type
                    )
                self._log(level, text)

    def addSuccess(self, test, details=None):
        self._log(logging.INFO, "OK: %s" % (test.id()))
        return super().addSuccess(test, details)

    def addError(self, test, err=None, details=None):
        self._log(logging.ERROR, "ERROR: %s" % (test.id()))
        self._log_details(logging.ERROR, test)
        return super().addError(test, err, details)

    def addFailure(self, test, err=None, details=None):
        """Called for a test which failed an assert."""
        self._log(logging.ERROR, "FAIL: %s" % (test.id()))
        self._log_details(logging.ERROR, test)
        return super().addFailure(test, err, details)

    def addSkip(self, test, reason=None, details=None):
        self._log(logging.INFO, "SKIP: %s" % test.id())
        return super().addSkip(test, reason, details)

    def addUnexpectedSuccess(self, test, details=None):
        self._log(logging.ERROR, "UNEXPECTED SUCCESS: %s" % test.id())
        self._log_details(logging.ERROR, test)
        return super().addUnexpectedSuccess(test, details)

    def addExpectedFailure(self, test, err=None, details=None):
        self._log(logging.INFO, "EXPECTED FAILURE: %s" % test.id())
        return super().addExpectedFailure(test, err, details)


def get_output_formats():
    """Get information regarding the different output formats supported.

    :returns: dict of supported formats and appropriate construct functions

    """
    supported_formats = {}

    supported_formats['text'] = _construct_text

    if try_import('junitxml'):
        supported_formats['xml'] = _construct_xml
    if try_import('subunit'):
        supported_formats['subunit'] = _construct_subunit
    return supported_formats


def get_default_format():
    return 'text'


def _construct_xml(**kwargs):
    from junitxml import JUnitXmlResult
    stream = kwargs.pop('stream')
    failfast = kwargs.pop('failfast')
    _raise_on_unknown_kwargs(kwargs)
    result_object = LoggedTestResultDecorator(
        ExtendedToOriginalDecorator(
            JUnitXmlResult(stream)
        )
    )
    result_object.failfast = failfast
    return result_object


def _construct_text(**kwargs):
    stream = kwargs.pop('stream')
    failfast = kwargs.pop('failfast')
    _raise_on_unknown_kwargs(kwargs)
    return LoggedTestResultDecorator(TextTestResult(stream, failfast))


def _construct_subunit(**kwargs):
    from subunit import StreamResultToBytes
    stream = kwargs.pop('stream')
    failfast = kwargs.pop('failfast')
    _raise_on_unknown_kwargs(kwargs)
    result_object = LoggedTestResultDecorator(
        ExtendedToStreamDecorator(
            StreamResultToBytes(stream)
        )
    )
    result_object.failfast = failfast
    return result_object