1
0

test_service.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. # -*- coding: utf-8 -*-
  2. # Import Python libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. # Import Salt Testing libs
  5. from tests.support.case import ModuleCase
  6. from tests.support.helpers import destructiveTest, flaky
  7. from tests.support.unit import skipIf
  8. # Import Salt libs
  9. import salt.utils.path
  10. import salt.utils.platform
  11. import salt.utils.systemd
  12. @destructiveTest
  13. class ServiceModuleTest(ModuleCase):
  14. '''
  15. Module testing the service module
  16. '''
  17. def setUp(self):
  18. self.service_name = 'cron'
  19. cmd_name = 'crontab'
  20. os_family = self.run_function('grains.get', ['os_family'])
  21. os_release = self.run_function('grains.get', ['osrelease'])
  22. if os_family == 'RedHat':
  23. if os_release[0] == '7':
  24. self.skipTest('Disabled on CentOS 7 until we can fix SSH connection issues.')
  25. self.service_name = 'crond'
  26. elif os_family == 'Arch':
  27. self.service_name = 'sshd'
  28. cmd_name = 'systemctl'
  29. elif os_family == 'MacOS':
  30. self.service_name = 'org.ntp.ntpd'
  31. if int(os_release.split('.')[1]) >= 13:
  32. self.service_name = 'com.apple.AirPlayXPCHelper'
  33. elif salt.utils.platform.is_windows():
  34. self.service_name = 'Spooler'
  35. self.pre_srv_status = self.run_function('service.status', [self.service_name])
  36. self.pre_srv_enabled = True if self.service_name in self.run_function('service.get_enabled') else False
  37. if salt.utils.path.which(cmd_name) is None and not salt.utils.platform.is_windows():
  38. self.skipTest('{0} is not installed'.format(cmd_name))
  39. def tearDown(self):
  40. post_srv_status = self.run_function('service.status', [self.service_name])
  41. post_srv_enabled = True if self.service_name in self.run_function('service.get_enabled') else False
  42. if post_srv_status != self.pre_srv_status:
  43. if self.pre_srv_status:
  44. self.run_function('service.enable', [self.service_name])
  45. else:
  46. self.run_function('service.disable', [self.service_name])
  47. if post_srv_enabled != self.pre_srv_enabled:
  48. if self.pre_srv_enabled:
  49. self.run_function('service.enable', [self.service_name])
  50. else:
  51. self.run_function('service.disable', [self.service_name])
  52. del self.service_name
  53. @flaky
  54. def test_service_status_running(self):
  55. '''
  56. test service.status execution module
  57. when service is running
  58. '''
  59. self.run_function('service.start', [self.service_name])
  60. check_service = self.run_function('service.status', [self.service_name])
  61. self.assertTrue(check_service)
  62. def test_service_status_dead(self):
  63. '''
  64. test service.status execution module
  65. when service is dead
  66. '''
  67. self.run_function('service.stop', [self.service_name])
  68. check_service = self.run_function('service.status', [self.service_name])
  69. self.assertFalse(check_service)
  70. def test_service_restart(self):
  71. '''
  72. test service.restart
  73. '''
  74. self.assertTrue(self.run_function('service.restart', [self.service_name]))
  75. def test_service_enable(self):
  76. '''
  77. test service.get_enabled and service.enable module
  78. '''
  79. # disable service before test
  80. self.assertTrue(self.run_function('service.disable', [self.service_name]))
  81. self.assertTrue(self.run_function('service.enable', [self.service_name]))
  82. self.assertIn(self.service_name, self.run_function('service.get_enabled'))
  83. def test_service_disable(self):
  84. '''
  85. test service.get_disabled and service.disable module
  86. '''
  87. # enable service before test
  88. self.assertTrue(self.run_function('service.enable', [self.service_name]))
  89. self.assertTrue(self.run_function('service.disable', [self.service_name]))
  90. if salt.utils.platform.is_darwin():
  91. self.assertTrue(self.run_function('service.disabled', [self.service_name]))
  92. else:
  93. self.assertIn(self.service_name, self.run_function('service.get_disabled'))
  94. def test_service_disable_doesnot_exist(self):
  95. '''
  96. test service.get_disabled and service.disable module
  97. when service name does not exist
  98. '''
  99. # enable service before test
  100. srv_name = 'doesnotexist'
  101. enable = self.run_function('service.enable', [srv_name])
  102. systemd = salt.utils.systemd.booted()
  103. # check service was not enabled
  104. try:
  105. self.assertFalse(enable)
  106. except AssertionError:
  107. self.assertIn('ERROR', enable)
  108. # check service was not disabled
  109. if tuple(self.run_function('grains.item', ['osrelease_info'])['osrelease_info']) == (14, 0o4) and not systemd:
  110. # currently upstart does not have a mechanism to report if disabling a service fails if does not exist
  111. self.assertTrue(self.run_function('service.disable', [srv_name]))
  112. elif self.run_function('grains.item', ['os'])['os'] == 'Debian' and \
  113. self.run_function('grains.item', ['osmajorrelease'])['osmajorrelease'] < 9 and systemd:
  114. # currently disabling a service via systemd that does not exist
  115. # on Debian 8 results in a True return code
  116. self.assertTrue(self.run_function('service.disable', [srv_name]))
  117. else:
  118. try:
  119. disable = self.run_function('service.disable', [srv_name])
  120. self.assertFalse(disable)
  121. except AssertionError:
  122. self.assertTrue('error' in disable.lower())
  123. if salt.utils.platform.is_darwin():
  124. self.assertFalse(self.run_function('service.disabled', [srv_name]))
  125. else:
  126. self.assertNotIn(srv_name, self.run_function('service.get_disabled'))
  127. @skipIf(not salt.utils.platform.is_windows(), 'Windows Only Test')
  128. def test_service_get_service_name(self):
  129. '''
  130. test service.get_service_name
  131. '''
  132. ret = self.run_function('service.get_service_name')
  133. self.assertIn(self.service_name, ret.values())