test_slackware_service.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. """
  2. :codeauthor: Piter Punk <piterpunk@slackware.com>
  3. """
  4. import os
  5. import pytest
  6. import salt.modules.slackware_service as slackware_service
  7. from tests.support.mock import MagicMock, patch
  8. @pytest.fixture
  9. def mocked_rcd():
  10. glob_output = [
  11. "/etc/rc.d/rc.S", # system rc file
  12. "/etc/rc.d/rc.M", # system rc file
  13. "/etc/rc.d/rc.lxc", # enabled
  14. "/etc/rc.d/rc.modules", # system rc file
  15. "/etc/rc.d/rc.ntpd", # enabled
  16. "/etc/rc.d/rc.rpc", # disabled
  17. "/etc/rc.d/rc.salt-master", # enabled
  18. "/etc/rc.d/rc.salt-minion", # disabled
  19. "/etc/rc.d/rc.something.conf", # config rc file
  20. "/etc/rc.d/rc.sshd", # disabled
  21. ]
  22. access_output = [
  23. True, # lxc
  24. True, # ntpd
  25. False, # rpc
  26. True, # salt-master
  27. False, # salt-minion
  28. False, # sshd
  29. ]
  30. glob_mock = patch("glob.glob", autospec=True, return_value=glob_output)
  31. os_path_exists_mock = patch("os.path.exists", autospec=True, return_value=True)
  32. os_access_mock = patch("os.access", autospec=True, side_effect=access_output)
  33. return glob_mock, os_path_exists_mock, os_access_mock
  34. @pytest.fixture(autouse=True)
  35. def setup_loader():
  36. setup_loader_modules = {slackware_service: {}}
  37. with pytest.helpers.loader_mock(setup_loader_modules) as loader_mock:
  38. yield loader_mock
  39. def test_get_all_rc_services_minus_system_and_config_files(mocked_rcd):
  40. """
  41. In Slackware, the services are started, stopped, enabled or disabled
  42. using rc.service scripts under the /etc/rc.d directory.
  43. This tests if only service rc scripts are returned by get_alli function.
  44. System rc scripts (like rc.M) and configuration rc files (like
  45. rc.something.conf) needs to be removed from output. Also, we remove the
  46. leading "/etc/rc.d/rc." to output only the service names.
  47. Return list: lxc ntpd rpc salt-master salt-minion sshd
  48. """
  49. services_all = ["lxc", "ntpd", "rpc", "salt-master", "salt-minion", "sshd"]
  50. glob_mock, os_path_exists_mock, os_access_mock = mocked_rcd
  51. with glob_mock, os_path_exists_mock, os_access_mock:
  52. assert slackware_service.get_all() == services_all
  53. def test_if_only_executable_rc_files_are_returned_by_get_enabled(mocked_rcd):
  54. """
  55. In Slackware, the services are enabled at boot by setting the executable
  56. bit in their respective rc files.
  57. This tests if all system rc scripts, configuration rc files and service rc
  58. scripts without the executable bit set were filtered out from output.
  59. Return list: lxc ntpd salt-master
  60. """
  61. services_enabled = ["lxc", "ntpd", "salt-master"]
  62. glob_mock, os_path_exists_mock, os_access_mock = mocked_rcd
  63. with glob_mock, os_path_exists_mock, os_access_mock:
  64. assert slackware_service.get_enabled() == services_enabled
  65. def test_if_only_not_executable_rc_files_are_returned_by_get_disabled(mocked_rcd):
  66. """
  67. In Slackware, the services are disabled at boot by unsetting the executable
  68. bit in their respective rc files.
  69. This tests if all system rc scripts, configuration rc files and service rc
  70. scripts with the executable bit set were filtered out from output.
  71. Return list: rpc salt-minion sshd
  72. """
  73. services_disabled = ["rpc", "salt-minion", "sshd"]
  74. glob_mock, os_path_exists_mock, os_access_mock = mocked_rcd
  75. with glob_mock, os_path_exists_mock, os_access_mock:
  76. assert slackware_service.get_disabled() == services_disabled
  77. def test_if_a_rc_service_file_in_rcd_is_listed_as_available(mocked_rcd):
  78. """
  79. Test if an existent service rc file with the rc.service name format is
  80. present in rc.d directory and returned by "available" function
  81. """
  82. glob_mock, os_path_exists_mock, os_access_mock = mocked_rcd
  83. with glob_mock, os_path_exists_mock, os_access_mock:
  84. assert slackware_service.available("lxc")
  85. def test_if_a_rc_service_file_not_in_rcd_is_not_listed_as_available(mocked_rcd):
  86. """
  87. Test if a non existent service rc file with the rc.service name format is
  88. not present in rc.d directory and not returned by "available" function
  89. """
  90. glob_mock, os_path_exists_mock, os_access_mock = mocked_rcd
  91. with glob_mock, os_path_exists_mock, os_access_mock:
  92. assert not slackware_service.available("docker")
  93. def test_if_a_rc_service_file_not_in_rcd_is_listed_as_missing(mocked_rcd):
  94. """
  95. Test if a non existent service rc file with the rc.service name format is
  96. not present in rc.d directory and returned by "missing" function
  97. """
  98. glob_mock, os_path_exists_mock, os_access_mock = mocked_rcd
  99. with glob_mock, os_path_exists_mock, os_access_mock:
  100. assert slackware_service.missing("docker")
  101. def test_if_a_rc_service_file_in_rcd_is_not_listed_as_missing(mocked_rcd):
  102. """
  103. Test if an existent service rc file with the rc.service name format is
  104. present in rc.d directory and not returned by "missing" function
  105. """
  106. glob_mock, os_path_exists_mock, os_access_mock = mocked_rcd
  107. with glob_mock, os_path_exists_mock, os_access_mock:
  108. assert not slackware_service.missing("lxc")
  109. def test_service_start():
  110. """
  111. Test for Start the specified service
  112. """
  113. mock = MagicMock(return_value=True)
  114. with patch.dict(slackware_service.__salt__, {"cmd.retcode": mock}):
  115. assert not slackware_service.start("name")
  116. def test_service_stop():
  117. """
  118. Test for Stop the specified service
  119. """
  120. mock = MagicMock(return_value=True)
  121. with patch.dict(slackware_service.__salt__, {"cmd.retcode": mock}):
  122. assert not slackware_service.stop("name")
  123. def test_service_restart():
  124. """
  125. Test for Restart the named service
  126. """
  127. mock = MagicMock(return_value=True)
  128. with patch.dict(slackware_service.__salt__, {"cmd.retcode": mock}):
  129. assert not slackware_service.restart("name")
  130. def test_service_reload_():
  131. """
  132. Test for Reload the named service
  133. """
  134. mock = MagicMock(return_value=True)
  135. with patch.dict(slackware_service.__salt__, {"cmd.retcode": mock}):
  136. assert not slackware_service.reload_("name")
  137. def test_service_force_reload():
  138. """
  139. Test for Force-reload the named service
  140. """
  141. mock = MagicMock(return_value=True)
  142. with patch.dict(slackware_service.__salt__, {"cmd.retcode": mock}):
  143. assert not slackware_service.force_reload("name")
  144. def test_service_status():
  145. """
  146. Test for Return the status for a service
  147. """
  148. mock = MagicMock(return_value=True)
  149. with patch.dict(slackware_service.__salt__, {"cmd.retcode": mock}):
  150. assert not slackware_service.status("name")
  151. def test_if_executable_bit_is_set_when_enable_a_disabled_service():
  152. """
  153. In Slackware, the services are enabled at boot by setting the executable
  154. bit in their respective rc files.
  155. This tests if, given a disabled rc file with permissions 0644, we enable it by
  156. changing its permissions to 0755
  157. """
  158. os_path_exists_mock = patch("os.path.exists", autospec=True, return_value=True)
  159. os_chmod = MagicMock(autospec=True, return_value=True)
  160. os_chmod_mock = patch("os.chmod", os_chmod)
  161. os_stat_result = os.stat_result(
  162. (0o100644, 142555, 64770, 1, 0, 0, 1340, 1597376187, 1597376188, 1597376189)
  163. )
  164. os_stat_mock = patch("os.stat", autospec=True, return_value=os_stat_result)
  165. with os_path_exists_mock, os_chmod_mock, os_stat_mock:
  166. slackware_service.enable("svc_to_enable")
  167. os_chmod.assert_called_with("/etc/rc.d/rc.svc_to_enable", 0o100755)
  168. def test_if_executable_bit_is_unset_when_disable_an_enabled_service():
  169. """
  170. In Slackware, the services are disabled at boot by unsetting the executable
  171. bit in their respective rc files.
  172. This tests if, given an enabled rc file with permissions 0755, we disable it by
  173. changing its permissions to 0644
  174. """
  175. os_path_exists_mock = patch("os.path.exists", autospec=True, return_value=True)
  176. os_chmod = MagicMock(autospec=True, return_value=True)
  177. os_chmod_mock = patch("os.chmod", os_chmod)
  178. os_stat_result = os.stat_result(
  179. (0o100755, 142555, 64770, 1, 0, 0, 1340, 1597376187, 1597376188, 1597376189)
  180. )
  181. os_stat_mock = patch("os.stat", autospec=True, return_value=os_stat_result)
  182. with os_path_exists_mock, os_chmod_mock, os_stat_mock:
  183. slackware_service.disable("svc_to_disable")
  184. os_chmod.assert_called_with("/etc/rc.d/rc.svc_to_disable", 0o100644)
  185. def test_if_an_enabled_service_is_not_disabled():
  186. """
  187. A service can't be enabled and disabled at same time.
  188. This tests if a service that returns True to enabled returns False to disabled
  189. """
  190. os_path_exists_mock = patch("os.path.exists", autospec=True, return_value=True)
  191. os_access_mock = patch("os.access", autospec=True, return_value=True)
  192. with os_path_exists_mock, os_access_mock:
  193. assert slackware_service.enabled("lxc")
  194. assert not slackware_service.disabled("lxc")
  195. def test_if_a_disabled_service_is_not_enabled():
  196. """
  197. A service can't be enabled and disabled at same time.
  198. This tests if a service that returns True to disabled returns False to enabled
  199. """
  200. os_path_exists_mock = patch("os.path.exists", autospec=True, return_value=True)
  201. os_access_mock = patch("os.access", autospec=True, return_value=False)
  202. with os_path_exists_mock, os_access_mock:
  203. assert slackware_service.disabled("rpc")
  204. assert not slackware_service.enabled("rpc")