1
0

test_service.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 Salt Testing libs
  9. from tests.support.case import ModuleCase
  10. from tests.support.helpers import destructiveTest
  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. @destructiveTest
  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 = True if self.service_name in self.run_function('service.get_enabled') else False
  42. self.post_srv_disable = False
  43. if not self.pre_srv_enabled:
  44. self.run_function('service.enable', name=self.service_name)
  45. self.post_srv_disable = True
  46. if os_family != 'Windows' and salt.utils.path.which(cmd_name) is None:
  47. self.skipTest('{0} is not installed'.format(cmd_name))
  48. def tearDown(self):
  49. if self.post_srv_disable:
  50. self.run_function('service.disable', name=self.service_name)
  51. def check_service_status(self, exp_return):
  52. '''
  53. helper method to check status of service
  54. '''
  55. check_status = self.run_function('service.status',
  56. name=self.service_name)
  57. try:
  58. if not re.match(exp_return, check_status):
  59. self.fail('status of service is not returning correctly')
  60. except TypeError:
  61. if check_status is not exp_return:
  62. self.fail('status of service is not returning correctly')
  63. def test_service_running(self):
  64. '''
  65. test service.running state module
  66. '''
  67. if self.run_function('service.status', name=self.service_name):
  68. stop_service = self.run_function('service.stop', name=self.service_name)
  69. self.assertTrue(stop_service)
  70. self.check_service_status(self.stopped)
  71. if salt.utils.platform.is_darwin():
  72. # make sure the service is enabled on macosx
  73. enable = self.run_function('service.enable', name=self.service_name)
  74. start_service = self.run_state('service.running',
  75. name=self.service_name)
  76. self.assertTrue(start_service)
  77. self.check_service_status(self.running)
  78. def test_service_dead(self):
  79. '''
  80. test service.dead state module
  81. '''
  82. start_service = self.run_state('service.running',
  83. name=self.service_name)
  84. self.assertSaltTrueReturn(start_service)
  85. self.check_service_status(self.running)
  86. ret = self.run_state('service.dead', name=self.service_name)
  87. self.assertSaltTrueReturn(ret)
  88. self.check_service_status(self.stopped)
  89. def test_service_dead_init_delay(self):
  90. '''
  91. test service.dead state module with init_delay arg
  92. '''
  93. start_service = self.run_state('service.running',
  94. name=self.service_name)
  95. self.assertSaltTrueReturn(start_service)
  96. self.check_service_status(self.running)
  97. ret = self.run_state('service.dead', name=self.service_name,
  98. init_delay=INIT_DELAY)
  99. self.assertSaltTrueReturn(ret)
  100. self.check_service_status(self.stopped)