test_opkg.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import pytest
  2. import salt.modules.opkg as opkg
  3. from tests.support.mock import patch
  4. @pytest.fixture(autouse=True)
  5. def setup_loader():
  6. setup_loader_modules = {opkg: {}}
  7. with pytest.helpers.loader_mock(setup_loader_modules) as loader_mock:
  8. yield loader_mock
  9. def test_when_os_is_NILinuxRT_and_creation_of_RESTART_CHECK_STATE_PATH_fails_virtual_should_be_False():
  10. expected_result = (
  11. False,
  12. "Error creating /var/lib/salt/restartcheck_state (-whatever): 42",
  13. )
  14. with patch.dict(opkg.__grains__, {"os_family": "NILinuxRT"}), patch(
  15. "os.makedirs", autospec=True, side_effect=OSError("whatever", 42, "boop")
  16. ):
  17. result = opkg.__virtual__()
  18. assert result == expected_result
  19. def test_when_os_is_NILinuxRT_and_creation_is_OK_and_no_files_exist_then_files_should_be_updated():
  20. patch_grains = patch.dict(opkg.__grains__, {"os_family": "NILinuxRT"})
  21. patch_makedirs = patch("os.makedirs", autospec=True, return_value=None)
  22. patch_update_state = patch(
  23. "salt.modules.opkg._update_nilrt_restart_state", autospec=True
  24. )
  25. patch_listdir = patch("os.listdir", return_value=[], autospec=True)
  26. with patch_grains, patch_makedirs, patch_listdir, patch_update_state as fake_update:
  27. opkg.__virtual__()
  28. fake_update.assert_called_once()
  29. def test_when_os_is_NILinuxRT_and_creation_is_OK_and_files_already_exist_then_files_should_not_be_updated():
  30. patch_grains = patch.dict(opkg.__grains__, {"os_family": "NILinuxRT"})
  31. patch_makedirs = patch("os.makedirs", autospec=True, return_value=None)
  32. patch_update_state = patch(
  33. "salt.modules.opkg._update_nilrt_restart_state", autospec=True
  34. )
  35. patch_listdir = patch(
  36. "os.listdir", return_value=["these", "are", "pretend", "files"], autospec=True
  37. )
  38. with patch_grains, patch_makedirs, patch_listdir, patch_update_state as fake_update:
  39. opkg.__virtual__()
  40. fake_update.assert_not_called()