test_runner.py 5.7 KB

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