1
0

test_syndic.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. @pytest.mark.windows_whitelisted
  40. class SyndicTest(ShellCase, testprogram.TestProgramCase, ShellCaseCommonTestsMixin):
  41. '''
  42. Test the salt-syndic command
  43. '''
  44. _call_binary_ = 'salt-syndic'
  45. @skipIf(salt.utils.platform.is_windows(), 'Skip on Windows OS')
  46. def test_exit_status_unknown_user(self):
  47. '''
  48. Ensure correct exit status when the syndic is configured to run as an unknown user.
  49. Skipped on windows because daemonization not supported
  50. '''
  51. syndic = testprogram.TestDaemonSaltSyndic(
  52. name='unknown_user',
  53. config_base={'user': 'some_unknown_user_xyz'},
  54. parent_dir=self._test_dir,
  55. )
  56. # Call setup here to ensure config and script exist
  57. syndic.setup()
  58. stdout, stderr, status = syndic.run(
  59. args=['-d'],
  60. catch_stderr=True,
  61. with_retcode=True,
  62. )
  63. try:
  64. self.assert_exit_status(
  65. status, 'EX_NOUSER',
  66. message='unknown user not on system',
  67. stdout=stdout, stderr=stderr
  68. )
  69. finally:
  70. # Although the start-up should fail, call shutdown() to set the
  71. # internal _shutdown flag and avoid the registered atexit calls to
  72. # cause timeout exceptions and respective traceback
  73. syndic.shutdown()
  74. # pylint: disable=invalid-name
  75. @skipIf(salt.utils.platform.is_windows(), 'Skip on Windows OS')
  76. def test_exit_status_unknown_argument(self):
  77. '''
  78. Ensure correct exit status when an unknown argument is passed to salt-syndic.
  79. Skipped on windows because daemonization not supported
  80. '''
  81. syndic = testprogram.TestDaemonSaltSyndic(
  82. name='unknown_argument',
  83. parent_dir=self._test_dir,
  84. )
  85. # Syndic setup here to ensure config and script exist
  86. syndic.setup()
  87. stdout, stderr, status = syndic.run(
  88. args=['-d', '--unknown-argument'],
  89. catch_stderr=True,
  90. with_retcode=True,
  91. )
  92. try:
  93. self.assert_exit_status(
  94. status, 'EX_USAGE',
  95. message='unknown argument',
  96. stdout=stdout, stderr=stderr
  97. )
  98. finally:
  99. # Although the start-up should fail, call shutdown() to set the
  100. # internal _shutdown flag and avoid the registered atexit calls to
  101. # cause timeout exceptions and respective traceback
  102. syndic.shutdown()
  103. @skipIf(salt.utils.platform.is_windows(), 'Skip on Windows OS')
  104. def test_exit_status_correct_usage(self):
  105. '''
  106. Ensure correct exit status when salt-syndic starts correctly.
  107. Skipped on windows because daemonization not supported
  108. '''
  109. syndic = testprogram.TestDaemonSaltSyndic(
  110. name='correct_usage',
  111. parent_dir=self._test_dir,
  112. )
  113. # Syndic setup here to ensure config and script exist
  114. syndic.setup()
  115. stdout, stderr, status = syndic.run(
  116. args=['-d', '-l', 'debug'],
  117. catch_stderr=True,
  118. with_retcode=True,
  119. )
  120. try:
  121. self.assert_exit_status(
  122. status, 'EX_OK',
  123. message='correct usage',
  124. stdout=stdout, stderr=stderr
  125. )
  126. finally:
  127. syndic.shutdown(wait_for_orphans=3)