| 1 |
# Copyright (c) Ansible project |
| 2 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 4 |
|
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
from unittest.mock import Mock, mock_open, patch |
| 8 |
|
| 9 |
from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import ( |
| 10 |
ModuleTestCase, |
| 11 |
set_module_args, |
| 12 |
) |
| 13 |
|
| 14 |
from ansible_collections.community.general.plugins.modules.modprobe import Modprobe, build_module |
| 15 |
|
| 16 |
|
| 17 |
class TestLoadModule(ModuleTestCase): |
| 18 |
def setUp(self): |
| 19 |
super().setUp() |
| 20 |
|
| 21 |
self.mock_module_loaded = patch( |
| 22 |
"ansible_collections.community.general.plugins.modules.modprobe.Modprobe.module_loaded" |
| 23 |
) |
| 24 |
self.mock_run_command = patch("ansible.module_utils.basic.AnsibleModule.run_command") |
| 25 |
self.mock_get_bin_path = patch("ansible.module_utils.basic.AnsibleModule.get_bin_path") |
| 26 |
|
| 27 |
self.module_loaded = self.mock_module_loaded.start() |
| 28 |
self.run_command = self.mock_run_command.start() |
| 29 |
self.get_bin_path = self.mock_get_bin_path.start() |
| 30 |
|
| 31 |
def tearDown(self): |
| 32 |
"""Teardown.""" |
| 33 |
super().tearDown() |
| 34 |
self.mock_module_loaded.stop() |
| 35 |
self.mock_run_command.stop() |
| 36 |
self.mock_get_bin_path.stop() |
| 37 |
|
| 38 |
def test_load_module_success(self): |
| 39 |
with set_module_args( |
| 40 |
dict( |
| 41 |
name="test", |
| 42 |
state="present", |
| 43 |
) |
| 44 |
): |
| 45 |
module = build_module() |
| 46 |
|
| 47 |
self.get_bin_path.side_effect = ["modprobe"] |
| 48 |
self.module_loaded.side_effect = [True] |
| 49 |
self.run_command.side_effect = [(0, "", "")] |
| 50 |
|
| 51 |
modprobe = Modprobe(module) |
| 52 |
modprobe.load_module() |
| 53 |
|
| 54 |
assert modprobe.result == { |
| 55 |
"changed": True, |
| 56 |
"name": "test", |
| 57 |
"params": "", |
| 58 |
"state": "present", |
| 59 |
} |
| 60 |
|
| 61 |
def test_load_module_unchanged(self): |
| 62 |
with set_module_args( |
| 63 |
dict( |
| 64 |
name="test", |
| 65 |
state="present", |
| 66 |
) |
| 67 |
): |
| 68 |
module = build_module() |
| 69 |
|
| 70 |
module.warn = Mock() |
| 71 |
|
| 72 |
self.get_bin_path.side_effect = ["modprobe"] |
| 73 |
self.module_loaded.side_effect = [False] |
| 74 |
self.run_command.side_effect = [(0, "", ""), (1, "", "")] |
| 75 |
|
| 76 |
modprobe = Modprobe(module) |
| 77 |
modprobe.load_module() |
| 78 |
|
| 79 |
module.warn.assert_called_once_with("") |
| 80 |
|
| 81 |
|
| 82 |
class TestUnloadModule(ModuleTestCase): |
| 83 |
def setUp(self): |
| 84 |
super().setUp() |
| 85 |
|
| 86 |
self.mock_module_loaded = patch( |
| 87 |
"ansible_collections.community.general.plugins.modules.modprobe.Modprobe.module_loaded" |
| 88 |
) |
| 89 |
self.mock_run_command = patch("ansible.module_utils.basic.AnsibleModule.run_command") |
| 90 |
self.mock_get_bin_path = patch("ansible.module_utils.basic.AnsibleModule.get_bin_path") |
| 91 |
|
| 92 |
self.module_loaded = self.mock_module_loaded.start() |
| 93 |
self.run_command = self.mock_run_command.start() |
| 94 |
self.get_bin_path = self.mock_get_bin_path.start() |
| 95 |
|
| 96 |
def tearDown(self): |
| 97 |
"""Teardown.""" |
| 98 |
super().tearDown() |
| 99 |
self.mock_module_loaded.stop() |
| 100 |
self.mock_run_command.stop() |
| 101 |
self.mock_get_bin_path.stop() |
| 102 |
|
| 103 |
def test_unload_module_success(self): |
| 104 |
with set_module_args( |
| 105 |
dict( |
| 106 |
name="test", |
| 107 |
state="absent", |
| 108 |
) |
| 109 |
): |
| 110 |
module = build_module() |
| 111 |
|
| 112 |
self.get_bin_path.side_effect = ["modprobe"] |
| 113 |
self.module_loaded.side_effect = [False] |
| 114 |
self.run_command.side_effect = [(0, "", "")] |
| 115 |
|
| 116 |
modprobe = Modprobe(module) |
| 117 |
modprobe.unload_module() |
| 118 |
|
| 119 |
assert modprobe.result == { |
| 120 |
"changed": True, |
| 121 |
"name": "test", |
| 122 |
"params": "", |
| 123 |
"state": "absent", |
| 124 |
} |
| 125 |
|
| 126 |
def test_unload_module_failure(self): |
| 127 |
with set_module_args( |
| 128 |
dict( |
| 129 |
name="test", |
| 130 |
state="absent", |
| 131 |
) |
| 132 |
): |
| 133 |
module = build_module() |
| 134 |
|
| 135 |
module.fail_json = Mock() |
| 136 |
|
| 137 |
self.get_bin_path.side_effect = ["modprobe"] |
| 138 |
self.module_loaded.side_effect = [True] |
| 139 |
self.run_command.side_effect = [(1, "", "")] |
| 140 |
|
| 141 |
modprobe = Modprobe(module) |
| 142 |
modprobe.unload_module() |
| 143 |
|
| 144 |
dummy_result = { |
| 145 |
"changed": False, |
| 146 |
"name": "test", |
| 147 |
"state": "absent", |
| 148 |
"params": "", |
| 149 |
} |
| 150 |
|
| 151 |
module.fail_json.assert_called_once_with(msg="", rc=1, stdout="", stderr="", **dummy_result) |
| 152 |
|
| 153 |
|
| 154 |
class TestModuleIsLoadedPersistently(ModuleTestCase): |
| 155 |
def setUp(self): |
| 156 |
super().setUp() |
| 157 |
|
| 158 |
self.mock_get_bin_path = patch("ansible.module_utils.basic.AnsibleModule.get_bin_path") |
| 159 |
|
| 160 |
self.get_bin_path = self.mock_get_bin_path.start() |
| 161 |
|
| 162 |
def tearDown(self): |
| 163 |
"""Teardown.""" |
| 164 |
super().tearDown() |
| 165 |
|
| 166 |
self.mock_get_bin_path.stop() |
| 167 |
|
| 168 |
def test_module_is_loaded(self): |
| 169 |
with set_module_args(dict(name="dummy", state="present", persistent="present")): |
| 170 |
module = build_module() |
| 171 |
|
| 172 |
self.get_bin_path.side_effect = ["modprobe"] |
| 173 |
|
| 174 |
modprobe = Modprobe(module) |
| 175 |
with patch( |
| 176 |
"ansible_collections.community.general.plugins.modules.modprobe.open", mock_open(read_data="dummy") |
| 177 |
) as mocked_file: |
| 178 |
with patch("ansible_collections.community.general.plugins.modules.modprobe.Modprobe.modules_files"): |
| 179 |
modprobe.modules_files = ["/etc/modules-load.d/dummy.conf"] |
| 180 |
|
| 181 |
assert modprobe.module_is_loaded_persistently |
| 182 |
|
| 183 |
mocked_file.assert_called_once_with("/etc/modules-load.d/dummy.conf") |
| 184 |
|
| 185 |
def test_module_is_not_loaded_empty_file(self): |
| 186 |
with set_module_args(dict(name="dummy", state="present", persistent="present")): |
| 187 |
module = build_module() |
| 188 |
|
| 189 |
self.get_bin_path.side_effect = ["modprobe"] |
| 190 |
|
| 191 |
modprobe = Modprobe(module) |
| 192 |
with patch( |
| 193 |
"ansible_collections.community.general.plugins.modules.modprobe.open", mock_open(read_data="") |
| 194 |
) as mocked_file: |
| 195 |
with patch("ansible_collections.community.general.plugins.modules.modprobe.Modprobe.modules_files"): |
| 196 |
modprobe.modules_files = ["/etc/modules-load.d/dummy.conf"] |
| 197 |
|
| 198 |
assert not modprobe.module_is_loaded_persistently |
| 199 |
|
| 200 |
mocked_file.assert_called_once_with("/etc/modules-load.d/dummy.conf") |
| 201 |
|
| 202 |
def test_module_is_not_loaded_no_files(self): |
| 203 |
with set_module_args(dict(name="dummy", state="present", persistent="present")): |
| 204 |
module = build_module() |
| 205 |
|
| 206 |
self.get_bin_path.side_effect = ["modprobe"] |
| 207 |
|
| 208 |
modprobe = Modprobe(module) |
| 209 |
with patch("ansible_collections.community.general.plugins.modules.modprobe.Modprobe.modules_files"): |
| 210 |
modprobe.modules_files = [] |
| 211 |
|
| 212 |
assert not modprobe.module_is_loaded_persistently |
| 213 |
|
| 214 |
|
| 215 |
class TestPermanentParams(ModuleTestCase): |
| 216 |
def setUp(self): |
| 217 |
super().setUp() |
| 218 |
|
| 219 |
self.mock_get_bin_path = patch("ansible.module_utils.basic.AnsibleModule.get_bin_path") |
| 220 |
|
| 221 |
self.get_bin_path = self.mock_get_bin_path.start() |
| 222 |
|
| 223 |
def tearDown(self): |
| 224 |
"""Teardown.""" |
| 225 |
super().tearDown() |
| 226 |
|
| 227 |
self.mock_get_bin_path.stop() |
| 228 |
|
| 229 |
def test_module_permanent_params_exist(self): |
| 230 |
files_content = [ |
| 231 |
"options dummy numdummies=4\noptions dummy dummy_parameter1=6", |
| 232 |
"options dummy dummy_parameter2=5 #Comment\noptions notdummy notdummy_param=5", |
| 233 |
] |
| 234 |
mock_files_content = [mock_open(read_data=content).return_value for content in files_content] |
| 235 |
|
| 236 |
with set_module_args(dict(name="dummy", state="present", persistent="present")): |
| 237 |
module = build_module() |
| 238 |
|
| 239 |
self.get_bin_path.side_effect = ["modprobe"] |
| 240 |
|
| 241 |
modprobe = Modprobe(module) |
| 242 |
|
| 243 |
with patch( |
| 244 |
"ansible_collections.community.general.plugins.modules.modprobe.open", mock_open() |
| 245 |
) as mocked_file: |
| 246 |
mocked_file.side_effect = mock_files_content |
| 247 |
with patch("ansible_collections.community.general.plugins.modules.modprobe.Modprobe.modprobe_files"): |
| 248 |
modprobe.modprobe_files = ["/etc/modprobe.d/dummy1.conf", "/etc/modprobe.d/dummy2.conf"] |
| 249 |
|
| 250 |
assert modprobe.permanent_params == {"numdummies=4", "dummy_parameter1=6", "dummy_parameter2=5"} |
| 251 |
|
| 252 |
def test_module_permanent_params_empty(self): |
| 253 |
files_content = ["", ""] |
| 254 |
mock_files_content = [mock_open(read_data=content).return_value for content in files_content] |
| 255 |
|
| 256 |
with set_module_args(dict(name="dummy", state="present", persistent="present")): |
| 257 |
module = build_module() |
| 258 |
|
| 259 |
self.get_bin_path.side_effect = ["modprobe"] |
| 260 |
|
| 261 |
modprobe = Modprobe(module) |
| 262 |
|
| 263 |
with patch( |
| 264 |
"ansible_collections.community.general.plugins.modules.modprobe.open", mock_open(read_data="") |
| 265 |
) as mocked_file: |
| 266 |
mocked_file.side_effect = mock_files_content |
| 267 |
with patch("ansible_collections.community.general.plugins.modules.modprobe.Modprobe.modprobe_files"): |
| 268 |
modprobe.modprobe_files = ["/etc/modprobe.d/dummy1.conf", "/etc/modprobe.d/dummy2.conf"] |
| 269 |
|
| 270 |
assert modprobe.permanent_params == set() |
| 271 |
|
| 272 |
|
| 273 |
class TestCreateModuleFIle(ModuleTestCase): |
| 274 |
def setUp(self): |
| 275 |
super().setUp() |
| 276 |
|
| 277 |
self.mock_get_bin_path = patch("ansible.module_utils.basic.AnsibleModule.get_bin_path") |
| 278 |
|
| 279 |
self.get_bin_path = self.mock_get_bin_path.start() |
| 280 |
|
| 281 |
def tearDown(self): |
| 282 |
"""Teardown.""" |
| 283 |
super().tearDown() |
| 284 |
|
| 285 |
self.mock_get_bin_path.stop() |
| 286 |
|
| 287 |
def test_create_file(self): |
| 288 |
with set_module_args(dict(name="dummy", state="present", persistent="present")): |
| 289 |
module = build_module() |
| 290 |
|
| 291 |
self.get_bin_path.side_effect = ["modprobe"] |
| 292 |
|
| 293 |
modprobe = Modprobe(module) |
| 294 |
|
| 295 |
with patch( |
| 296 |
"ansible_collections.community.general.plugins.modules.modprobe.open", mock_open() |
| 297 |
) as mocked_file: |
| 298 |
modprobe.create_module_file() |
| 299 |
mocked_file.assert_called_once_with("/etc/modules-load.d/dummy.conf", "w") |
| 300 |
mocked_file().write.assert_called_once_with("dummy\n") |
| 301 |
|
| 302 |
|
| 303 |
class TestCreateModuleOptionsFIle(ModuleTestCase): |
| 304 |
def setUp(self): |
| 305 |
super().setUp() |
| 306 |
|
| 307 |
self.mock_get_bin_path = patch("ansible.module_utils.basic.AnsibleModule.get_bin_path") |
| 308 |
|
| 309 |
self.get_bin_path = self.mock_get_bin_path.start() |
| 310 |
|
| 311 |
def tearDown(self): |
| 312 |
"""Teardown.""" |
| 313 |
super().tearDown() |
| 314 |
|
| 315 |
self.mock_get_bin_path.stop() |
| 316 |
|
| 317 |
def test_create_file(self): |
| 318 |
with set_module_args(dict(name="dummy", state="present", params="numdummies=4", persistent="present")): |
| 319 |
module = build_module() |
| 320 |
|
| 321 |
self.get_bin_path.side_effect = ["modprobe"] |
| 322 |
|
| 323 |
modprobe = Modprobe(module) |
| 324 |
|
| 325 |
with patch( |
| 326 |
"ansible_collections.community.general.plugins.modules.modprobe.open", mock_open() |
| 327 |
) as mocked_file: |
| 328 |
modprobe.create_module_options_file() |
| 329 |
mocked_file.assert_called_once_with("/etc/modprobe.d/dummy.conf", "w") |
| 330 |
mocked_file().write.assert_called_once_with("options dummy numdummies=4\n") |
| 331 |
|
| 332 |
|
| 333 |
class TestDisableOldParams(ModuleTestCase): |
| 334 |
def setUp(self): |
| 335 |
super().setUp() |
| 336 |
|
| 337 |
self.mock_get_bin_path = patch("ansible.module_utils.basic.AnsibleModule.get_bin_path") |
| 338 |
|
| 339 |
self.get_bin_path = self.mock_get_bin_path.start() |
| 340 |
|
| 341 |
def tearDown(self): |
| 342 |
"""Teardown.""" |
| 343 |
super().tearDown() |
| 344 |
|
| 345 |
self.mock_get_bin_path.stop() |
| 346 |
|
| 347 |
def test_disable_old_params_file_changed(self): |
| 348 |
mock_data = "options dummy numdummies=4" |
| 349 |
|
| 350 |
with set_module_args(dict(name="dummy", state="present", params="numdummies=4", persistent="present")): |
| 351 |
module = build_module() |
| 352 |
|
| 353 |
self.get_bin_path.side_effect = ["modprobe"] |
| 354 |
|
| 355 |
modprobe = Modprobe(module) |
| 356 |
|
| 357 |
with patch( |
| 358 |
"ansible_collections.community.general.plugins.modules.modprobe.open", mock_open(read_data=mock_data) |
| 359 |
) as mocked_file: |
| 360 |
with patch("ansible_collections.community.general.plugins.modules.modprobe.Modprobe.modprobe_files"): |
| 361 |
modprobe.modprobe_files = ["/etc/modprobe.d/dummy1.conf"] |
| 362 |
modprobe.disable_old_params() |
| 363 |
mocked_file.assert_called_with("/etc/modprobe.d/dummy1.conf", "w") |
| 364 |
mocked_file().write.assert_called_once_with("#options dummy numdummies=4") |
| 365 |
|
| 366 |
def test_disable_old_params_file_unchanged(self): |
| 367 |
mock_data = "options notdummy numdummies=4" |
| 368 |
|
| 369 |
with set_module_args(dict(name="dummy", state="present", params="numdummies=4", persistent="present")): |
| 370 |
module = build_module() |
| 371 |
|
| 372 |
self.get_bin_path.side_effect = ["modprobe"] |
| 373 |
|
| 374 |
modprobe = Modprobe(module) |
| 375 |
|
| 376 |
with patch( |
| 377 |
"ansible_collections.community.general.plugins.modules.modprobe.open", mock_open(read_data=mock_data) |
| 378 |
) as mocked_file: |
| 379 |
with patch("ansible_collections.community.general.plugins.modules.modprobe.Modprobe.modprobe_files"): |
| 380 |
modprobe.modprobe_files = ["/etc/modprobe.d/dummy1.conf"] |
| 381 |
modprobe.disable_old_params() |
| 382 |
mocked_file.assert_called_once_with("/etc/modprobe.d/dummy1.conf") |
| 383 |
|
| 384 |
|
| 385 |
class TestDisableModulePermanent(ModuleTestCase): |
| 386 |
def setUp(self): |
| 387 |
super().setUp() |
| 388 |
|
| 389 |
self.mock_get_bin_path = patch("ansible.module_utils.basic.AnsibleModule.get_bin_path") |
| 390 |
|
| 391 |
self.get_bin_path = self.mock_get_bin_path.start() |
| 392 |
|
| 393 |
def tearDown(self): |
| 394 |
"""Teardown.""" |
| 395 |
super().tearDown() |
| 396 |
|
| 397 |
self.mock_get_bin_path.stop() |
| 398 |
|
| 399 |
def test_disable_module_permanent_file_changed(self): |
| 400 |
with set_module_args(dict(name="dummy", state="present", params="numdummies=4", persistent="present")): |
| 401 |
module = build_module() |
| 402 |
|
| 403 |
self.get_bin_path.side_effect = ["modprobe"] |
| 404 |
|
| 405 |
modprobe = Modprobe(module) |
| 406 |
|
| 407 |
with patch( |
| 408 |
"ansible_collections.community.general.plugins.modules.modprobe.open", mock_open(read_data="dummy") |
| 409 |
) as mocked_file: |
| 410 |
with patch("ansible_collections.community.general.plugins.modules.modprobe.Modprobe.modules_files"): |
| 411 |
modprobe.modules_files = ["/etc/modules-load.d/dummy.conf"] |
| 412 |
modprobe.disable_module_permanent() |
| 413 |
mocked_file.assert_called_with("/etc/modules-load.d/dummy.conf", "w") |
| 414 |
mocked_file().write.assert_called_once_with("#dummy") |
| 415 |
|
| 416 |
def test_disable_module_permanent_file_unchanged(self): |
| 417 |
with set_module_args(dict(name="dummy", state="present", params="numdummies=4", persistent="present")): |
| 418 |
module = build_module() |
| 419 |
|
| 420 |
self.get_bin_path.side_effect = ["modprobe"] |
| 421 |
|
| 422 |
modprobe = Modprobe(module) |
| 423 |
|
| 424 |
with patch( |
| 425 |
"ansible_collections.community.general.plugins.modules.modprobe.open", mock_open(read_data="notdummy") |
| 426 |
) as mocked_file: |
| 427 |
with patch("ansible_collections.community.general.plugins.modules.modprobe.Modprobe.modules_files"): |
| 428 |
modprobe.modules_files = ["/etc/modules-load.d/dummy.conf"] |
| 429 |
modprobe.disable_module_permanent() |
| 430 |
mocked_file.assert_called_once_with("/etc/modules-load.d/dummy.conf") |