test_service.py 4.1 KB

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