-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_utils.py
More file actions
66 lines (54 loc) · 2.64 KB
/
Copy pathtest_utils.py
File metadata and controls
66 lines (54 loc) · 2.64 KB
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
# Copyright (c) 2026 SUSE LLC. All rights reserved.
#
# This file is part of registration-engine. registration-engine provides an
# api and command line utilities for testing images in the Public Cloud.
#
# 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/>.
"""Unit tests for the utils/logging module."""
import logging
import os
import tempfile
from unittest.mock import patch
from registration_engine.utils import get_logger
def test_get_logger_config():
"""Test get_logger configuration and level changes."""
# Patch home directory expansion so it doesn't write to real home
with tempfile.TemporaryDirectory() as tmpdir:
fake_home = os.path.join(tmpdir, "home")
os.makedirs(fake_home)
log_file = os.path.join(fake_home, "registration_engine.log")
with patch("os.path.expanduser", return_value=log_file):
# Clear handlers from any previous test runs to ensure fully run
logger = logging.getLogger("registration-engine")
logger.handlers.clear()
# 1. Test standard logger
log_instance = get_logger(debug=False)
assert log_instance.level == logging.INFO
assert len(log_instance.handlers) == 2
# 2. Test debug logger update
log_instance_debug = get_logger(debug=True)
assert log_instance_debug.level == logging.DEBUG
# Should not duplicate handler
assert len(log_instance_debug.handlers) == 2
@patch("registration_engine.utils.RotatingFileHandler")
def test_get_logger_unwritable_directory(mock_rotating_handler):
"""Test get_logger handles RotatingFileHandler failures gracefully."""
mock_rotating_handler.side_effect = PermissionError("Permission denied")
logger = logging.getLogger("registration-engine")
logger.handlers.clear()
log_instance = get_logger(debug=False)
assert log_instance.level == logging.INFO
# Only StreamHandler should be present as file_handler creation failed
assert len(log_instance.handlers) == 1
assert isinstance(log_instance.handlers[0], logging.StreamHandler)