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
|
# This file is part of TALER
# (C) 2019 TALER SYSTEMS
#
# This library 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; either
# version 2.1 of the License, or (at your option) any later
# version.
#
# This library 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 library; if not, write to the Free
# Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#
# @author Marcello Stanisci
# @version 0.0
# @repository https://git.taler.net/copylib.git/
# This code is "copylib", it is versioned under the Git repository
# mentioned above, and it is meant to be manually copied into any
# project which might need it.
from unittest import TestCase
from gnunet_log import GnunetLogger as GL
import os
from mock import patch
import logging
# How the logging module defines loglevels.
#
# ERROR = 40
# WARNING = 30
# INFO = 20
# DEBUG = 10
def clean_env():
if os.environ.get("GNUNET_FORCE_LOG"):
del os.environ["GNUNET_FORCE_LOG"]
if os.environ.get("GNUNET_LOG"):
del os.environ["GNUNET_LOG"]
if os.environ.get("GNUNET_FORCE_LOGFILE"):
del os.environ["GNUNET_FORCE_LOGFILE"]
# NOTE: no logs will appear on screen, as the setLevel
# function is mocked (and the level specified won't be
# made effective -- rather, only the very default level
# (WARNING) will apply)!
class TestGnunetLog(TestCase):
def setUp(self):
clean_env()
# This function tests the very basic case, where no
# env variable is set and no explicit loglevel is given
# via the "setup()" method. The expected result is that
# the level is set to INFO.
@patch("logging.Logger.setLevel")
@patch("logging.basicConfig")
def test_no_env_and_no_setup(self, mocked_basicConfig, mocked_setLevel):
# Double-check no env variable gets in the way.
assert None == os.environ.get("GNUNET_FORCE_LOG")
assert None == os.environ.get("GNUNET_LOG")
gl = GL("gnunet-pylog")
gl.log("msg", gl.INFO)
mocked_setLevel.assert_called_with(level=logging.INFO)
# This function tests the case where *only* the GNUNET_LOG
# env variable is set -- not even the manual setup of the
# loglevel is put in place.
@patch("logging.Logger.setLevel")
@patch("logging.basicConfig")
def test_non_forced_env(self, mocked_basicConfig, mocked_setLevel):
assert None == os.environ.get("GNUNET_FORCE_LOG")
os.environ["GNUNET_LOG"] = "gnunet-pylog;test_ut.py;test_non_forced_env;60-90;ERROR"
gl = GL("gnunet-pylog")
gl.log("msg", gl.DEBUG)
mocked_setLevel.assert_called_with(level=logging.ERROR)
# This function tests the case where *only* the GNUNET_FORCE_LOG
# env variable is set -- not even the manual setup of the loglevel
# is put in place.
@patch("logging.Logger.setLevel")
@patch("logging.basicConfig")
def test_only_forced_env(self, mocked_basicConfig, mocked_setLevel):
assert None == os.environ.get("GNUNET_LOG")
os.environ["GNUNET_FORCE_LOG"] = "gnunet-pylog;test_ut.py;test_only_forced_env;70-100;ERROR"
gl = GL("gnunet-pylog")
gl.log("msg", gl.DEBUG)
mocked_setLevel.assert_called_with(level=logging.ERROR)
# This function tests the case where *only* the manual
# loglevel setup is put in place.
@patch("logging.Logger.setLevel")
@patch("logging.basicConfig")
def test_only_manual_loglevel_setup(self, mocked_basicConfig, mocked_setLevel):
assert None == os.environ.get("GNUNET_LOG")
assert None == os.environ.get("GNUNET_FORCE_LOG")
gl = GL("gnunet-pylog")
gl.setup(gl.DEBUG)
gl.log("msg", gl.WARNING)
mocked_setLevel.assert_called_with(level=logging.DEBUG)
|