test_service.py 6.3 KB

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