test_service.py 4.1 KB

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