summaryrefslogtreecommitdiff
path: root/oops/tests/test_createhooks.py
blob: 48686e8e796eedddcbb2b882de2e6e4fa0187a6b (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
# Copyright (c) 2010, 2011, Canonical Ltd
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, version 3 only.
#
# 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# GNU Lesser General Public License version 3 (see the file LICENSE).

"""Tests for the oops config module."""

import datetime
import socket
import sys

import testtools

from oops.createhooks import (
    attach_date,
    attach_exc_info,
    attach_hostname,
    copy_reporter,
    copy_topic,
    copy_url,
    default_hooks,
)


class TestCreateHooks(testtools.TestCase):

    def test_attach_exc_info_missing(self):
        report = {}
        attach_exc_info(report, {})
        self.assertEqual({}, report)

    def test_attach_exc_info_actual_exception(self):
        report = {}
        try:
            raise ValueError("foo bar")
        except ValueError:
            attach_exc_info(report, {"exc_info": sys.exc_info()})
        self.assertEqual("ValueError", report["type"])
        self.assertEqual("foo bar", report["value"])
        self.assertIsInstance(report["tb_text"], str)

    def test_attach_date(self):
        report = {}
        attach_date(report, {})
        self.assertIsInstance(report["time"], datetime.datetime)

    def test_attach_exc_info_strings(self):
        report = {}
        exc_info = {"exc_info": ("ValueError", "foo bar", "my traceback")}
        attach_exc_info(report, exc_info)
        self.assertEqual("ValueError", report["type"])
        self.assertEqual("foo bar", report["value"])
        self.assertEqual("my traceback", report["tb_text"])

    def test_attach_exc_info_broken_exception(self):
        class UnprintableException(Exception):
            def __str__(self):
                raise RuntimeError("arrgh")

            __repr__ = __str__

        report = {}
        try:
            raise UnprintableException("foo bar")
        except UnprintableException:
            attach_exc_info(report, {"exc_info": sys.exc_info()})

        error_msg = "<unprintable UnprintableException object>"
        self.assertEqual(error_msg, report["value"])
        self.assertIsInstance(report["tb_text"], str)

    def test_defaults(self):
        self.assertEqual(
            [
                attach_exc_info,
                attach_date,
                copy_reporter,
                copy_topic,
                copy_url,
                attach_hostname,
            ],
            default_hooks,
        )

    def test_reporter(self):
        report = {}
        copy_reporter(report, {})
        self.assertEqual({}, report)
        copy_reporter(report, {"reporter": "foo"})
        self.assertEqual({"reporter": "foo"}, report)

    def test_topic(self):
        report = {}
        copy_topic(report, {})
        self.assertEqual({}, report)
        copy_topic(report, {"topic": "foo"})
        self.assertEqual({"topic": "foo"}, report)

    def test_url(self):
        report = {}
        copy_url(report, {})
        self.assertEqual({}, report)
        copy_url(report, {"url": "foo"})
        self.assertEqual({"url": "foo"}, report)

    def test_hostname(self):
        report = {}
        attach_hostname(report, {})
        expected_hostname = socket.gethostname()
        self.assertEqual({"hostname": expected_hostname}, report)