1
0

test_runner.py 5.7 KB

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