test_service.py 6.6 KB

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