test_launchctl_service.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: Rupesh Tare <rupesht@saltstack.com>
  4. """
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import salt.modules.launchctl_service as launchctl
  8. import salt.utils.stringutils
  9. # Import Salt Libs
  10. from salt.ext import six
  11. # Import Salt Testing Libs
  12. from tests.support.mixins import LoaderModuleMockMixin
  13. from tests.support.mock import MagicMock, patch
  14. from tests.support.unit import TestCase
  15. class LaunchctlTestCase(TestCase, LoaderModuleMockMixin):
  16. """
  17. Test cases for salt.modules.launchctl
  18. """
  19. def setup_loader_modules(self):
  20. return {launchctl: {}}
  21. def test_get_all(self):
  22. """
  23. Test for Return all installed services
  24. """
  25. with patch.dict(
  26. launchctl.__salt__, {"cmd.run": MagicMock(return_value="A\tB\tC\t\n")}
  27. ):
  28. with patch.object(
  29. launchctl, "_available_services", return_value={"A": "a", "B": "b"}
  30. ):
  31. self.assertEqual(launchctl.get_all(), ["A", "B", "C"])
  32. def test_available(self):
  33. """
  34. Test for Check that the given service is available.
  35. """
  36. with patch.object(launchctl, "_service_by_name", return_value=True):
  37. self.assertTrue(launchctl.available("job_label"))
  38. def test_missing(self):
  39. """
  40. Test for The inverse of service.available
  41. """
  42. with patch.object(launchctl, "_service_by_name", return_value=True):
  43. self.assertFalse(launchctl.missing("job_label"))
  44. def test_status(self):
  45. """
  46. Test for Return the status for a service
  47. """
  48. launchctl_data = """<?xml version="1.0" encoding="UTF-8"?>
  49. <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  50. <plist version="1.0">
  51. <dict>
  52. <key>Label</key>
  53. <string>salt-minion</string>
  54. <key>LastExitStatus</key>
  55. <integer>0</integer>
  56. <key>LimitLoadToSessionType</key>
  57. <string>System</string>
  58. <key>OnDemand</key>
  59. <false/>
  60. <key>PID</key>
  61. <integer>71</integer>
  62. <key>ProgramArguments</key>
  63. <array>
  64. <string>/usr/local/bin/salt-minion</string>
  65. </array>
  66. <key>TimeOut</key>
  67. <integer>30</integer>
  68. </dict>
  69. </plist>"""
  70. with patch.object(
  71. launchctl, "_service_by_name", return_value={"plist": {"Label": "A"}}
  72. ):
  73. if six.PY3:
  74. launchctl_data = salt.utils.stringutils.to_bytes(launchctl_data)
  75. with patch.object(
  76. launchctl, "_get_launchctl_data", return_value=launchctl_data
  77. ):
  78. self.assertTrue(launchctl.status("job_label"))
  79. def test_stop(self):
  80. """
  81. Test for Stop the specified service
  82. """
  83. with patch.object(
  84. launchctl, "_service_by_name", return_value={"file_path": "A"}
  85. ):
  86. with patch.dict(
  87. launchctl.__salt__, {"cmd.retcode": MagicMock(return_value=False)}
  88. ):
  89. self.assertTrue(launchctl.stop("job_label"))
  90. with patch.object(launchctl, "_service_by_name", return_value=None):
  91. self.assertFalse(launchctl.stop("job_label"))
  92. def test_start(self):
  93. """
  94. Test for Start the specified service
  95. """
  96. with patch.object(
  97. launchctl, "_service_by_name", return_value={"file_path": "A"}
  98. ):
  99. with patch.dict(
  100. launchctl.__salt__, {"cmd.retcode": MagicMock(return_value=False)}
  101. ):
  102. self.assertTrue(launchctl.start("job_label"))
  103. with patch.object(launchctl, "_service_by_name", return_value=None):
  104. self.assertFalse(launchctl.start("job_label"))
  105. def test_restart(self):
  106. """
  107. Test for Restart the named service
  108. """
  109. with patch.object(launchctl, "stop", return_value=None):
  110. with patch.object(launchctl, "start", return_value=True):
  111. self.assertTrue(launchctl.restart("job_label"))