| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
import json |
| 13 |
import logging |
| 14 |
import syslog |
| 15 |
import typing |
| 16 |
import unittest |
| 17 |
from datetime import timedelta |
| 18 |
from unittest import mock |
| 19 |
|
| 20 |
import daiquiri |
| 21 |
from daiquiri import output |
| 22 |
|
| 23 |
|
| 24 |
class DatadogMatcher(object): |
| 25 |
def __init__(self, expected: typing.Any) -> None: |
| 26 |
self.expected = expected |
| 27 |
|
| 28 |
def __eq__(self, other: typing.Any) -> bool: |
| 29 |
return bool(json.loads(other.decode()[:-1]) == self.expected) |
| 30 |
|
| 31 |
def __repr__(self) -> str: |
| 32 |
return ( |
| 33 |
"b'" |
| 34 |
+ json.dumps(self.expected, default=lambda x: "unserializable") |
| 35 |
+ "\\n'" |
| 36 |
) |
| 37 |
|
| 38 |
|
| 39 |
class TestOutput(unittest.TestCase): |
| 40 |
def test_find_facility(self) -> None: |
| 41 |
self.assertEqual(syslog.LOG_USER, output.Syslog._find_facility("user")) |
| 42 |
self.assertEqual(syslog.LOG_LOCAL1, output.Syslog._find_facility("log_local1")) |
| 43 |
self.assertEqual(syslog.LOG_LOCAL2, output.Syslog._find_facility("LOG_local2")) |
| 44 |
self.assertEqual(syslog.LOG_LOCAL3, output.Syslog._find_facility("LOG_LOCAL3")) |
| 45 |
self.assertEqual(syslog.LOG_LOCAL4, output.Syslog._find_facility("LOCaL4")) |
| 46 |
|
| 47 |
def test_get_log_file_path(self) -> None: |
| 48 |
self.assertEqual("foobar.log", output._get_log_file_path("foobar.log")) |
| 49 |
self.assertEqual( |
| 50 |
"/var/log/foo/foobar.log", |
| 51 |
output._get_log_file_path("foobar.log", logdir="/var/log/foo"), |
| 52 |
) |
| 53 |
self.assertEqual( |
| 54 |
"/var/log/foobar.log", |
| 55 |
output._get_log_file_path(logdir="/var/log", program_name="foobar"), |
| 56 |
) |
| 57 |
self.assertEqual( |
| 58 |
"/var/log/foobar.log", |
| 59 |
output._get_log_file_path(logdir="/var/log", program_name="foobar"), |
| 60 |
) |
| 61 |
self.assertEqual( |
| 62 |
"/var/log/foobar.journal", |
| 63 |
output._get_log_file_path( |
| 64 |
logdir="/var/log", logfile_suffix=".journal", program_name="foobar" |
| 65 |
), |
| 66 |
) |
| 67 |
|
| 68 |
def test_timedelta_seconds(self) -> None: |
| 69 |
fn = output.TimedRotatingFile._timedelta_to_seconds |
| 70 |
hour = 60 * 60 |
| 71 |
|
| 72 |
one_hour = [ |
| 73 |
timedelta(hours=1), |
| 74 |
timedelta(minutes=60), |
| 75 |
timedelta(seconds=hour), |
| 76 |
] |
| 77 |
for t in one_hour: |
| 78 |
self.assertEqual(hour, fn(t)) |
| 79 |
|
| 80 |
assert hour == fn(float(hour)) |
| 81 |
assert hour == fn(hour) |
| 82 |
|
| 83 |
error_cases = [ |
| 84 |
"string", |
| 85 |
["some", "list"], |
| 86 |
( |
| 87 |
"some", |
| 88 |
"tuple", |
| 89 |
), |
| 90 |
("tuple",), |
| 91 |
{"dict": "mapping"}, |
| 92 |
] |
| 93 |
for t in error_cases: |
| 94 |
self.assertRaises(AttributeError, fn, t) |
| 95 |
|
| 96 |
def test_datadog(self) -> None: |
| 97 |
with mock.patch("socket.socket") as mock_socket: |
| 98 |
socket_instance = mock_socket.return_value |
| 99 |
daiquiri.setup(outputs=(daiquiri.output.Datadog(),), level=logging.DEBUG) |
| 100 |
logger = daiquiri.getLogger() |
| 101 |
logger.error("foo", bar=1) |
| 102 |
logger.info("bar") |
| 103 |
expected_error_1 = { |
| 104 |
"status": "error", |
| 105 |
"message": "foo", |
| 106 |
"bar": 1, |
| 107 |
"logger": {"name": "root"}, |
| 108 |
"timestamp": mock.ANY, |
| 109 |
} |
| 110 |
expected_info_1 = { |
| 111 |
"status": "info", |
| 112 |
"message": "bar", |
| 113 |
"logger": {"name": "root"}, |
| 114 |
"timestamp": mock.ANY, |
| 115 |
} |
| 116 |
expected_error_2 = { |
| 117 |
"status": "error", |
| 118 |
"message": "backtrace", |
| 119 |
"logger": {"name": "saymyname"}, |
| 120 |
"timestamp": mock.ANY, |
| 121 |
"error": { |
| 122 |
"kind": "ZeroDivisionError", |
| 123 |
"stack": None, |
| 124 |
"message": mock.ANY, |
| 125 |
}, |
| 126 |
} |
| 127 |
try: |
| 128 |
1 / 0 |
| 129 |
except ZeroDivisionError: |
| 130 |
logger = daiquiri.getLogger("saymyname") |
| 131 |
logger.exception("backtrace") |
| 132 |
socket_instance.connect.assert_called_once_with(("127.0.0.1", 10518)) |
| 133 |
socket_instance.sendall.assert_has_calls( |
| 134 |
[ |
| 135 |
mock.call(DatadogMatcher(expected_error_1)), |
| 136 |
mock.call(DatadogMatcher(expected_info_1)), |
| 137 |
mock.call(DatadogMatcher(expected_error_2)), |
| 138 |
] |
| 139 |
) |