Skip to content

Commit 97446ad

Browse files
committed
bug, test: fix tests for trace_id; readd import logging.config
Made save_restore_logging a test level fixture. It was a class level which worked fine until I started using caplog for tests in the same class. Due to loading config from dict, the roundup channel was set to not propagate which broke the new formatting test used for trace_id. Forgot to update some tests due to change in default format adding %(trace_id). Also re-added logging.config import which broke loading logging config files in configuration.py.
1 parent 4e4b409 commit 97446ad

File tree

2 files changed

+15
-16
lines changed

2 files changed

+15
-16
lines changed

roundup/configuration.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import errno
1212
import getopt
1313
import logging
14+
import logging.config
1415
import os
1516
import re
1617
import smtplib

test/test_config.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ class TrackerConfig(unittest.TestCase):
422422
def inject_fixtures(self, caplog):
423423
self._caplog = caplog
424424

425-
@pytest.fixture(scope="class")
425+
@pytest.fixture(autouse=True)
426426
def save_restore_logging(self):
427427
"""Save logger state and try to restore it after all tests in
428428
this class have finished.
@@ -461,7 +461,7 @@ def save_restore_logging(self):
461461
# cribbed from configuration.py:init_loggers
462462
hdlr = logging.StreamHandler(sys.stdout)
463463
formatter = logging.Formatter(
464-
'%(asctime)s %(levelname)s %(message)s')
464+
'%(asctime)s %(trace_id)s %(levelname)s %(message)s')
465465
hdlr.setFormatter(formatter)
466466

467467
for logger in roundup_loggers:
@@ -1019,11 +1019,7 @@ def testFormattedLogging(self):
10191019
def find_file_occurances(string):
10201020
return len(re.findall(r'\bFile\b', string))
10211021

1022-
config = configuration.CoreConfig()
1023-
1024-
config.LOGGING_LEVEL = "DEBUG"
1025-
config.init_logging()
1026-
1022+
config = configuration.CoreConfig(settings={"LOGGING_LEVEL": "DEBUG"})
10271023

10281024
# format the record and verify the logformat/trace_id.
10291025
config._logging_test(None, msg="message")
@@ -1036,7 +1032,9 @@ def find_file_occurances(string):
10361032
# verify that %(trace_id) was set and substituted
10371033
# Note: trace_id is not initialized in this test case
10381034
log_parts = log.split()
1039-
self.assertRegex(log_parts[2], r'^[A-Za-z0-9]{22}')
1035+
# testing len(shorten_int_uuid(uuid.uuid4().int))
1036+
# for 20000 tests gives range [19,22]
1037+
self.assertRegex(log_parts[2], r'^[A-Za-z0-9]{19,22}')
10401038
self._caplog.clear()
10411039

10421040
# the rest check various values of sinfo and msg formating.
@@ -1250,25 +1248,25 @@ def testLoggerFormat(self):
12501248

12511249
# verify config is initalized to defaults
12521250
self.assertEqual(config['LOGGING_FORMAT'],
1253-
'%(asctime)s %(levelname)s %(message)s')
1251+
'%(asctime)s %(trace_id)s %(levelname)s %(message)s')
12541252

12551253
# load config
12561254
config.load(self.dirname)
12571255
self.assertEqual(config['LOGGING_FORMAT'],
1258-
'%(asctime)s %(levelname)s %(message)s')
1256+
'%(asctime)s %(trace_id)s %(levelname)s %(message)s')
12591257

12601258
# break config using an incomplete format specifier (no trailing 's')
1261-
self.munge_configini(mods=[ ("format = ", "%%(asctime)s %%(levelname) %%(message)s") ], section="[logging]")
1259+
self.munge_configini(mods=[ ("format = ", "%%(asctime)s %%(trace_id)s %%(levelname) %%(message)s") ], section="[logging]")
12621260

12631261
# load config
12641262
with self.assertRaises(configuration.OptionValueError) as cm:
12651263
config.load(self.dirname)
12661264

1267-
self.assertIn('Unrecognized use of %(...) in: %(levelname)',
1265+
self.assertIn('Unrecognized use of %(...) in: %(levelname)',
12681266
cm.exception.args[2])
12691267

1270-
# break config by not dubling % sign to quote it from configparser
1271-
self.munge_configini(mods=[ ("format = ", "%(asctime)s %%(levelname) %%(message)s") ], section="[logging]")
1268+
# break config by not doubling % sign to quote it from configparser
1269+
self.munge_configini(mods=[ ("format = ", "%(asctime)s %%(trace_id)s %%(levelname) %%(message)s") ], section="[logging]")
12721270

12731271
with self.assertRaises(
12741272
configuration.ParsingOptionError) as cm:
@@ -1279,8 +1277,8 @@ def testLoggerFormat(self):
12791277
"[logging] at option format: Bad value substitution: "
12801278
"option 'format' in section 'logging' contains an "
12811279
"interpolation key 'asctime' which is not a valid "
1282-
"option name. Raw value: '%(asctime)s %%(levelname) "
1283-
"%%(message)s'")
1280+
"option name. Raw value: '%(asctime)s %%(trace_id)s "
1281+
"%%(levelname) %%(message)s'")
12841282

12851283
def testDictLoggerConfigViaJson(self):
12861284

0 commit comments

Comments
 (0)