1
0

test_runner.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. # -*- coding: utf-8 -*-
  2. """
  3. Tests for the salt-run command
  4. """
  5. from __future__ import absolute_import
  6. import pytest
  7. import salt.utils.files
  8. import salt.utils.platform
  9. import salt.utils.yaml
  10. from tests.integration.utils import testprogram
  11. from tests.support.case import ShellCase
  12. from tests.support.helpers import skip_if_not_root, slowTest
  13. from tests.support.mixins import ShellCaseCommonTestsMixin
  14. USERA = "saltdev"
  15. USERA_PWD = "saltdev"
  16. HASHED_USERA_PWD = "$6$SALTsalt$ZZFD90fKFWq8AGmmX0L3uBtS9fXL62SrTk5zcnQ6EkD6zoiM3kB88G1Zvs0xm/gZ7WXJRs5nsTBybUvGSqZkT."
  17. @pytest.mark.windows_whitelisted
  18. class RunTest(ShellCase, testprogram.TestProgramCase, ShellCaseCommonTestsMixin):
  19. """
  20. Test the salt-run command
  21. """
  22. _call_binary_ = "salt-run"
  23. def _add_user(self):
  24. """
  25. helper method to add user
  26. """
  27. try:
  28. add_user = self.run_call("user.add {0} createhome=False".format(USERA))
  29. add_pwd = self.run_call(
  30. "shadow.set_password {0} '{1}'".format(
  31. USERA,
  32. USERA_PWD if salt.utils.platform.is_darwin() else HASHED_USERA_PWD,
  33. )
  34. )
  35. self.assertTrue(add_user)
  36. self.assertTrue(add_pwd)
  37. user_list = self.run_call("user.list_users")
  38. self.assertIn(USERA, str(user_list))
  39. except AssertionError:
  40. self.run_call("user.delete {0} remove=True".format(USERA))
  41. self.skipTest("Could not add user or password, skipping test")
  42. def _remove_user(self):
  43. """
  44. helper method to remove user
  45. """
  46. user_list = self.run_call("user.list_users")
  47. for user in user_list:
  48. if USERA in user:
  49. self.run_call("user.delete {0} remove=True".format(USERA))
  50. @slowTest
  51. def test_in_docs(self):
  52. """
  53. test the salt-run docs system
  54. """
  55. data = self.run_run("-d")
  56. data = "\n".join(data)
  57. self.assertIn("jobs.active:", data)
  58. self.assertIn("jobs.list_jobs:", data)
  59. self.assertIn("jobs.lookup_jid:", data)
  60. self.assertIn("manage.down:", data)
  61. self.assertIn("manage.up:", data)
  62. self.assertIn("network.wol:", data)
  63. self.assertIn("network.wollist:", data)
  64. @slowTest
  65. def test_notin_docs(self):
  66. """
  67. Verify that hidden methods are not in run docs
  68. """
  69. data = self.run_run("-d")
  70. data = "\n".join(data)
  71. self.assertNotIn("jobs.SaltException:", data)
  72. @slowTest
  73. def test_salt_documentation_too_many_arguments(self):
  74. """
  75. Test to see if passing additional arguments shows an error
  76. """
  77. data = self.run_run("-d virt.list foo", catch_stderr=True)
  78. self.assertIn(
  79. "You can only get documentation for one method at one time",
  80. "\n".join(data[1]),
  81. )
  82. @slowTest
  83. def test_exit_status_unknown_argument(self):
  84. """
  85. Ensure correct exit status when an unknown argument is passed to salt-run.
  86. """
  87. runner = testprogram.TestProgramSaltRun(
  88. name="run-unknown_argument", parent_dir=self._test_dir,
  89. )
  90. # Call setup here to ensure config and script exist
  91. runner.setup()
  92. stdout, stderr, status = runner.run(
  93. args=["--unknown-argument"], catch_stderr=True, with_retcode=True,
  94. )
  95. self.assert_exit_status(
  96. status, "EX_USAGE", message="unknown argument", stdout=stdout, stderr=stderr
  97. )
  98. # runner.shutdown() should be unnecessary since the start-up should fail
  99. @slowTest
  100. def test_exit_status_correct_usage(self):
  101. """
  102. Ensure correct exit status when salt-run starts correctly.
  103. """
  104. runner = testprogram.TestProgramSaltRun(
  105. name="run-correct_usage", parent_dir=self._test_dir,
  106. )
  107. # Call setup here to ensure config and script exist
  108. runner.setup()
  109. stdout, stderr, status = runner.run(catch_stderr=True, with_retcode=True,)
  110. self.assert_exit_status(
  111. status, "EX_OK", message="correct usage", stdout=stdout, stderr=stderr
  112. )
  113. @skip_if_not_root
  114. @slowTest
  115. def test_salt_run_with_eauth_all_args(self):
  116. """
  117. test salt-run with eauth
  118. tests all eauth args
  119. """
  120. args = ["--auth", "--eauth", "--external-auth", "-a"]
  121. self._add_user()
  122. for arg in args:
  123. run_cmd = self.run_run(
  124. "{0} pam --username {1} --password {2}\
  125. test.arg arg kwarg=kwarg1".format(
  126. arg, USERA, USERA_PWD
  127. )
  128. )
  129. expect = [
  130. "args:",
  131. " - arg",
  132. "kwargs:",
  133. " ----------",
  134. " kwarg:",
  135. " kwarg1",
  136. ]
  137. self.assertEqual(expect, run_cmd)
  138. self._remove_user()
  139. @skip_if_not_root
  140. @slowTest
  141. def test_salt_run_with_eauth_bad_passwd(self):
  142. """
  143. test salt-run with eauth and bad password
  144. """
  145. self._add_user()
  146. run_cmd = self.run_run(
  147. "-a pam --username {0} --password wrongpassword\
  148. test.arg arg kwarg=kwarg1".format(
  149. USERA
  150. )
  151. )
  152. expect = ['Authentication failure of type "eauth" occurred for user saltdev.']
  153. self.assertEqual(expect, run_cmd)
  154. self._remove_user()
  155. @slowTest
  156. def test_salt_run_with_wrong_eauth(self):
  157. """
  158. test salt-run with wrong eauth parameter
  159. """
  160. run_cmd = self.run_run(
  161. "-a wrongeauth --username {0} --password {1}\
  162. test.arg arg kwarg=kwarg1".format(
  163. USERA, USERA_PWD
  164. )
  165. )
  166. expect = r"^The specified external authentication system \"wrongeauth\" is not available\tAvailable eauth types: auto, .*"
  167. self.assertRegex("\t".join(run_cmd), expect)