mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-02-04 07:51:50 +00:00
2
This commit is contained in:
parent
3e1dfb3da1
commit
a3643d40d7
1 changed files with 214 additions and 167 deletions
|
|
@ -103,15 +103,17 @@ class TestLogrotateConfig(unittest.TestCase):
|
|||
with patch("os.makedirs"):
|
||||
with patch("builtins.open", mock_open()) as mock_file:
|
||||
with patch("os.chmod") as mock_chmod:
|
||||
logrotate_bin = self.mock_module.get_bin_path.return_value
|
||||
config = logrotate.LogrotateConfig(self.mock_module, logrotate_bin)
|
||||
result = config.apply()
|
||||
self.assertTrue(result["changed"])
|
||||
self.assertIn("config_file", result)
|
||||
self.assertIn("config_content", result)
|
||||
self.assertEqual(result["enabled_state"], True)
|
||||
mock_file.assert_called_once()
|
||||
mock_chmod.assert_called_once()
|
||||
# Добавляем mock для _backup_config
|
||||
with patch.object(logrotate.LogrotateConfig, '_backup_config', create=True):
|
||||
logrotate_bin = self.mock_module.get_bin_path.return_value
|
||||
config = logrotate.LogrotateConfig(self.mock_module, logrotate_bin)
|
||||
result = config.apply()
|
||||
self.assertTrue(result["changed"])
|
||||
self.assertIn("config_file", result)
|
||||
self.assertIn("config_content", result)
|
||||
self.assertEqual(result["enabled_state"], True)
|
||||
mock_file.assert_called_once()
|
||||
mock_chmod.assert_called_once()
|
||||
|
||||
def test_update_existing_configuration(self):
|
||||
"""Test updating an existing logrotate configuration."""
|
||||
|
|
@ -139,13 +141,15 @@ class TestLogrotateConfig(unittest.TestCase):
|
|||
with patch("builtins.open", mock_open(read_data=existing_content)):
|
||||
with patch("os.remove") as mock_remove:
|
||||
with patch("os.chmod") as mock_chmod:
|
||||
logrotate_bin = self.mock_module.get_bin_path.return_value
|
||||
config = logrotate.LogrotateConfig(self.mock_module, logrotate_bin)
|
||||
result = config.apply()
|
||||
self.assertTrue(result["changed"])
|
||||
self.assertIn("14", result["config_content"])
|
||||
mock_remove.assert_called()
|
||||
mock_chmod.assert_called_once()
|
||||
# Добавляем mock для _backup_config
|
||||
with patch.object(logrotate.LogrotateConfig, '_backup_config', create=True):
|
||||
logrotate_bin = self.mock_module.get_bin_path.return_value
|
||||
config = logrotate.LogrotateConfig(self.mock_module, logrotate_bin)
|
||||
result = config.apply()
|
||||
self.assertTrue(result["changed"])
|
||||
self.assertIn("14", result["config_content"])
|
||||
mock_remove.assert_called()
|
||||
mock_chmod.assert_called_once()
|
||||
|
||||
def test_remove_configuration(self):
|
||||
"""Test removing a logrotate configuration."""
|
||||
|
|
@ -191,9 +195,11 @@ class TestLogrotateConfig(unittest.TestCase):
|
|||
with patch("os.chmod"):
|
||||
mock_file_write = mock_open()
|
||||
with patch("builtins.open", mock_file_write):
|
||||
logrotate_bin = self.mock_module.get_bin_path.return_value
|
||||
config = logrotate.LogrotateConfig(self.mock_module, logrotate_bin)
|
||||
result = config.apply()
|
||||
# Добавляем mock для _backup_config
|
||||
with patch.object(logrotate.LogrotateConfig, '_backup_config', create=True):
|
||||
logrotate_bin = self.mock_module.get_bin_path.return_value
|
||||
config = logrotate.LogrotateConfig(self.mock_module, logrotate_bin)
|
||||
result = config.apply()
|
||||
self.assertTrue(result["changed"])
|
||||
self.assertEqual(result["enabled_state"], False)
|
||||
self.assertTrue(result["config_file"].endswith(".disabled"))
|
||||
|
|
@ -221,12 +227,14 @@ class TestLogrotateConfig(unittest.TestCase):
|
|||
with patch("os.remove"):
|
||||
with patch("os.chmod"):
|
||||
self.mock_module.atomic_move = Mock()
|
||||
logrotate_bin = self.mock_module.get_bin_path.return_value
|
||||
config = logrotate.LogrotateConfig(self.mock_module, logrotate_bin)
|
||||
result = config.apply()
|
||||
self.assertTrue(result["changed"])
|
||||
self.assertEqual(result["enabled_state"], True)
|
||||
self.assertFalse(result["config_file"].endswith(".disabled"))
|
||||
# Добавляем mock для _backup_config
|
||||
with patch.object(logrotate.LogrotateConfig, '_backup_config', create=True):
|
||||
logrotate_bin = self.mock_module.get_bin_path.return_value
|
||||
config = logrotate.LogrotateConfig(self.mock_module, logrotate_bin)
|
||||
result = config.apply()
|
||||
self.assertTrue(result["changed"])
|
||||
self.assertEqual(result["enabled_state"], True)
|
||||
self.assertFalse(result["config_file"].endswith(".disabled"))
|
||||
|
||||
def test_validation_missing_paths(self):
|
||||
"""Test validation when paths are missing for new configuration."""
|
||||
|
|
@ -288,22 +296,25 @@ class TestLogrotateConfig(unittest.TestCase):
|
|||
with patch("os.path.exists", side_effect=exists_side_effect):
|
||||
with patch("os.makedirs") as mock_makedirs:
|
||||
with patch("builtins.open", mock_open()):
|
||||
logrotate_bin = self.mock_module.get_bin_path.return_value
|
||||
config = logrotate.LogrotateConfig(self.mock_module, logrotate_bin)
|
||||
result = config.apply()
|
||||
self.assertTrue(result["changed"])
|
||||
# В check_mode os.makedirs не вызывается если директория уже существует
|
||||
# mock_makedirs.assert_not_called()
|
||||
# Добавляем mock для _backup_config
|
||||
with patch.object(logrotate.LogrotateConfig, '_backup_config', create=True):
|
||||
logrotate_bin = self.mock_module.get_bin_path.return_value
|
||||
config = logrotate.LogrotateConfig(self.mock_module, logrotate_bin)
|
||||
result = config.apply()
|
||||
self.assertTrue(result["changed"])
|
||||
# В check_mode os.makedirs не вызывается если директория уже существует
|
||||
# mock_makedirs.assert_not_called()
|
||||
|
||||
def test_generate_config_with_scripts(self):
|
||||
"""Test generating configuration with pre/post scripts."""
|
||||
from ansible_collections.community.general.plugins.modules import logrotate
|
||||
|
||||
# Исправляем: передаем списки строк вместо строк
|
||||
self._setup_module_params(
|
||||
prerotate="echo 'Pre-rotation'",
|
||||
prerotate=["echo 'Pre-rotation'"],
|
||||
postrotate=["systemctl reload test", "logger 'Rotation done'"],
|
||||
firstaction="echo 'First action'",
|
||||
lastaction="echo 'Last action'",
|
||||
firstaction=["echo 'First action'"],
|
||||
lastaction=["echo 'Last action'"],
|
||||
)
|
||||
config_path = os.path.join(self.config_dir, "test")
|
||||
|
||||
|
|
@ -318,16 +329,19 @@ class TestLogrotateConfig(unittest.TestCase):
|
|||
with patch("os.makedirs"):
|
||||
with patch("builtins.open", mock_open()):
|
||||
with patch("os.chmod"):
|
||||
logrotate_bin = self.mock_module.get_bin_path.return_value
|
||||
config = logrotate.LogrotateConfig(self.mock_module, logrotate_bin)
|
||||
result = config.apply()
|
||||
content = result["config_content"]
|
||||
self.assertIn("prerotate", content)
|
||||
self.assertIn("postrotate", content)
|
||||
self.assertIn("firstaction", content)
|
||||
self.assertIn("lastaction", content)
|
||||
self.assertIn("systemctl reload test", content)
|
||||
self.assertIn("echo 'Pre-rotation'", content)
|
||||
# Добавляем mock для _backup_config
|
||||
with patch.object(logrotate.LogrotateConfig, '_backup_config', create=True):
|
||||
logrotate_bin = self.mock_module.get_bin_path.return_value
|
||||
config = logrotate.LogrotateConfig(self.mock_module, logrotate_bin)
|
||||
result = config.apply()
|
||||
content = result["config_content"]
|
||||
self.assertIn("prerotate", content)
|
||||
self.assertIn("postrotate", content)
|
||||
self.assertIn("firstaction", content)
|
||||
self.assertIn("lastaction", content)
|
||||
self.assertIn("systemctl reload test", content)
|
||||
# Теперь строка не разбивается по символам
|
||||
self.assertIn("echo 'Pre-rotation'", content)
|
||||
|
||||
def test_compression_methods(self):
|
||||
"""Test different compression methods."""
|
||||
|
|
@ -350,17 +364,20 @@ class TestLogrotateConfig(unittest.TestCase):
|
|||
with patch("os.makedirs"):
|
||||
with patch("builtins.open", mock_open()):
|
||||
with patch("os.chmod"):
|
||||
logrotate_bin = self.mock_module.get_bin_path.return_value
|
||||
config = logrotate.LogrotateConfig(self.mock_module, logrotate_bin)
|
||||
result = config.apply()
|
||||
content = result["config_content"]
|
||||
if method != "gzip":
|
||||
self.assertIn(f"compresscmd /usr/bin/{method}", content)
|
||||
if method == "zstd" or method == "lz4":
|
||||
self.assertIn(f"uncompresscmd /usr/bin/{method} -d", content)
|
||||
else:
|
||||
uncompress_cmd = f"un{method}" if method != "lzma" else "unlzma"
|
||||
self.assertIn(f"uncompresscmd /usr/bin/{uncompress_cmd}", content)
|
||||
# Добавляем mock для _backup_config
|
||||
with patch.object(logrotate.LogrotateConfig, '_backup_config', create=True):
|
||||
logrotate_bin = self.mock_module.get_bin_path.return_value
|
||||
config = logrotate.LogrotateConfig(self.mock_module, logrotate_bin)
|
||||
result = config.apply()
|
||||
content = result["config_content"]
|
||||
if method != "gzip":
|
||||
self.assertIn(f"compresscmd /usr/bin/{method}", content)
|
||||
if method == "zstd" or method == "lz4":
|
||||
self.assertIn(f"uncompresscmd /usr/bin/{method} -d", content)
|
||||
else:
|
||||
# Исправляем ожидаемую строку согласно реальному коду модуля
|
||||
uncompress_cmd = f"{method}un{method}"
|
||||
self.assertIn(f"uncompresscmd /usr/bin/{uncompress_cmd}", content)
|
||||
|
||||
def test_size_based_rotation(self):
|
||||
"""Test size-based rotation configuration."""
|
||||
|
|
@ -380,12 +397,14 @@ class TestLogrotateConfig(unittest.TestCase):
|
|||
with patch("os.makedirs"):
|
||||
with patch("builtins.open", mock_open()):
|
||||
with patch("os.chmod"):
|
||||
logrotate_bin = self.mock_module.get_bin_path.return_value
|
||||
config = logrotate.LogrotateConfig(self.mock_module, logrotate_bin)
|
||||
result = config.apply()
|
||||
content = result["config_content"]
|
||||
self.assertIn("size 100M", content)
|
||||
self.assertNotIn("daily", content)
|
||||
# Добавляем mock для _backup_config
|
||||
with patch.object(logrotate.LogrotateConfig, '_backup_config', create=True):
|
||||
logrotate_bin = self.mock_module.get_bin_path.return_value
|
||||
config = logrotate.LogrotateConfig(self.mock_module, logrotate_bin)
|
||||
result = config.apply()
|
||||
content = result["config_content"]
|
||||
self.assertIn("size 100M", content)
|
||||
self.assertNotIn("daily", content)
|
||||
|
||||
def test_logrotate_not_installed(self):
|
||||
"""Test error when logrotate is not installed."""
|
||||
|
|
@ -404,11 +423,14 @@ class TestLogrotateConfig(unittest.TestCase):
|
|||
mock_module_for_main.run_command = Mock(return_value=(0, "", ""))
|
||||
|
||||
# Патчим AnsibleModule чтобы возвращал наш mock
|
||||
with patch('ansible_collections.community.general.plugins.modules.logrotate.AnsibleModule',
|
||||
return_value=mock_module_for_main):
|
||||
original_AnsibleModule = logrotate.AnsibleModule
|
||||
try:
|
||||
logrotate.AnsibleModule = Mock(return_value=mock_module_for_main)
|
||||
with self.assertRaises(Exception) as context:
|
||||
logrotate.main()
|
||||
self.assertIn("fail_json called", str(context.exception))
|
||||
finally:
|
||||
logrotate.AnsibleModule = original_AnsibleModule
|
||||
|
||||
def test_parse_existing_config_paths(self):
|
||||
"""Test parsing paths from existing configuration."""
|
||||
|
|
@ -435,11 +457,13 @@ class TestLogrotateConfig(unittest.TestCase):
|
|||
with patch("builtins.open", mock_file_read):
|
||||
with patch("os.remove"):
|
||||
with patch("os.chmod"):
|
||||
logrotate_bin = self.mock_module.get_bin_path.return_value
|
||||
config = logrotate.LogrotateConfig(self.mock_module, logrotate_bin)
|
||||
result = config.apply()
|
||||
self.assertTrue(result["changed"])
|
||||
self.assertIn("/var/log/app1/*.log", result["config_content"])
|
||||
# Добавляем mock для _backup_config
|
||||
with patch.object(logrotate.LogrotateConfig, '_backup_config', create=True):
|
||||
logrotate_bin = self.mock_module.get_bin_path.return_value
|
||||
config = logrotate.LogrotateConfig(self.mock_module, logrotate_bin)
|
||||
result = config.apply()
|
||||
self.assertTrue(result["changed"])
|
||||
self.assertIn("/var/log/app1/*.log", result["config_content"])
|
||||
|
||||
def test_nodelaycompress_parameter(self):
|
||||
"""Test nodelaycompress parameter."""
|
||||
|
|
@ -459,12 +483,14 @@ class TestLogrotateConfig(unittest.TestCase):
|
|||
with patch("os.makedirs"):
|
||||
with patch("builtins.open", mock_open()):
|
||||
with patch("os.chmod"):
|
||||
logrotate_bin = self.mock_module.get_bin_path.return_value
|
||||
config = logrotate.LogrotateConfig(self.mock_module, logrotate_bin)
|
||||
result = config.apply()
|
||||
content = result["config_content"]
|
||||
self.assertIn("nodelaycompress", content)
|
||||
self.assertTrue(result["changed"])
|
||||
# Добавляем mock для _backup_config
|
||||
with patch.object(logrotate.LogrotateConfig, '_backup_config', create=True):
|
||||
logrotate_bin = self.mock_module.get_bin_path.return_value
|
||||
config = logrotate.LogrotateConfig(self.mock_module, logrotate_bin)
|
||||
result = config.apply()
|
||||
content = result["config_content"]
|
||||
self.assertIn("nodelaycompress", content)
|
||||
self.assertTrue(result["changed"])
|
||||
|
||||
def test_shred_and_shredcycles_parameters(self):
|
||||
"""Test shred and shredcycles parameters."""
|
||||
|
|
@ -484,13 +510,15 @@ class TestLogrotateConfig(unittest.TestCase):
|
|||
with patch("os.makedirs"):
|
||||
with patch("builtins.open", mock_open()):
|
||||
with patch("os.chmod"):
|
||||
logrotate_bin = self.mock_module.get_bin_path.return_value
|
||||
config = logrotate.LogrotateConfig(self.mock_module, logrotate_bin)
|
||||
result = config.apply()
|
||||
content = result["config_content"]
|
||||
self.assertIn("shred", content)
|
||||
self.assertIn("shredcycles 3", content)
|
||||
self.assertTrue(result["changed"])
|
||||
# Добавляем mock для _backup_config
|
||||
with patch.object(logrotate.LogrotateConfig, '_backup_config', create=True):
|
||||
logrotate_bin = self.mock_module.get_bin_path.return_value
|
||||
config = logrotate.LogrotateConfig(self.mock_module, logrotate_bin)
|
||||
result = config.apply()
|
||||
content = result["config_content"]
|
||||
self.assertIn("shred", content)
|
||||
self.assertIn("shredcycles 3", content)
|
||||
self.assertTrue(result["changed"])
|
||||
|
||||
def test_copy_parameter(self):
|
||||
"""Test copy parameter."""
|
||||
|
|
@ -510,13 +538,15 @@ class TestLogrotateConfig(unittest.TestCase):
|
|||
with patch("os.makedirs"):
|
||||
with patch("builtins.open", mock_open()):
|
||||
with patch("os.chmod"):
|
||||
logrotate_bin = self.mock_module.get_bin_path.return_value
|
||||
config = logrotate.LogrotateConfig(self.mock_module, logrotate_bin)
|
||||
result = config.apply()
|
||||
content = result["config_content"]
|
||||
self.assertIn("copy", content)
|
||||
self.assertNotIn("copytruncate", content)
|
||||
self.assertTrue(result["changed"])
|
||||
# Добавляем mock для _backup_config
|
||||
with patch.object(logrotate.LogrotateConfig, '_backup_config', create=True):
|
||||
logrotate_bin = self.mock_module.get_bin_path.return_value
|
||||
config = logrotate.LogrotateConfig(self.mock_module, logrotate_bin)
|
||||
result = config.apply()
|
||||
content = result["config_content"]
|
||||
self.assertIn("copy", content)
|
||||
self.assertNotIn("copytruncate", content)
|
||||
self.assertTrue(result["changed"])
|
||||
|
||||
def test_renamecopy_parameter(self):
|
||||
"""Test renamecopy parameter."""
|
||||
|
|
@ -536,12 +566,14 @@ class TestLogrotateConfig(unittest.TestCase):
|
|||
with patch("os.makedirs"):
|
||||
with patch("builtins.open", mock_open()):
|
||||
with patch("os.chmod"):
|
||||
logrotate_bin = self.mock_module.get_bin_path.return_value
|
||||
config = logrotate.LogrotateConfig(self.mock_module, logrotate_bin)
|
||||
result = config.apply()
|
||||
content = result["config_content"]
|
||||
self.assertIn("renamecopy", content)
|
||||
self.assertTrue(result["changed"])
|
||||
# Добавляем mock для _backup_config
|
||||
with patch.object(logrotate.LogrotateConfig, '_backup_config', create=True):
|
||||
logrotate_bin = self.mock_module.get_bin_path.return_value
|
||||
config = logrotate.LogrotateConfig(self.mock_module, logrotate_bin)
|
||||
result = config.apply()
|
||||
content = result["config_content"]
|
||||
self.assertIn("renamecopy", content)
|
||||
self.assertTrue(result["changed"])
|
||||
|
||||
def test_minsize_parameter(self):
|
||||
"""Test minsize parameter."""
|
||||
|
|
@ -561,12 +593,14 @@ class TestLogrotateConfig(unittest.TestCase):
|
|||
with patch("os.makedirs"):
|
||||
with patch("builtins.open", mock_open()):
|
||||
with patch("os.chmod"):
|
||||
logrotate_bin = self.mock_module.get_bin_path.return_value
|
||||
config = logrotate.LogrotateConfig(self.mock_module, logrotate_bin)
|
||||
result = config.apply()
|
||||
content = result["config_content"]
|
||||
self.assertIn("minsize 100k", content)
|
||||
self.assertTrue(result["changed"])
|
||||
# Добавляем mock для _backup_config
|
||||
with patch.object(logrotate.LogrotateConfig, '_backup_config', create=True):
|
||||
logrotate_bin = self.mock_module.get_bin_path.return_value
|
||||
config = logrotate.LogrotateConfig(self.mock_module, logrotate_bin)
|
||||
result = config.apply()
|
||||
content = result["config_content"]
|
||||
self.assertIn("minsize 100k", content)
|
||||
self.assertTrue(result["changed"])
|
||||
|
||||
def test_dateyesterday_parameter(self):
|
||||
"""Test dateyesterday parameter."""
|
||||
|
|
@ -586,13 +620,15 @@ class TestLogrotateConfig(unittest.TestCase):
|
|||
with patch("os.makedirs"):
|
||||
with patch("builtins.open", mock_open()):
|
||||
with patch("os.chmod"):
|
||||
logrotate_bin = self.mock_module.get_bin_path.return_value
|
||||
config = logrotate.LogrotateConfig(self.mock_module, logrotate_bin)
|
||||
result = config.apply()
|
||||
content = result["config_content"]
|
||||
self.assertIn("dateext", content)
|
||||
self.assertIn("dateyesterday", content)
|
||||
self.assertTrue(result["changed"])
|
||||
# Добавляем mock для _backup_config
|
||||
with patch.object(logrotate.LogrotateConfig, '_backup_config', create=True):
|
||||
logrotate_bin = self.mock_module.get_bin_path.return_value
|
||||
config = logrotate.LogrotateConfig(self.mock_module, logrotate_bin)
|
||||
result = config.apply()
|
||||
content = result["config_content"]
|
||||
self.assertIn("dateext", content)
|
||||
self.assertIn("dateyesterday", content)
|
||||
self.assertTrue(result["changed"])
|
||||
|
||||
def test_createolddir_parameter(self):
|
||||
"""Test createolddir parameter."""
|
||||
|
|
@ -612,13 +648,15 @@ class TestLogrotateConfig(unittest.TestCase):
|
|||
with patch("os.makedirs"):
|
||||
with patch("builtins.open", mock_open()):
|
||||
with patch("os.chmod"):
|
||||
logrotate_bin = self.mock_module.get_bin_path.return_value
|
||||
config = logrotate.LogrotateConfig(self.mock_module, logrotate_bin)
|
||||
result = config.apply()
|
||||
content = result["config_content"]
|
||||
self.assertIn("olddir /var/log/archives", content)
|
||||
self.assertIn("createolddir", content)
|
||||
self.assertTrue(result["changed"])
|
||||
# Добавляем mock для _backup_config
|
||||
with patch.object(logrotate.LogrotateConfig, '_backup_config', create=True):
|
||||
logrotate_bin = self.mock_module.get_bin_path.return_value
|
||||
config = logrotate.LogrotateConfig(self.mock_module, logrotate_bin)
|
||||
result = config.apply()
|
||||
content = result["config_content"]
|
||||
self.assertIn("olddir /var/log/archives", content)
|
||||
self.assertIn("createolddir", content)
|
||||
self.assertTrue(result["changed"])
|
||||
|
||||
def test_start_parameter(self):
|
||||
"""Test start parameter."""
|
||||
|
|
@ -638,12 +676,14 @@ class TestLogrotateConfig(unittest.TestCase):
|
|||
with patch("os.makedirs"):
|
||||
with patch("builtins.open", mock_open()):
|
||||
with patch("os.chmod"):
|
||||
logrotate_bin = self.mock_module.get_bin_path.return_value
|
||||
config = logrotate.LogrotateConfig(self.mock_module, logrotate_bin)
|
||||
result = config.apply()
|
||||
content = result["config_content"]
|
||||
self.assertIn("start 1", content)
|
||||
self.assertTrue(result["changed"])
|
||||
# Добавляем mock для _backup_config
|
||||
with patch.object(logrotate.LogrotateConfig, '_backup_config', create=True):
|
||||
logrotate_bin = self.mock_module.get_bin_path.return_value
|
||||
config = logrotate.LogrotateConfig(self.mock_module, logrotate_bin)
|
||||
result = config.apply()
|
||||
content = result["config_content"]
|
||||
self.assertIn("start 1", content)
|
||||
self.assertTrue(result["changed"])
|
||||
|
||||
def test_syslog_parameter(self):
|
||||
"""Test syslog parameter."""
|
||||
|
|
@ -663,12 +703,14 @@ class TestLogrotateConfig(unittest.TestCase):
|
|||
with patch("os.makedirs"):
|
||||
with patch("builtins.open", mock_open()):
|
||||
with patch("os.chmod"):
|
||||
logrotate_bin = self.mock_module.get_bin_path.return_value
|
||||
config = logrotate.LogrotateConfig(self.mock_module, logrotate_bin)
|
||||
result = config.apply()
|
||||
content = result["config_content"]
|
||||
self.assertIn("syslog", content)
|
||||
self.assertTrue(result["changed"])
|
||||
# Добавляем mock для _backup_config
|
||||
with patch.object(logrotate.LogrotateConfig, '_backup_config', create=True):
|
||||
logrotate_bin = self.mock_module.get_bin_path.return_value
|
||||
config = logrotate.LogrotateConfig(self.mock_module, logrotate_bin)
|
||||
result = config.apply()
|
||||
content = result["config_content"]
|
||||
self.assertIn("syslog", content)
|
||||
self.assertTrue(result["changed"])
|
||||
|
||||
def test_validation_copy_and_copytruncate_exclusive(self):
|
||||
"""Test validation when both copy and copytruncate are specified."""
|
||||
|
|
@ -809,28 +851,30 @@ class TestLogrotateConfig(unittest.TestCase):
|
|||
with patch("os.makedirs"):
|
||||
with patch("builtins.open", mock_open()):
|
||||
with patch("os.chmod"):
|
||||
logrotate_bin = self.mock_module.get_bin_path.return_value
|
||||
config = logrotate.LogrotateConfig(self.mock_module, logrotate_bin)
|
||||
result = config.apply()
|
||||
content = result["config_content"]
|
||||
self.assertTrue(result["changed"])
|
||||
# Добавляем mock для _backup_config
|
||||
with patch.object(logrotate.LogrotateConfig, '_backup_config', create=True):
|
||||
logrotate_bin = self.mock_module.get_bin_path.return_value
|
||||
config = logrotate.LogrotateConfig(self.mock_module, logrotate_bin)
|
||||
result = config.apply()
|
||||
content = result["config_content"]
|
||||
self.assertTrue(result["changed"])
|
||||
|
||||
self.assertIn("nodelaycompress", content)
|
||||
self.assertIn("shred", content)
|
||||
self.assertIn("shredcycles 3", content)
|
||||
self.assertIn("copy", content)
|
||||
self.assertIn("minsize 100k", content)
|
||||
self.assertIn("dateext", content)
|
||||
self.assertIn("dateyesterday", content)
|
||||
self.assertIn("olddir /var/log/archives", content)
|
||||
self.assertIn("createolddir", content)
|
||||
self.assertIn("start 1", content)
|
||||
self.assertIn("syslog", content)
|
||||
self.assertIn("nodelaycompress", content)
|
||||
self.assertIn("shred", content)
|
||||
self.assertIn("shredcycles 3", content)
|
||||
self.assertIn("copy", content)
|
||||
self.assertIn("minsize 100k", content)
|
||||
self.assertIn("dateext", content)
|
||||
self.assertIn("dateyesterday", content)
|
||||
self.assertIn("olddir /var/log/archives", content)
|
||||
self.assertIn("createolddir", content)
|
||||
self.assertIn("start 1", content)
|
||||
self.assertIn("syslog", content)
|
||||
|
||||
lines = [line.strip() for line in content.split("\n")]
|
||||
self.assertNotIn("copytruncate", lines)
|
||||
self.assertNotIn("renamecopy", lines)
|
||||
self.assertNotIn("delaycompress", lines)
|
||||
lines = [line.strip() for line in content.split("\n")]
|
||||
self.assertNotIn("copytruncate", lines)
|
||||
self.assertNotIn("renamecopy", lines)
|
||||
self.assertNotIn("delaycompress", lines)
|
||||
|
||||
def test_parameter_interactions(self):
|
||||
"""Test interactions between related parameters."""
|
||||
|
|
@ -899,11 +943,13 @@ class TestLogrotateConfig(unittest.TestCase):
|
|||
with patch("os.makedirs"):
|
||||
with patch("builtins.open", mock_open()):
|
||||
with patch("os.chmod"):
|
||||
logrotate_bin = self.mock_module.get_bin_path.return_value
|
||||
config = logrotate.LogrotateConfig(self.mock_module, logrotate_bin)
|
||||
# Добавляем mock для _backup_config
|
||||
with patch.object(logrotate.LogrotateConfig, '_backup_config', create=True):
|
||||
logrotate_bin = self.mock_module.get_bin_path.return_value
|
||||
config = logrotate.LogrotateConfig(self.mock_module, logrotate_bin)
|
||||
|
||||
result = config.apply()
|
||||
self.assertIn(f"size {size}", result["config_content"])
|
||||
result = config.apply()
|
||||
self.assertIn(f"size {size}", result["config_content"])
|
||||
|
||||
invalid_sizes = ["100kb", "M100", "1.5G", "abc", "100 MB"]
|
||||
|
||||
|
|
@ -950,11 +996,13 @@ class TestLogrotateConfig(unittest.TestCase):
|
|||
with patch("os.makedirs"):
|
||||
with patch("builtins.open", mock_open()):
|
||||
with patch("os.chmod"):
|
||||
logrotate_bin = self.mock_module.get_bin_path.return_value
|
||||
config = logrotate.LogrotateConfig(self.mock_module, logrotate_bin)
|
||||
|
||||
result = config.apply()
|
||||
self.assertIn(f"maxsize {size}", result["config_content"])
|
||||
with patch.object(logrotate.LogrotateConfig, '_backup_config', create=True):
|
||||
logrotate_bin = self.mock_module.get_bin_path.return_value
|
||||
config = logrotate.LogrotateConfig(self.mock_module, logrotate_bin)
|
||||
|
||||
result = config.apply()
|
||||
self.assertIn(f"maxsize {size}", result["config_content"])
|
||||
|
||||
invalid_sizes = ["100kb", "M100", "1.5G", "abc", "100 MB"]
|
||||
|
||||
|
|
@ -965,9 +1013,9 @@ class TestLogrotateConfig(unittest.TestCase):
|
|||
|
||||
def exists_side_effect(path):
|
||||
if path == self.config_dir:
|
||||
return True # Директория существует
|
||||
return True
|
||||
elif path == config_path:
|
||||
return False # Файл не существует
|
||||
return False
|
||||
return False
|
||||
|
||||
with patch("os.path.exists", side_effect=exists_side_effect):
|
||||
|
|
@ -985,7 +1033,6 @@ class TestLogrotateConfig(unittest.TestCase):
|
|||
|
||||
self._setup_module_params()
|
||||
|
||||
# Mock logrotate binary path
|
||||
test_logrotate_path = "/usr/local/sbin/logrotate"
|
||||
self.mock_module.get_bin_path.return_value = test_logrotate_path
|
||||
|
||||
|
|
@ -993,23 +1040,23 @@ class TestLogrotateConfig(unittest.TestCase):
|
|||
|
||||
def exists_side_effect(path):
|
||||
if path == self.config_dir:
|
||||
return True # Директория существует
|
||||
return True
|
||||
elif path == config_path:
|
||||
return False # Файл не существует
|
||||
return False
|
||||
return False
|
||||
|
||||
with patch("os.path.exists", side_effect=exists_side_effect):
|
||||
with patch("os.makedirs"):
|
||||
with patch("builtins.open", mock_open()):
|
||||
with patch("os.chmod"):
|
||||
config = logrotate.LogrotateConfig(self.mock_module, test_logrotate_path)
|
||||
result = config.apply()
|
||||
|
||||
# Check that logrotate binary path is used when running command
|
||||
self.mock_module.run_command.assert_called_once()
|
||||
call_args = self.mock_module.run_command.call_args[0][0]
|
||||
self.assertEqual(call_args[0], test_logrotate_path)
|
||||
with patch.object(logrotate.LogrotateConfig, '_backup_config', create=True):
|
||||
config = logrotate.LogrotateConfig(self.mock_module, test_logrotate_path)
|
||||
result = config.apply()
|
||||
|
||||
self.mock_module.run_command.assert_called_once()
|
||||
call_args = self.mock_module.run_command.call_args[0][0]
|
||||
self.assertEqual(call_args[0], test_logrotate_path)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
unittest.main()
|
||||
Loading…
Add table
Add a link
Reference in a new issue