test_smf_service.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
  4. """
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. # Import Salt Libs
  8. import salt.modules.smf_service as smf
  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 SmfTestCase(TestCase, LoaderModuleMockMixin):
  14. """
  15. Test cases for salt.modules.smf
  16. """
  17. def setup_loader_modules(self):
  18. return {smf: {}}
  19. def test_get_running(self):
  20. """
  21. Test to return the running services
  22. """
  23. with patch.dict(
  24. smf.__salt__, {"cmd.run": MagicMock(return_value="A online\n")}
  25. ):
  26. self.assertEqual(smf.get_running(), ["A"])
  27. def test_get_stopped(self):
  28. """
  29. Test to return the stopped services
  30. """
  31. with patch.dict(smf.__salt__, {"cmd.run": MagicMock(return_value="A\n")}):
  32. self.assertListEqual(smf.get_stopped(), ["A"])
  33. def test_available(self):
  34. """
  35. Test to returns ``True`` if the specified service is available,
  36. otherwise returns ``False``.
  37. """
  38. with patch.dict(smf.__salt__, {"cmd.run": MagicMock(return_value="A")}):
  39. with patch.object(smf, "get_all", return_value=("A")):
  40. self.assertTrue(smf.available("A"))
  41. def test_missing(self):
  42. """
  43. The inverse of service.available.
  44. Returns ``True`` if the specified service is not available, otherwise
  45. returns ``False``.
  46. """
  47. with patch.dict(smf.__salt__, {"cmd.run": MagicMock(return_value="A")}):
  48. with patch.object(smf, "get_all", return_value=("A")):
  49. self.assertFalse(smf.missing("A"))
  50. def test_get_all(self):
  51. """
  52. Test to return all installed services
  53. """
  54. with patch.dict(smf.__salt__, {"cmd.run": MagicMock(return_value="A\n")}):
  55. self.assertListEqual(smf.get_all(), ["A"])
  56. def test_start(self):
  57. """
  58. Test to start the specified service
  59. """
  60. with patch.dict(
  61. smf.__salt__,
  62. {"cmd.retcode": MagicMock(side_effect=[False, 3, None, False, 4])},
  63. ):
  64. self.assertTrue(smf.start("name"))
  65. self.assertTrue(smf.start("name"))
  66. self.assertFalse(smf.start("name"))
  67. def test_stop(self):
  68. """
  69. Test to stop the specified service
  70. """
  71. with patch.dict(smf.__salt__, {"cmd.retcode": MagicMock(return_value=False)}):
  72. self.assertTrue(smf.stop("name"))
  73. def test_restart(self):
  74. """
  75. Test to restart the named service
  76. """
  77. with patch.dict(
  78. smf.__salt__, {"cmd.retcode": MagicMock(side_effect=[False, True])}
  79. ):
  80. with patch.object(smf, "start", return_value="A"):
  81. self.assertEqual(smf.restart("name"), "A")
  82. self.assertFalse(smf.restart("name"))
  83. def test_reload_(self):
  84. """
  85. Test to reload the named service
  86. """
  87. with patch.dict(
  88. smf.__salt__, {"cmd.retcode": MagicMock(side_effect=[False, True])}
  89. ):
  90. with patch.object(smf, "start", return_value="A"):
  91. self.assertEqual(smf.reload_("name"), "A")
  92. self.assertFalse(smf.reload_("name"))
  93. def test_status(self):
  94. """
  95. Test to return the status for a service, returns a bool whether the
  96. service is running.
  97. """
  98. with patch.dict(
  99. smf.__salt__, {"cmd.run": MagicMock(side_effect=["online", "online1"])}
  100. ):
  101. self.assertTrue(smf.status("name"))
  102. self.assertFalse(smf.status("name"))
  103. def test_enable(self):
  104. """
  105. Test to enable the named service to start at boot
  106. """
  107. with patch.dict(smf.__salt__, {"cmd.retcode": MagicMock(return_value=False)}):
  108. self.assertTrue(smf.enable("name"))
  109. def test_disable(self):
  110. """
  111. Test to disable the named service to start at boot
  112. """
  113. with patch.dict(smf.__salt__, {"cmd.retcode": MagicMock(return_value=False)}):
  114. self.assertTrue(smf.disable("name"))
  115. def test_enabled(self):
  116. """
  117. Test to check to see if the named service is enabled to start on boot
  118. """
  119. with patch.dict(
  120. smf.__salt__,
  121. {
  122. "cmd.run": MagicMock(
  123. side_effect=["fmri", "A B true", "fmri", "A B false"]
  124. )
  125. },
  126. ):
  127. self.assertTrue(smf.enabled("name"))
  128. self.assertFalse(smf.enabled("name"))
  129. def test_disabled(self):
  130. """
  131. Test to check to see if the named service is disabled to start on boot
  132. """
  133. with patch.object(smf, "enabled", return_value=False):
  134. self.assertTrue(smf.disabled("name"))
  135. def test_get_enabled(self):
  136. """
  137. Test to return the enabled services
  138. """
  139. with patch.object(smf, "_get_enabled_disabled", return_value=True):
  140. self.assertTrue(smf.get_enabled())
  141. def test_get_disabled(self):
  142. """
  143. Test to return the disabled services
  144. """
  145. with patch.object(smf, "_get_enabled_disabled", return_value=True):
  146. self.assertTrue(smf.get_disabled())