test_service.py 6.2 KB

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