test_service.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. """
  2. Tests for the service state
  3. """
  4. import re
  5. import pytest
  6. import salt.utils.path
  7. import salt.utils.platform
  8. from tests.support.case import ModuleCase
  9. from tests.support.helpers import destructiveTest, slowTest
  10. from tests.support.mixins import SaltReturnAssertsMixin
  11. INIT_DELAY = 5
  12. @destructiveTest
  13. @pytest.mark.windows_whitelisted
  14. class ServiceTest(ModuleCase, SaltReturnAssertsMixin):
  15. """
  16. Validate the service state
  17. """
  18. def setUp(self):
  19. self.service_name = "cron"
  20. cmd_name = "crontab"
  21. os_family = self.run_function("grains.get", ["os_family"])
  22. os_release = self.run_function("grains.get", ["osrelease"])
  23. self.stopped = False
  24. self.running = True
  25. if os_family == "RedHat":
  26. self.service_name = "crond"
  27. elif os_family == "Arch":
  28. self.service_name = "sshd"
  29. cmd_name = "systemctl"
  30. elif os_family == "MacOS":
  31. self.service_name = "org.ntp.ntpd"
  32. if int(os_release.split(".")[1]) >= 13:
  33. self.service_name = "com.apple.AirPlayXPCHelper"
  34. elif os_family == "Windows":
  35. self.service_name = "Spooler"
  36. self.pre_srv_enabled = (
  37. True
  38. if self.service_name in self.run_function("service.get_enabled")
  39. else False
  40. )
  41. self.post_srv_disable = False
  42. if not self.pre_srv_enabled:
  43. self.run_function("service.enable", name=self.service_name)
  44. self.post_srv_disable = True
  45. if os_family != "Windows" and salt.utils.path.which(cmd_name) is None:
  46. self.skipTest("{} is not installed".format(cmd_name))
  47. def tearDown(self):
  48. if self.post_srv_disable:
  49. self.run_function("service.disable", name=self.service_name)
  50. def check_service_status(self, exp_return):
  51. """
  52. helper method to check status of service
  53. """
  54. check_status = self.run_function("service.status", name=self.service_name)
  55. try:
  56. if not re.match(exp_return, check_status):
  57. self.fail("status of service is not returning correctly")
  58. except TypeError:
  59. if check_status is not exp_return:
  60. self.fail("status of service is not returning correctly")
  61. @slowTest
  62. def test_service_running(self):
  63. """
  64. test service.running state module
  65. """
  66. if self.run_function("service.status", name=self.service_name):
  67. stop_service = self.run_function("service.stop", name=self.service_name)
  68. self.assertTrue(stop_service)
  69. self.check_service_status(self.stopped)
  70. if salt.utils.platform.is_darwin():
  71. # make sure the service is enabled on macosx
  72. enable = self.run_function("service.enable", name=self.service_name)
  73. start_service = self.run_state("service.running", name=self.service_name)
  74. self.assertTrue(start_service)
  75. self.check_service_status(self.running)
  76. @slowTest
  77. def test_service_dead(self):
  78. """
  79. test service.dead state module
  80. """
  81. start_service = self.run_state("service.running", name=self.service_name)
  82. self.assertSaltTrueReturn(start_service)
  83. self.check_service_status(self.running)
  84. ret = self.run_state("service.dead", name=self.service_name)
  85. self.assertSaltTrueReturn(ret)
  86. self.check_service_status(self.stopped)
  87. @slowTest
  88. def test_service_dead_init_delay(self):
  89. """
  90. test service.dead state module with init_delay arg
  91. """
  92. start_service = self.run_state("service.running", name=self.service_name)
  93. self.assertSaltTrueReturn(start_service)
  94. self.check_service_status(self.running)
  95. ret = self.run_state(
  96. "service.dead", name=self.service_name, init_delay=INIT_DELAY
  97. )
  98. self.assertSaltTrueReturn(ret)
  99. self.check_service_status(self.stopped)