test_runner.py 5.7 KB

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