test_linux_service.py 7.0 KB

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