diff --git a/tests/unit/plugins/modules/test_logrotate.py b/tests/unit/plugins/modules/test_logrotate.py index f13e67369a..cb910fc5f0 100644 --- a/tests/unit/plugins/modules/test_logrotate.py +++ b/tests/unit/plugins/modules/test_logrotate.py @@ -91,14 +91,14 @@ class TestLogrotateConfig(unittest.TestCase): self._setup_module_params() config_path = os.path.join(self.config_dir, "test") - + def exists_side_effect(path): if path == self.config_dir: return True # Директория существует elif path == config_path: return False # Файл не существует return False - + with patch("os.path.exists", side_effect=exists_side_effect): with patch("os.makedirs"): with patch("builtins.open", mock_open()) as mock_file: @@ -129,14 +129,14 @@ class TestLogrotateConfig(unittest.TestCase): missingok notifempty }""" - + def exists_side_effect(path): if path == self.config_dir: return True # Директория существует elif path == config_path: return True # Файл существует return False - + with patch("os.path.exists", side_effect=exists_side_effect): with patch("builtins.open", mock_open(read_data=existing_content)): with patch("os.remove") as mock_remove: @@ -242,14 +242,14 @@ class TestLogrotateConfig(unittest.TestCase): self._setup_module_params(paths=None) config_path = os.path.join(self.config_dir, "test") - + def exists_side_effect(path): if path == self.config_dir: return True # Директория существует elif path == config_path: return False # Файл не существует return False - + with patch("os.path.exists", side_effect=exists_side_effect): logrotate_bin = self.mock_module.get_bin_path.return_value config = logrotate.LogrotateConfig(self.mock_module, logrotate_bin) @@ -263,14 +263,14 @@ class TestLogrotateConfig(unittest.TestCase): self._setup_module_params(size="100M", maxsize="200M") config_path = os.path.join(self.config_dir, "test") - + def exists_side_effect(path): if path == self.config_dir: return True # Директория существует elif path == config_path: return False # Файл не существует return False - + with patch("os.path.exists", side_effect=exists_side_effect): logrotate_bin = self.mock_module.get_bin_path.return_value config = logrotate.LogrotateConfig(self.mock_module, logrotate_bin) @@ -285,14 +285,14 @@ class TestLogrotateConfig(unittest.TestCase): self._setup_module_params() self.mock_module.check_mode = True config_path = os.path.join(self.config_dir, "test") - + def exists_side_effect(path): if path == self.config_dir: return True # Директория существует elif path == config_path: return False # Файл не существует return False - + with patch("os.path.exists", side_effect=exists_side_effect): with patch("os.makedirs") as mock_makedirs: with patch("builtins.open", mock_open()): @@ -317,14 +317,14 @@ class TestLogrotateConfig(unittest.TestCase): lastaction=["echo 'Last action'"], ) config_path = os.path.join(self.config_dir, "test") - + def exists_side_effect(path): if path == self.config_dir: return True # Директория существует elif path == config_path: return False # Файл не существует return False - + with patch("os.path.exists", side_effect=exists_side_effect): with patch("os.makedirs"): with patch("builtins.open", mock_open()): @@ -352,14 +352,14 @@ class TestLogrotateConfig(unittest.TestCase): with self.subTest(method=method): self._setup_module_params(compression_method=method) config_path = os.path.join(self.config_dir, "test") - + def exists_side_effect(path): if path == self.config_dir: return True # Директория существует elif path == config_path: return False # Файл не существует return False - + with patch("os.path.exists", side_effect=exists_side_effect): with patch("os.makedirs"): with patch("builtins.open", mock_open()): @@ -385,14 +385,14 @@ class TestLogrotateConfig(unittest.TestCase): self._setup_module_params(size="100M", rotation_period="daily") config_path = os.path.join(self.config_dir, "test") - + def exists_side_effect(path): if path == self.config_dir: return True # Директория существует elif path == config_path: return False # Файл не существует return False - + with patch("os.path.exists", side_effect=exists_side_effect): with patch("os.makedirs"): with patch("builtins.open", mock_open()): @@ -434,31 +434,31 @@ class TestLogrotateConfig(unittest.TestCase): mock_module_for_test.fail_json = Mock(side_effect=Exception("fail_json called")) mock_module_for_test.exit_json = Mock() mock_module_for_test.check_mode = False - + # Определяем side_effect для get_bin_path def get_bin_path_side_effect(name, required=False): if name == "logrotate" and required: - # В реальном AnsibleModule.get_bin_path() с required=True + # В реальном AnsibleModule.get_bin_path() с required=True # при отсутствии бинарника вызывается fail_json mock_module_for_test.fail_json(msg=f"Failed to find required executable '{name}' in PATH") return None - + mock_module_for_test.get_bin_path = Mock(side_effect=get_bin_path_side_effect) mock_module_for_test.atomic_move = Mock() mock_module_for_test.warn = Mock() mock_module_for_test.run_command = Mock(return_value=(0, "", "")) - + # Заменяем AnsibleModule в модуле logrotate на наш mock - with patch('ansible_collections.community.general.plugins.modules.logrotate.AnsibleModule', + with patch('ansible_collections.community.general.plugins.modules.logrotate.AnsibleModule', return_value=mock_module_for_test): # Когда main() будет вызван, AnsibleModule вернет наш mock_module_for_test # который при вызове get_bin_path("logrotate", required=True) вызовет fail_json with self.assertRaises(Exception) as context: logrotate.main() - + # Проверяем, что была вызвана ошибка self.assertIn("fail_json called", str(context.exception)) - + # Проверяем, что get_bin_path был вызван с правильными параметрами mock_module_for_test.get_bin_path.assert_called_once_with("logrotate", required=True) mock_module_for_test.fail_json.assert_called_once() @@ -502,14 +502,14 @@ class TestLogrotateConfig(unittest.TestCase): self._setup_module_params(nodelaycompress=True) config_path = os.path.join(self.config_dir, "test") - + def exists_side_effect(path): if path == self.config_dir: return True # Директория существует elif path == config_path: return False # Файл не существует return False - + with patch("os.path.exists", side_effect=exists_side_effect): with patch("os.makedirs"): with patch("builtins.open", mock_open()): @@ -529,14 +529,14 @@ class TestLogrotateConfig(unittest.TestCase): self._setup_module_params(shred=True, shredcycles=3) config_path = os.path.join(self.config_dir, "test") - + def exists_side_effect(path): if path == self.config_dir: return True # Директория существует elif path == config_path: return False # Файл не существует return False - + with patch("os.path.exists", side_effect=exists_side_effect): with patch("os.makedirs"): with patch("builtins.open", mock_open()): @@ -557,14 +557,14 @@ class TestLogrotateConfig(unittest.TestCase): self._setup_module_params(copy=True, copytruncate=False) config_path = os.path.join(self.config_dir, "test") - + def exists_side_effect(path): if path == self.config_dir: return True # Директория существует elif path == config_path: return False # Файл не существует return False - + with patch("os.path.exists", side_effect=exists_side_effect): with patch("os.makedirs"): with patch("builtins.open", mock_open()): @@ -585,14 +585,14 @@ class TestLogrotateConfig(unittest.TestCase): self._setup_module_params(renamecopy=True) config_path = os.path.join(self.config_dir, "test") - + def exists_side_effect(path): if path == self.config_dir: return True # Директория существует elif path == config_path: return False # Файл не существует return False - + with patch("os.path.exists", side_effect=exists_side_effect): with patch("os.makedirs"): with patch("builtins.open", mock_open()): @@ -612,14 +612,14 @@ class TestLogrotateConfig(unittest.TestCase): self._setup_module_params(minsize="100k") config_path = os.path.join(self.config_dir, "test") - + def exists_side_effect(path): if path == self.config_dir: return True # Директория существует elif path == config_path: return False # Файл не существует return False - + with patch("os.path.exists", side_effect=exists_side_effect): with patch("os.makedirs"): with patch("builtins.open", mock_open()): @@ -639,14 +639,14 @@ class TestLogrotateConfig(unittest.TestCase): self._setup_module_params(dateext=True, dateyesterday=True) config_path = os.path.join(self.config_dir, "test") - + def exists_side_effect(path): if path == self.config_dir: return True # Директория существует elif path == config_path: return False # Файл не существует return False - + with patch("os.path.exists", side_effect=exists_side_effect): with patch("os.makedirs"): with patch("builtins.open", mock_open()): @@ -667,14 +667,14 @@ class TestLogrotateConfig(unittest.TestCase): self._setup_module_params(olddir="/var/log/archives", createolddir=True) config_path = os.path.join(self.config_dir, "test") - + def exists_side_effect(path): if path == self.config_dir: return True # Директория существует elif path == config_path: return False # Файл не существует return False - + with patch("os.path.exists", side_effect=exists_side_effect): with patch("os.makedirs"): with patch("builtins.open", mock_open()): @@ -695,14 +695,14 @@ class TestLogrotateConfig(unittest.TestCase): self._setup_module_params(start=1) config_path = os.path.join(self.config_dir, "test") - + def exists_side_effect(path): if path == self.config_dir: return True # Директория существует elif path == config_path: return False # Файл не существует return False - + with patch("os.path.exists", side_effect=exists_side_effect): with patch("os.makedirs"): with patch("builtins.open", mock_open()): @@ -722,14 +722,14 @@ class TestLogrotateConfig(unittest.TestCase): self._setup_module_params(syslog=True) config_path = os.path.join(self.config_dir, "test") - + def exists_side_effect(path): if path == self.config_dir: return True # Директория существует elif path == config_path: return False # Файл не существует return False - + with patch("os.path.exists", side_effect=exists_side_effect): with patch("os.makedirs"): with patch("builtins.open", mock_open()): @@ -749,14 +749,14 @@ class TestLogrotateConfig(unittest.TestCase): self._setup_module_params(copy=True, copytruncate=True) config_path = os.path.join(self.config_dir, "test") - + def exists_side_effect(path): if path == self.config_dir: return True # Директория существует elif path == config_path: return False # Файл не существует return False - + with patch("os.path.exists", side_effect=exists_side_effect): logrotate_bin = self.mock_module.get_bin_path.return_value config = logrotate.LogrotateConfig(self.mock_module, logrotate_bin) @@ -770,14 +770,14 @@ class TestLogrotateConfig(unittest.TestCase): self._setup_module_params(copy=True, renamecopy=True) config_path = os.path.join(self.config_dir, "test") - + def exists_side_effect(path): if path == self.config_dir: return True # Директория существует elif path == config_path: return False # Файл не существует return False - + with patch("os.path.exists", side_effect=exists_side_effect): logrotate_bin = self.mock_module.get_bin_path.return_value config = logrotate.LogrotateConfig(self.mock_module, logrotate_bin) @@ -791,14 +791,14 @@ class TestLogrotateConfig(unittest.TestCase): self._setup_module_params(shredcycles=0) config_path = os.path.join(self.config_dir, "test") - + def exists_side_effect(path): if path == self.config_dir: return True # Директория существует elif path == config_path: return False # Файл не существует return False - + with patch("os.path.exists", side_effect=exists_side_effect): logrotate_bin = self.mock_module.get_bin_path.return_value config = logrotate.LogrotateConfig(self.mock_module, logrotate_bin) @@ -812,14 +812,14 @@ class TestLogrotateConfig(unittest.TestCase): self._setup_module_params(start=-1) config_path = os.path.join(self.config_dir, "test") - + def exists_side_effect(path): if path == self.config_dir: return True # Директория существует elif path == config_path: return False # Файл не существует return False - + with patch("os.path.exists", side_effect=exists_side_effect): logrotate_bin = self.mock_module.get_bin_path.return_value config = logrotate.LogrotateConfig(self.mock_module, logrotate_bin) @@ -833,14 +833,14 @@ class TestLogrotateConfig(unittest.TestCase): self._setup_module_params(olddir="/var/log/archives", noolddir=True) config_path = os.path.join(self.config_dir, "test") - + def exists_side_effect(path): if path == self.config_dir: return True # Директория существует elif path == config_path: return False # Файл не существует return False - + with patch("os.path.exists", side_effect=exists_side_effect): logrotate_bin = self.mock_module.get_bin_path.return_value config = logrotate.LogrotateConfig(self.mock_module, logrotate_bin) @@ -868,9 +868,9 @@ class TestLogrotateConfig(unittest.TestCase): copytruncate=False, delaycompress=False, ) - + config_path = os.path.join(self.config_dir, "test") - + def exists_side_effect(path): if path == self.config_dir: return True # Директория существует @@ -913,7 +913,7 @@ class TestLogrotateConfig(unittest.TestCase): self._setup_module_params(olddir="/var/log/archives", noolddir=True) config_path = os.path.join(self.config_dir, "test") - + def exists_side_effect(path): if path == self.config_dir: return True # Директория существует @@ -929,9 +929,9 @@ class TestLogrotateConfig(unittest.TestCase): config.apply() self.assertIn("fail_json called", str(context.exception)) - + self._setup_module_params(copy=True, renamecopy=True) - + with patch("os.path.exists", side_effect=exists_side_effect): logrotate_bin = self.mock_module.get_bin_path.return_value config = logrotate.LogrotateConfig(self.mock_module, logrotate_bin) @@ -942,7 +942,7 @@ class TestLogrotateConfig(unittest.TestCase): self.assertIn("fail_json called", str(context.exception)) self._setup_module_params(copy=True, copytruncate=True) - + with patch("os.path.exists", side_effect=exists_side_effect): logrotate_bin = self.mock_module.get_bin_path.return_value config = logrotate.LogrotateConfig(self.mock_module, logrotate_bin) @@ -962,7 +962,7 @@ class TestLogrotateConfig(unittest.TestCase): with self.subTest(valid_size=size): self._setup_module_params(size=size) config_path = os.path.join(self.config_dir, "test") - + def exists_side_effect(path): if path == self.config_dir: return True # Директория существует @@ -988,7 +988,7 @@ class TestLogrotateConfig(unittest.TestCase): with self.subTest(invalid_size=size): self._setup_module_params(size=size) config_path = os.path.join(self.config_dir, "test") - + def exists_side_effect(path): if path == self.config_dir: return True # Директория существует @@ -1015,7 +1015,7 @@ class TestLogrotateConfig(unittest.TestCase): with self.subTest(valid_size=size): self._setup_module_params(maxsize=size) config_path = os.path.join(self.config_dir, "test") - + def exists_side_effect(path): if path == self.config_dir: return True # Директория существует @@ -1041,7 +1041,7 @@ class TestLogrotateConfig(unittest.TestCase): with self.subTest(invalid_size=size): self._setup_module_params(maxsize=size) config_path = os.path.join(self.config_dir, "test") - + def exists_side_effect(path): if path == self.config_dir: return True # Директория существует @@ -1063,20 +1063,20 @@ class TestLogrotateConfig(unittest.TestCase): from ansible_collections.community.general.plugins.modules import logrotate 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 - + config_path = os.path.join(self.config_dir, "test") - + def exists_side_effect(path): if path == self.config_dir: return True # Директория существует elif path == config_path: return False # Файл не существует return False - + with patch("os.path.exists", side_effect=exists_side_effect): with patch("os.makedirs"): with patch("builtins.open", mock_open()): @@ -1085,7 +1085,7 @@ class TestLogrotateConfig(unittest.TestCase): with patch.object(logrotate.LogrotateConfig, '_backup_config', create=True): 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]