test_mac_service.py 7.2 KB

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