test_s6.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Marek Skrobacki <skrobul@skrobul.com>
  4. '''
  5. # Import Python Libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import os
  8. # Import Salt Testing Libs
  9. from tests.support.mixins import LoaderModuleMockMixin
  10. from tests.support.unit import TestCase
  11. from tests.support.mock import (
  12. MagicMock,
  13. patch,
  14. )
  15. # Import Salt Libs
  16. import salt.modules.s6 as s6
  17. class S6TestCase(TestCase, LoaderModuleMockMixin):
  18. '''
  19. Test cases for salt.modules.s6
  20. '''
  21. def setup_loader_modules(self):
  22. return {s6: {'SERVICE_DIR': '/etc/service'}}
  23. # 'start' function tests: 1
  24. def test_start(self):
  25. '''
  26. Test if it starts service via s6-svc.
  27. '''
  28. mock_ret = MagicMock(return_value=False)
  29. with patch.dict(s6.__salt__, {'cmd.retcode': mock_ret}):
  30. self.assertTrue(s6.start('ssh'))
  31. # 'stop' function tests: 1
  32. def test_stop(self):
  33. '''
  34. Test if it stops service via s6.
  35. '''
  36. mock_ret = MagicMock(return_value=False)
  37. with patch.dict(s6.__salt__, {'cmd.retcode': mock_ret}):
  38. self.assertTrue(s6.stop('ssh'))
  39. # 'term' function tests: 1
  40. def test_term(self):
  41. '''
  42. Test if it send a TERM to service via s6.
  43. '''
  44. mock_ret = MagicMock(return_value=False)
  45. with patch.dict(s6.__salt__, {'cmd.retcode': mock_ret}):
  46. self.assertTrue(s6.term('ssh'))
  47. # 'reload_' function tests: 1
  48. def test_reload(self):
  49. '''
  50. Test if it send a HUP to service via s6.
  51. '''
  52. mock_ret = MagicMock(return_value=False)
  53. with patch.dict(s6.__salt__, {'cmd.retcode': mock_ret}):
  54. self.assertTrue(s6.reload_('ssh'))
  55. # 'restart' function tests: 1
  56. def test_restart(self):
  57. '''
  58. Test if it restart service via s6. This will stop/start service.
  59. '''
  60. mock_ret = MagicMock(return_value=False)
  61. with patch.dict(s6.__salt__, {'cmd.retcode': mock_ret}):
  62. self.assertTrue(s6.restart('ssh'))
  63. # 'full_restart' function tests: 1
  64. def test_full_restart(self):
  65. '''
  66. Test if it calls s6.restart() function.
  67. '''
  68. mock_ret = MagicMock(return_value=False)
  69. with patch.dict(s6.__salt__, {'cmd.retcode': mock_ret}):
  70. self.assertIsNone(s6.full_restart('ssh'))
  71. # 'status' function tests: 1
  72. def test_status(self):
  73. '''
  74. Test if it return the status for a service via s6,
  75. return pid if running.
  76. '''
  77. mock_run = MagicMock(return_value='salt')
  78. with patch.dict(s6.__salt__, {'cmd.run_stdout': mock_run}):
  79. self.assertEqual(s6.status('ssh'), '')
  80. # 'available' function tests: 1
  81. def test_available(self):
  82. '''
  83. Test if it returns ``True`` if the specified service is available,
  84. otherwise returns ``False``.
  85. '''
  86. with patch.object(os, 'listdir',
  87. MagicMock(return_value=['/etc/service'])):
  88. self.assertTrue(s6.available('/etc/service'))
  89. # 'missing' function tests: 1
  90. def test_missing(self):
  91. '''
  92. Test if it returns ``True`` if the specified service is not available,
  93. otherwise returns ``False``.
  94. '''
  95. with patch.object(os, 'listdir',
  96. MagicMock(return_value=['/etc/service'])):
  97. self.assertTrue(s6.missing('foo'))
  98. # 'get_all' function tests: 1
  99. def test_get_all(self):
  100. '''
  101. Test if it return a list of all available services.
  102. '''
  103. with patch.object(os, 'listdir',
  104. MagicMock(return_value=['/etc/service'])):
  105. self.assertListEqual(s6.get_all(), ['/etc/service'])