test_service.py 4.2 KB

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