test_service.py 4.1 KB

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