test_runner.py 6.2 KB

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