diff --git a/tests/unit/plugins/modules/test_logrotate.py b/tests/unit/plugins/modules/test_logrotate.py index 5818d690e8..b9b3a5461a 100644 --- a/tests/unit/plugins/modules/test_logrotate.py +++ b/tests/unit/plugins/modules/test_logrotate.py @@ -103,7 +103,7 @@ class TestLogrotateConfig(unittest.TestCase): with patch("os.makedirs"): with patch("builtins.open", mock_open()) as mock_file: with patch("os.chmod") as mock_chmod: - with patch.object(logrotate.LogrotateConfig, '_backup_config', create=True): + 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() @@ -140,7 +140,7 @@ 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: - with patch.object(logrotate.LogrotateConfig, '_backup_config', create=True): + 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() @@ -193,7 +193,7 @@ class TestLogrotateConfig(unittest.TestCase): with patch("os.chmod"): mock_file_write = mock_open() with patch("builtins.open", mock_file_write): - with patch.object(logrotate.LogrotateConfig, '_backup_config', create=True): + 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() @@ -224,7 +224,7 @@ class TestLogrotateConfig(unittest.TestCase): with patch("os.remove"): with patch("os.chmod"): self.mock_module.atomic_move = Mock() - with patch.object(logrotate.LogrotateConfig, '_backup_config', create=True): + 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() @@ -290,9 +290,9 @@ class TestLogrotateConfig(unittest.TestCase): return False with patch("os.path.exists", side_effect=exists_side_effect): - with patch("os.makedirs") as mock_makedirs: + with patch("os.makedirs"): with patch("builtins.open", mock_open()): - with patch.object(logrotate.LogrotateConfig, '_backup_config', create=True): + 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() @@ -321,7 +321,7 @@ class TestLogrotateConfig(unittest.TestCase): with patch("os.makedirs"): with patch("builtins.open", mock_open()): with patch("os.chmod"): - with patch.object(logrotate.LogrotateConfig, '_backup_config', create=True): + 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() @@ -343,10 +343,10 @@ class TestLogrotateConfig(unittest.TestCase): self._setup_module_params(compression_method=method) config_path = os.path.join(self.config_dir, "test") - def exists_side_effect(path): + def exists_side_effect(path, current_config_path=config_path): if path == self.config_dir: return True - elif path == config_path: + elif path == current_config_path: return False return False @@ -354,7 +354,7 @@ class TestLogrotateConfig(unittest.TestCase): with patch("os.makedirs"): with patch("builtins.open", mock_open()): with patch("os.chmod"): - with patch.object(logrotate.LogrotateConfig, '_backup_config', create=True): + 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() @@ -385,7 +385,7 @@ class TestLogrotateConfig(unittest.TestCase): with patch("os.makedirs"): with patch("builtins.open", mock_open()): with patch("os.chmod"): - with patch.object(logrotate.LogrotateConfig, '_backup_config', create=True): + 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() @@ -430,8 +430,10 @@ class TestLogrotateConfig(unittest.TestCase): mock_module_for_test.warn = Mock() mock_module_for_test.run_command = Mock(return_value=(0, "", "")) - with patch('ansible_collections.community.general.plugins.modules.logrotate.AnsibleModule', - return_value=mock_module_for_test): + with patch( + "ansible_collections.community.general.plugins.modules.logrotate.AnsibleModule", + return_value=mock_module_for_test, + ): with self.assertRaises(Exception) as context: logrotate.main() @@ -465,7 +467,7 @@ class TestLogrotateConfig(unittest.TestCase): with patch("builtins.open", mock_file_read): with patch("os.remove"): with patch("os.chmod"): - with patch.object(logrotate.LogrotateConfig, '_backup_config', create=True): + 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() @@ -490,7 +492,7 @@ class TestLogrotateConfig(unittest.TestCase): with patch("os.makedirs"): with patch("builtins.open", mock_open()): with patch("os.chmod"): - with patch.object(logrotate.LogrotateConfig, '_backup_config', create=True): + 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() @@ -516,7 +518,7 @@ class TestLogrotateConfig(unittest.TestCase): with patch("os.makedirs"): with patch("builtins.open", mock_open()): with patch("os.chmod"): - with patch.object(logrotate.LogrotateConfig, '_backup_config', create=True): + 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() @@ -543,7 +545,7 @@ class TestLogrotateConfig(unittest.TestCase): with patch("os.makedirs"): with patch("builtins.open", mock_open()): with patch("os.chmod"): - with patch.object(logrotate.LogrotateConfig, '_backup_config', create=True): + 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() @@ -570,7 +572,7 @@ class TestLogrotateConfig(unittest.TestCase): with patch("os.makedirs"): with patch("builtins.open", mock_open()): with patch("os.chmod"): - with patch.object(logrotate.LogrotateConfig, '_backup_config', create=True): + 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() @@ -596,7 +598,7 @@ class TestLogrotateConfig(unittest.TestCase): with patch("os.makedirs"): with patch("builtins.open", mock_open()): with patch("os.chmod"): - with patch.object(logrotate.LogrotateConfig, '_backup_config', create=True): + 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() @@ -622,7 +624,7 @@ class TestLogrotateConfig(unittest.TestCase): with patch("os.makedirs"): with patch("builtins.open", mock_open()): with patch("os.chmod"): - with patch.object(logrotate.LogrotateConfig, '_backup_config', create=True): + 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() @@ -649,7 +651,7 @@ class TestLogrotateConfig(unittest.TestCase): with patch("os.makedirs"): with patch("builtins.open", mock_open()): with patch("os.chmod"): - with patch.object(logrotate.LogrotateConfig, '_backup_config', create=True): + 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() @@ -676,7 +678,7 @@ class TestLogrotateConfig(unittest.TestCase): with patch("os.makedirs"): with patch("builtins.open", mock_open()): with patch("os.chmod"): - with patch.object(logrotate.LogrotateConfig, '_backup_config', create=True): + 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() @@ -702,7 +704,7 @@ class TestLogrotateConfig(unittest.TestCase): with patch("os.makedirs"): with patch("builtins.open", mock_open()): with patch("os.chmod"): - with patch.object(logrotate.LogrotateConfig, '_backup_config', create=True): + 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() @@ -849,7 +851,7 @@ class TestLogrotateConfig(unittest.TestCase): with patch("os.makedirs"): with patch("builtins.open", mock_open()): with patch("os.chmod"): - with patch.object(logrotate.LogrotateConfig, '_backup_config', create=True): + 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() @@ -929,10 +931,10 @@ class TestLogrotateConfig(unittest.TestCase): self._setup_module_params(size=size) config_path = os.path.join(self.config_dir, "test") - def exists_side_effect(path): + def exists_side_effect(path, current_config_path=config_path): if path == self.config_dir: return True - elif path == config_path: + elif path == current_config_path: return False return False @@ -940,7 +942,7 @@ class TestLogrotateConfig(unittest.TestCase): with patch("os.makedirs"): with patch("builtins.open", mock_open()): with patch("os.chmod"): - with patch.object(logrotate.LogrotateConfig, '_backup_config', create=True): + 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) @@ -981,10 +983,10 @@ class TestLogrotateConfig(unittest.TestCase): self._setup_module_params(maxsize=size) config_path = os.path.join(self.config_dir, "test") - def exists_side_effect(path): + def exists_side_effect(path, current_config_path=config_path): if path == self.config_dir: return True - elif path == config_path: + elif path == current_config_path: return False return False @@ -992,7 +994,7 @@ class TestLogrotateConfig(unittest.TestCase): with patch("os.makedirs"): with patch("builtins.open", mock_open()): with patch("os.chmod"): - with patch.object(logrotate.LogrotateConfig, '_backup_config', create=True): + 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) @@ -1044,9 +1046,9 @@ class TestLogrotateConfig(unittest.TestCase): with patch("os.makedirs"): with patch("builtins.open", mock_open()): with patch("os.chmod"): - with patch.object(logrotate.LogrotateConfig, '_backup_config', create=True): + with patch.object(logrotate.LogrotateConfig, "_backup_config", create=True): config = logrotate.LogrotateConfig(self.mock_module, test_logrotate_path) - result = config.apply() + config.apply() # Изменено: результат не сохраняется self.mock_module.run_command.assert_called_once() call_args = self.mock_module.run_command.call_args[0][0]