1
0

test_service.py 6.2 KB

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