123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- """
- :codeauthor: Piter Punk <piterpunk@slackware.com>
- """
- import os
- import pytest
- import salt.modules.slackware_service as slackware_service
- from tests.support.mock import MagicMock, patch
- @pytest.fixture
- def mocked_rcd():
- glob_output = [
- "/etc/rc.d/rc.S", # system rc file
- "/etc/rc.d/rc.M", # system rc file
- "/etc/rc.d/rc.lxc", # enabled
- "/etc/rc.d/rc.modules", # system rc file
- "/etc/rc.d/rc.ntpd", # enabled
- "/etc/rc.d/rc.rpc", # disabled
- "/etc/rc.d/rc.salt-master", # enabled
- "/etc/rc.d/rc.salt-minion", # disabled
- "/etc/rc.d/rc.something.conf", # config rc file
- "/etc/rc.d/rc.sshd", # disabled
- ]
- access_output = [
- True, # lxc
- True, # ntpd
- False, # rpc
- True, # salt-master
- False, # salt-minion
- False, # sshd
- ]
- glob_mock = patch("glob.glob", autospec=True, return_value=glob_output)
- os_path_exists_mock = patch("os.path.exists", autospec=True, return_value=True)
- os_access_mock = patch("os.access", autospec=True, side_effect=access_output)
- return glob_mock, os_path_exists_mock, os_access_mock
- @pytest.fixture(autouse=True)
- def setup_loader():
- setup_loader_modules = {slackware_service: {}}
- with pytest.helpers.loader_mock(setup_loader_modules) as loader_mock:
- yield loader_mock
- def test_get_all_rc_services_minus_system_and_config_files(mocked_rcd):
- """
- In Slackware, the services are started, stopped, enabled or disabled
- using rc.service scripts under the /etc/rc.d directory.
- This tests if only service rc scripts are returned by get_alli function.
- System rc scripts (like rc.M) and configuration rc files (like
- rc.something.conf) needs to be removed from output. Also, we remove the
- leading "/etc/rc.d/rc." to output only the service names.
- Return list: lxc ntpd rpc salt-master salt-minion sshd
- """
- services_all = ["lxc", "ntpd", "rpc", "salt-master", "salt-minion", "sshd"]
- glob_mock, os_path_exists_mock, os_access_mock = mocked_rcd
- with glob_mock, os_path_exists_mock, os_access_mock:
- assert slackware_service.get_all() == services_all
- def test_if_only_executable_rc_files_are_returned_by_get_enabled(mocked_rcd):
- """
- In Slackware, the services are enabled at boot by setting the executable
- bit in their respective rc files.
- This tests if all system rc scripts, configuration rc files and service rc
- scripts without the executable bit set were filtered out from output.
- Return list: lxc ntpd salt-master
- """
- services_enabled = ["lxc", "ntpd", "salt-master"]
- glob_mock, os_path_exists_mock, os_access_mock = mocked_rcd
- with glob_mock, os_path_exists_mock, os_access_mock:
- assert slackware_service.get_enabled() == services_enabled
- def test_if_only_not_executable_rc_files_are_returned_by_get_disabled(mocked_rcd):
- """
- In Slackware, the services are disabled at boot by unsetting the executable
- bit in their respective rc files.
- This tests if all system rc scripts, configuration rc files and service rc
- scripts with the executable bit set were filtered out from output.
- Return list: rpc salt-minion sshd
- """
- services_disabled = ["rpc", "salt-minion", "sshd"]
- glob_mock, os_path_exists_mock, os_access_mock = mocked_rcd
- with glob_mock, os_path_exists_mock, os_access_mock:
- assert slackware_service.get_disabled() == services_disabled
- def test_if_a_rc_service_file_in_rcd_is_listed_as_available(mocked_rcd):
- """
- Test if an existent service rc file with the rc.service name format is
- present in rc.d directory and returned by "available" function
- """
- glob_mock, os_path_exists_mock, os_access_mock = mocked_rcd
- with glob_mock, os_path_exists_mock, os_access_mock:
- assert slackware_service.available("lxc")
- def test_if_a_rc_service_file_not_in_rcd_is_not_listed_as_available(mocked_rcd):
- """
- Test if a non existent service rc file with the rc.service name format is
- not present in rc.d directory and not returned by "available" function
- """
- glob_mock, os_path_exists_mock, os_access_mock = mocked_rcd
- with glob_mock, os_path_exists_mock, os_access_mock:
- assert not slackware_service.available("docker")
- def test_if_a_rc_service_file_not_in_rcd_is_listed_as_missing(mocked_rcd):
- """
- Test if a non existent service rc file with the rc.service name format is
- not present in rc.d directory and returned by "missing" function
- """
- glob_mock, os_path_exists_mock, os_access_mock = mocked_rcd
- with glob_mock, os_path_exists_mock, os_access_mock:
- assert slackware_service.missing("docker")
- def test_if_a_rc_service_file_in_rcd_is_not_listed_as_missing(mocked_rcd):
- """
- Test if an existent service rc file with the rc.service name format is
- present in rc.d directory and not returned by "missing" function
- """
- glob_mock, os_path_exists_mock, os_access_mock = mocked_rcd
- with glob_mock, os_path_exists_mock, os_access_mock:
- assert not slackware_service.missing("lxc")
- def test_service_start():
- """
- Test for Start the specified service
- """
- mock = MagicMock(return_value=True)
- with patch.dict(slackware_service.__salt__, {"cmd.retcode": mock}):
- assert not slackware_service.start("name")
- def test_service_stop():
- """
- Test for Stop the specified service
- """
- mock = MagicMock(return_value=True)
- with patch.dict(slackware_service.__salt__, {"cmd.retcode": mock}):
- assert not slackware_service.stop("name")
- def test_service_restart():
- """
- Test for Restart the named service
- """
- mock = MagicMock(return_value=True)
- with patch.dict(slackware_service.__salt__, {"cmd.retcode": mock}):
- assert not slackware_service.restart("name")
- def test_service_reload_():
- """
- Test for Reload the named service
- """
- mock = MagicMock(return_value=True)
- with patch.dict(slackware_service.__salt__, {"cmd.retcode": mock}):
- assert not slackware_service.reload_("name")
- def test_service_force_reload():
- """
- Test for Force-reload the named service
- """
- mock = MagicMock(return_value=True)
- with patch.dict(slackware_service.__salt__, {"cmd.retcode": mock}):
- assert not slackware_service.force_reload("name")
- def test_service_status():
- """
- Test for Return the status for a service
- """
- mock = MagicMock(return_value=True)
- with patch.dict(slackware_service.__salt__, {"cmd.retcode": mock}):
- assert not slackware_service.status("name")
- def test_if_executable_bit_is_set_when_enable_a_disabled_service():
- """
- In Slackware, the services are enabled at boot by setting the executable
- bit in their respective rc files.
- This tests if, given a disabled rc file with permissions 0644, we enable it by
- changing its permissions to 0755
- """
- os_path_exists_mock = patch("os.path.exists", autospec=True, return_value=True)
- os_chmod = MagicMock(autospec=True, return_value=True)
- os_chmod_mock = patch("os.chmod", os_chmod)
- os_stat_result = os.stat_result(
- (0o100644, 142555, 64770, 1, 0, 0, 1340, 1597376187, 1597376188, 1597376189)
- )
- os_stat_mock = patch("os.stat", autospec=True, return_value=os_stat_result)
- with os_path_exists_mock, os_chmod_mock, os_stat_mock:
- slackware_service.enable("svc_to_enable")
- os_chmod.assert_called_with("/etc/rc.d/rc.svc_to_enable", 0o100755)
- def test_if_executable_bit_is_unset_when_disable_an_enabled_service():
- """
- In Slackware, the services are disabled at boot by unsetting the executable
- bit in their respective rc files.
- This tests if, given an enabled rc file with permissions 0755, we disable it by
- changing its permissions to 0644
- """
- os_path_exists_mock = patch("os.path.exists", autospec=True, return_value=True)
- os_chmod = MagicMock(autospec=True, return_value=True)
- os_chmod_mock = patch("os.chmod", os_chmod)
- os_stat_result = os.stat_result(
- (0o100755, 142555, 64770, 1, 0, 0, 1340, 1597376187, 1597376188, 1597376189)
- )
- os_stat_mock = patch("os.stat", autospec=True, return_value=os_stat_result)
- with os_path_exists_mock, os_chmod_mock, os_stat_mock:
- slackware_service.disable("svc_to_disable")
- os_chmod.assert_called_with("/etc/rc.d/rc.svc_to_disable", 0o100644)
- def test_if_an_enabled_service_is_not_disabled():
- """
- A service can't be enabled and disabled at same time.
- This tests if a service that returns True to enabled returns False to disabled
- """
- os_path_exists_mock = patch("os.path.exists", autospec=True, return_value=True)
- os_access_mock = patch("os.access", autospec=True, return_value=True)
- with os_path_exists_mock, os_access_mock:
- assert slackware_service.enabled("lxc")
- assert not slackware_service.disabled("lxc")
- def test_if_a_disabled_service_is_not_enabled():
- """
- A service can't be enabled and disabled at same time.
- This tests if a service that returns True to disabled returns False to enabled
- """
- os_path_exists_mock = patch("os.path.exists", autospec=True, return_value=True)
- os_access_mock = patch("os.access", autospec=True, return_value=False)
- with os_path_exists_mock, os_access_mock:
- assert slackware_service.disabled("rpc")
- assert not slackware_service.enabled("rpc")
|