1
0

test_linux_service.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. # -*- coding: utf-8 -*-
  2. from __future__ import absolute_import, print_function, unicode_literals
  3. import pytest
  4. import salt.utils.path
  5. import salt.utils.platform
  6. import salt.utils.systemd
  7. from tests.support.case import ModuleCase
  8. from tests.support.helpers import destructiveTest, flaky, slowTest
  9. from tests.support.unit import skipIf
  10. @destructiveTest
  11. @pytest.mark.windows_whitelisted
  12. class ServiceModuleTest(ModuleCase):
  13. """
  14. Module testing the service module
  15. """
  16. def setUp(self):
  17. self.service_name = "cron"
  18. cmd_name = "crontab"
  19. os_family = self.run_function("grains.get", ["os_family"])
  20. os_release = self.run_function("grains.get", ["osrelease"])
  21. if os_family == "RedHat":
  22. if os_release[0] == "7":
  23. self.skipTest(
  24. "Disabled on CentOS 7 until we can fix SSH connection issues."
  25. )
  26. self.service_name = "crond"
  27. elif os_family == "Arch":
  28. self.service_name = "sshd"
  29. cmd_name = "systemctl"
  30. elif os_family == "NILinuxRT":
  31. self.service_name = "syslog"
  32. cmd_name = "syslog-ng"
  33. elif os_family == "MacOS":
  34. self.service_name = "org.ntp.ntpd"
  35. if int(os_release.split(".")[1]) >= 13:
  36. self.service_name = "com.apple.AirPlayXPCHelper"
  37. elif salt.utils.platform.is_windows():
  38. self.service_name = "Spooler"
  39. self.pre_srv_status = self.run_function("service.status", [self.service_name])
  40. self.pre_srv_enabled = (
  41. True
  42. if self.service_name in self.run_function("service.get_enabled")
  43. else False
  44. )
  45. if (
  46. salt.utils.path.which(cmd_name) is None
  47. and not salt.utils.platform.is_windows()
  48. ):
  49. self.skipTest("{0} is not installed".format(cmd_name))
  50. def tearDown(self):
  51. post_srv_status = self.run_function("service.status", [self.service_name])
  52. post_srv_enabled = (
  53. True
  54. if self.service_name in self.run_function("service.get_enabled")
  55. else False
  56. )
  57. if post_srv_status != self.pre_srv_status:
  58. if self.pre_srv_status:
  59. self.run_function("service.enable", [self.service_name])
  60. else:
  61. self.run_function("service.disable", [self.service_name])
  62. if post_srv_enabled != self.pre_srv_enabled:
  63. if self.pre_srv_enabled:
  64. self.run_function("service.enable", [self.service_name])
  65. else:
  66. self.run_function("service.disable", [self.service_name])
  67. del self.service_name
  68. @flaky
  69. @slowTest
  70. def test_service_status_running(self):
  71. """
  72. test service.status execution module
  73. when service is running
  74. """
  75. self.run_function("service.start", [self.service_name])
  76. check_service = self.run_function("service.status", [self.service_name])
  77. self.assertTrue(check_service)
  78. @slowTest
  79. def test_service_status_dead(self):
  80. """
  81. test service.status execution module
  82. when service is dead
  83. """
  84. self.run_function("service.stop", [self.service_name])
  85. check_service = self.run_function("service.status", [self.service_name])
  86. self.assertFalse(check_service)
  87. @slowTest
  88. def test_service_restart(self):
  89. """
  90. test service.restart
  91. """
  92. self.assertTrue(self.run_function("service.restart", [self.service_name]))
  93. @slowTest
  94. def test_service_enable(self):
  95. """
  96. test service.get_enabled and service.enable module
  97. """
  98. # disable service before test
  99. self.assertTrue(self.run_function("service.disable", [self.service_name]))
  100. self.assertTrue(self.run_function("service.enable", [self.service_name]))
  101. self.assertIn(self.service_name, self.run_function("service.get_enabled"))
  102. @slowTest
  103. def test_service_disable(self):
  104. """
  105. test service.get_disabled and service.disable module
  106. """
  107. # enable service before test
  108. self.assertTrue(self.run_function("service.enable", [self.service_name]))
  109. self.assertTrue(self.run_function("service.disable", [self.service_name]))
  110. if salt.utils.platform.is_darwin():
  111. self.assertTrue(self.run_function("service.disabled", [self.service_name]))
  112. else:
  113. self.assertIn(self.service_name, self.run_function("service.get_disabled"))
  114. @slowTest
  115. def test_service_disable_doesnot_exist(self):
  116. """
  117. test service.get_disabled and service.disable module
  118. when service name does not exist
  119. """
  120. # enable service before test
  121. srv_name = "doesnotexist"
  122. enable = self.run_function("service.enable", [srv_name])
  123. systemd = salt.utils.systemd.booted()
  124. # check service was not enabled
  125. try:
  126. self.assertFalse(enable)
  127. except AssertionError:
  128. self.assertIn("ERROR", enable)
  129. # check service was not disabled
  130. if (
  131. tuple(
  132. self.run_function("grains.item", ["osrelease_info"])["osrelease_info"]
  133. )
  134. == (14, 0o4)
  135. and not systemd
  136. ):
  137. # currently upstart does not have a mechanism to report if disabling a service fails if does not exist
  138. self.assertTrue(self.run_function("service.disable", [srv_name]))
  139. elif (
  140. self.run_function("grains.item", ["os"])["os"] == "Debian"
  141. and self.run_function("grains.item", ["osmajorrelease"])["osmajorrelease"]
  142. < 9
  143. and systemd
  144. ):
  145. # currently disabling a service via systemd that does not exist
  146. # on Debian 8 results in a True return code
  147. self.assertTrue(self.run_function("service.disable", [srv_name]))
  148. else:
  149. try:
  150. disable = self.run_function("service.disable", [srv_name])
  151. self.assertFalse(disable)
  152. except AssertionError:
  153. self.assertTrue("error" in disable.lower())
  154. if salt.utils.platform.is_darwin():
  155. self.assertFalse(self.run_function("service.disabled", [srv_name]))
  156. else:
  157. self.assertNotIn(srv_name, self.run_function("service.get_disabled"))
  158. @skipIf(not salt.utils.platform.is_windows(), "Windows Only Test")
  159. @slowTest
  160. def test_service_get_service_name(self):
  161. """
  162. test service.get_service_name
  163. """
  164. ret = self.run_function("service.get_service_name")
  165. self.assertIn(self.service_name, ret.values())