1
0

test_syndic.py 5.0 KB

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