test_syndic.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Pedro Algarvio (pedro@algarvio.me)
  4. tests.integration.shell.syndic
  5. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  6. '''
  7. # Import python libs
  8. from __future__ import absolute_import
  9. import logging
  10. from collections import OrderedDict
  11. # Import Salt Testing libs
  12. from tests.support.case import ShellCase
  13. from tests.support.mixins import ShellCaseCommonTestsMixin
  14. from tests.support.unit import skipIf
  15. from tests.integration.utils import testprogram
  16. # Import salt libs
  17. import salt.utils.files
  18. import salt.utils.yaml
  19. import salt.utils.platform
  20. # Import 3rd-party libs
  21. import psutil
  22. #import pytest
  23. log = logging.getLogger(__name__)
  24. #@pytest.fixture(scope='module', autouse=True)
  25. def session_salt_syndic(request, session_salt_master_of_masters, session_salt_syndic):
  26. request.session.stats_processes.update(OrderedDict((
  27. ('Salt Syndic Master', psutil.Process(session_salt_master_of_masters.pid)),
  28. (' Salt Syndic', psutil.Process(session_salt_syndic.pid)),
  29. )).items())
  30. yield session_salt_syndic
  31. request.session.stats_processes.pop('Salt Syndic Master')
  32. request.session.stats_processes.pop(' Salt Syndic')
  33. # Stop daemons now(they would be stopped at the end of the test run session
  34. for daemon in (session_salt_syndic, session_salt_master_of_masters):
  35. try:
  36. daemon.terminate()
  37. except Exception as exc: # pylint: disable=broad-except
  38. log.warning('Failed to terminate daemon: %s', daemon.__class__.__name__)
  39. class SyndicTest(ShellCase, testprogram.TestProgramCase, ShellCaseCommonTestsMixin):
  40. '''
  41. Test the salt-syndic command
  42. '''
  43. _call_binary_ = 'salt-syndic'
  44. @skipIf(salt.utils.platform.is_windows(), 'Skip on Windows OS')
  45. def test_exit_status_unknown_user(self):
  46. '''
  47. Ensure correct exit status when the syndic is configured to run as an unknown user.
  48. Skipped on windows because daemonization not supported
  49. '''
  50. syndic = testprogram.TestDaemonSaltSyndic(
  51. name='unknown_user',
  52. config_base={'user': 'some_unknown_user_xyz'},
  53. parent_dir=self._test_dir,
  54. )
  55. # Call setup here to ensure config and script exist
  56. syndic.setup()
  57. stdout, stderr, status = syndic.run(
  58. args=['-d'],
  59. catch_stderr=True,
  60. with_retcode=True,
  61. )
  62. try:
  63. self.assert_exit_status(
  64. status, 'EX_NOUSER',
  65. message='unknown user not on system',
  66. stdout=stdout, stderr=stderr
  67. )
  68. finally:
  69. # Although the start-up should fail, call shutdown() to set the
  70. # internal _shutdown flag and avoid the registered atexit calls to
  71. # cause timeout exceptions and respective traceback
  72. syndic.shutdown()
  73. # pylint: disable=invalid-name
  74. @skipIf(salt.utils.platform.is_windows(), 'Skip on Windows OS')
  75. def test_exit_status_unknown_argument(self):
  76. '''
  77. Ensure correct exit status when an unknown argument is passed to salt-syndic.
  78. Skipped on windows because daemonization not supported
  79. '''
  80. syndic = testprogram.TestDaemonSaltSyndic(
  81. name='unknown_argument',
  82. parent_dir=self._test_dir,
  83. )
  84. # Syndic setup here to ensure config and script exist
  85. syndic.setup()
  86. stdout, stderr, status = syndic.run(
  87. args=['-d', '--unknown-argument'],
  88. catch_stderr=True,
  89. with_retcode=True,
  90. )
  91. try:
  92. self.assert_exit_status(
  93. status, 'EX_USAGE',
  94. message='unknown argument',
  95. stdout=stdout, stderr=stderr
  96. )
  97. finally:
  98. # Although the start-up should fail, call shutdown() to set the
  99. # internal _shutdown flag and avoid the registered atexit calls to
  100. # cause timeout exceptions and respective traceback
  101. syndic.shutdown()
  102. @skipIf(salt.utils.platform.is_windows(), 'Skip on Windows OS')
  103. def test_exit_status_correct_usage(self):
  104. '''
  105. Ensure correct exit status when salt-syndic starts correctly.
  106. Skipped on windows because daemonization not supported
  107. '''
  108. syndic = testprogram.TestDaemonSaltSyndic(
  109. name='correct_usage',
  110. parent_dir=self._test_dir,
  111. )
  112. # Syndic setup here to ensure config and script exist
  113. syndic.setup()
  114. stdout, stderr, status = syndic.run(
  115. args=['-d', '-l', 'debug'],
  116. catch_stderr=True,
  117. with_retcode=True,
  118. )
  119. try:
  120. self.assert_exit_status(
  121. status, 'EX_OK',
  122. message='correct usage',
  123. stdout=stdout, stderr=stderr
  124. )
  125. finally:
  126. syndic.shutdown(wait_for_orphans=3)