1
0

test_mac_service.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: Megan Wilhite<mwilhite@saltstack.com>
  4. """
  5. # Import Python libs
  6. from __future__ import absolute_import
  7. # Import Salt Libs
  8. import salt.modules.mac_service as mac_service
  9. # Import Salt Testing Libs
  10. from tests.support.mixins import LoaderModuleMockMixin
  11. from tests.support.mock import MagicMock, patch
  12. from tests.support.unit import TestCase
  13. class MacServiceTestCase(TestCase, LoaderModuleMockMixin):
  14. """
  15. TestCase for salt.modules.mac_service module
  16. """
  17. def setup_loader_modules(self):
  18. return {mac_service: {}}
  19. def test_service_disabled_when_enabled(self):
  20. """
  21. test service.disabled when service is enabled
  22. """
  23. srv_name = "com.apple.atrun"
  24. cmd = 'disabled services = {\n\t"com.saltstack.salt.minion" => false\n\t"com.apple.atrun" => false\n{'
  25. with patch.object(mac_service, "launchctl", MagicMock(return_value=cmd)):
  26. self.assertFalse(mac_service.disabled(srv_name))
  27. def test_service_disabled_when_disabled(self):
  28. """
  29. test service.disabled when service is disabled
  30. """
  31. srv_name = "com.apple.atrun"
  32. cmd = 'disabled services = {\n\t"com.saltstack.salt.minion" => false\n\t"com.apple.atrun" => true\n{'
  33. with patch.object(mac_service, "launchctl", MagicMock(return_value=cmd)):
  34. self.assertTrue(mac_service.disabled(srv_name))
  35. def test_service_disabled_srvname_wrong(self):
  36. """
  37. test service.disabled when service is just slightly wrong
  38. """
  39. srv_names = ["com.apple.atru", "com", "apple"]
  40. cmd = 'disabled services = {\n\t"com.saltstack.salt.minion" => false\n\t"com.apple.atrun" => true\n}'
  41. for name in srv_names:
  42. with patch.object(mac_service, "launchctl", MagicMock(return_value=cmd)):
  43. self.assertFalse(mac_service.disabled(name))
  44. def test_service_disabled_status_upper_case(self):
  45. """
  46. test service.disabled when disabled status is uppercase
  47. """
  48. srv_name = "com.apple.atrun"
  49. cmd = 'disabled services = {\n\t"com.saltstack.salt.minion" => false\n\t"com.apple.atrun" => True\n{'
  50. with patch.object(mac_service, "launchctl", MagicMock(return_value=cmd)):
  51. self.assertTrue(mac_service.disabled(srv_name))
  52. def test_service_keep_alive_pathstate_file_rm(self):
  53. """
  54. test _always_running_service when keep_alive
  55. has pathstate set in plist file and file doesn't exist
  56. """
  57. srv_name = "com.apple.atrun"
  58. info = {
  59. "plist": {
  60. "EnableTransactions": True,
  61. "ProgramArguments": ["/usr/libexec/ntpd-wrapper"],
  62. "Label": "org.ntp.ntpd",
  63. "KeepAlive": {"PathState": {"/private/etc/ntp.conf": True}},
  64. }
  65. }
  66. with patch.object(mac_service, "show", MagicMock(return_value=info)):
  67. with patch("os.path.exists", MagicMock(return_value=False)):
  68. assert mac_service._always_running_service(srv_name) is False
  69. def test_service_keep_alive_empty(self):
  70. """
  71. test _always_running_service when keep_alive
  72. is empty
  73. """
  74. srv_name = "com.apple.atrun"
  75. info = {
  76. "plist": {
  77. "EnableTransactions": True,
  78. "ProgramArguments": ["/usr/libexec/ntpd-wrapper"],
  79. "Label": "org.ntp.ntpd",
  80. "KeepAlive": {},
  81. }
  82. }
  83. with patch.object(mac_service, "show", MagicMock(return_value=info)):
  84. with patch("os.path.exists", MagicMock(return_value=False)):
  85. assert mac_service._always_running_service(srv_name) is False
  86. def test_service_keep_alive_pathstate_false(self):
  87. """
  88. test _always_running_service when keep_alive
  89. has pathstate set in plist file and file is false
  90. """
  91. srv_name = "com.apple.atrun"
  92. info = {
  93. "plist": {
  94. "EnableTransactions": True,
  95. "ProgramArguments": ["/usr/libexec/ntpd-wrapper"],
  96. "Label": "org.ntp.ntpd",
  97. "KeepAlive": {"PathState": {"/private/etc/ntp.conf": False}},
  98. }
  99. }
  100. with patch.object(mac_service, "show", MagicMock(return_value=info)):
  101. with patch("os.path.exists", MagicMock(return_value=False)):
  102. assert mac_service._always_running_service(srv_name) is True
  103. def test_service_keep_alive_pathstate(self):
  104. """
  105. test _always_running_service when keep_alive
  106. has pathstate set in plist file
  107. """
  108. srv_name = "com.apple.atrun"
  109. info = {
  110. "plist": {
  111. "EnableTransactions": True,
  112. "ProgramArguments": ["/usr/libexec/ntpd-wrapper"],
  113. "Label": "org.ntp.ntpd",
  114. "KeepAlive": {"PathState": {"/private/etc/ntp.conf": True}},
  115. }
  116. }
  117. with patch.object(mac_service, "show", MagicMock(return_value=info)):
  118. with patch("os.path.exists", MagicMock(return_value=True)):
  119. assert mac_service._always_running_service(srv_name) is True
  120. def test_service_keep_alive(self):
  121. """
  122. test _always_running_service when keep_alive set
  123. """
  124. srv_name = "com.apple.atrun"
  125. info = {
  126. "plist": {
  127. "EnableTransactions": True,
  128. "ProgramArguments": ["/usr/libexec/ntpd-wrapper"],
  129. "Label": "org.ntp.ntpd",
  130. "KeepAlive": True,
  131. }
  132. }
  133. with patch.object(mac_service, "show", MagicMock(return_value=info)):
  134. assert mac_service._always_running_service(srv_name) is True
  135. def test_service_keep_alive_false(self):
  136. """
  137. test _always_running_service when keep_alive False
  138. """
  139. srv_name = "com.apple.atrun"
  140. info = {
  141. "plist": {
  142. "EnableTransactions": True,
  143. "ProgramArguments": ["/usr/libexec/ntpd-wrapper"],
  144. "Label": "org.ntp.ntpd",
  145. "KeepAlive": False,
  146. }
  147. }
  148. with patch.object(mac_service, "show", MagicMock(return_value=info)):
  149. assert mac_service._always_running_service(srv_name) is False
  150. def test_service_keep_alive_missing(self):
  151. """
  152. test _always_running_service when keep_alive not in dict
  153. """
  154. srv_name = "com.apple.atrun"
  155. info = {
  156. "plist": {
  157. "EnableTransactions": True,
  158. "ProgramArguments": ["/usr/libexec/ntpd-wrapper"],
  159. "Label": "org.ntp.ntpd",
  160. }
  161. }
  162. with patch.object(mac_service, "show", MagicMock(return_value=info)):
  163. assert mac_service._always_running_service(srv_name) is False
  164. def test_service_keep_alive_wrong_setting(self):
  165. """
  166. test _always_running_service when keep_alive
  167. has pathstate set in plist file
  168. """
  169. srv_name = "com.apple.atrun"
  170. info = {
  171. "plist": {
  172. "EnableTransactions": True,
  173. "ProgramArguments": ["/usr/libexec/ntpd-wrapper"],
  174. "Label": "org.ntp.ntpd",
  175. "KeepAlive": {"Doesnotexist": {"doesnt_exist": True}},
  176. }
  177. }
  178. with patch.object(mac_service, "show", MagicMock(return_value=info)):
  179. assert mac_service._always_running_service(srv_name) is False