1
0

test_call.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Pedro Algarvio (pedro@algarvio.me)
  4. tests.integration.shell.call
  5. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  6. '''
  7. # Import python libs
  8. from __future__ import absolute_import
  9. import logging
  10. import os
  11. import re
  12. import shutil
  13. import sys
  14. # Import Salt Testing libs
  15. from tests.support.runtime import RUNTIME_VARS
  16. from tests.support.case import ShellCase
  17. from tests.support.unit import skipIf
  18. from tests.support.mixins import ShellCaseCommonTestsMixin
  19. from tests.support.helpers import flaky, with_tempfile
  20. from tests.integration.utils import testprogram
  21. # Import salt libs
  22. import salt.utils.files
  23. import salt.utils.json
  24. import salt.utils.platform
  25. import salt.utils.yaml
  26. from salt.ext import six
  27. log = logging.getLogger(__name__)
  28. class CallTest(ShellCase, testprogram.TestProgramCase, ShellCaseCommonTestsMixin):
  29. _call_binary_ = 'salt-call'
  30. def test_default_output(self):
  31. out = self.run_call('-l quiet test.fib 3')
  32. expect = ['local:',
  33. ' - 2']
  34. self.assertEqual(expect, out[:-1])
  35. def test_text_output(self):
  36. out = self.run_call('-l quiet --out txt test.fib 3')
  37. expect = [
  38. 'local: (2'
  39. ]
  40. self.assertEqual(''.join(expect), ''.join(out).rsplit(",", 1)[0])
  41. def test_json_out_indent(self):
  42. out = self.run_call('test.ping -l quiet --out=json --out-indent=-1')
  43. self.assertIn('"local": true', ''.join(out))
  44. out = self.run_call('test.ping -l quiet --out=json --out-indent=0')
  45. self.assertIn('"local": true', ''.join(out))
  46. out = self.run_call('test.ping -l quiet --out=json --out-indent=1')
  47. self.assertIn('"local": true', ''.join(out))
  48. def test_local_sls_call(self):
  49. fileroot = os.path.join(RUNTIME_VARS.FILES, 'file', 'base')
  50. out = self.run_call('--file-root {0} state.sls saltcalllocal'.format(fileroot), local=True)
  51. self.assertIn('Name: test.echo', ''.join(out))
  52. self.assertIn('Result: True', ''.join(out))
  53. self.assertIn('hello', ''.join(out))
  54. self.assertIn('Succeeded: 1', ''.join(out))
  55. @with_tempfile()
  56. def test_local_salt_call(self, name):
  57. '''
  58. This tests to make sure that salt-call does not execute the
  59. function twice, see https://github.com/saltstack/salt/pull/49552
  60. '''
  61. def _run_call(cmd):
  62. cmd = '--out=json ' + cmd
  63. return salt.utils.json.loads(''.join(self.run_call(cmd, local=True)))['local']
  64. ret = _run_call('state.single file.append name={0} text="foo"'.format(name))
  65. ret = ret[next(iter(ret))]
  66. # Make sure we made changes
  67. assert ret['changes']
  68. # 2nd sanity check: make sure that "foo" only exists once in the file
  69. with salt.utils.files.fopen(name) as fp_:
  70. contents = fp_.read()
  71. assert contents.count('foo') == 1, contents
  72. @skipIf(salt.utils.platform.is_windows() or salt.utils.platform.is_darwin(), 'This test requires a supported master')
  73. def test_user_delete_kw_output(self):
  74. ret = self.run_call('-l quiet -d user.delete')
  75. assert 'salt \'*\' user.delete name remove=True force=True' in ''.join(ret)
  76. def test_salt_documentation_too_many_arguments(self):
  77. '''
  78. Test to see if passing additional arguments shows an error
  79. '''
  80. data = self.run_call('-d virtualenv.create /tmp/ve', catch_stderr=True)
  81. self.assertIn('You can only get documentation for one method at one time', '\n'.join(data[1]))
  82. def test_issue_6973_state_highstate_exit_code(self):
  83. '''
  84. If there is no tops/master_tops or state file matches
  85. for this minion, salt-call should exit non-zero if invoked with
  86. option --retcode-passthrough
  87. '''
  88. src = os.path.join(RUNTIME_VARS.BASE_FILES, 'top.sls')
  89. dst = os.path.join(RUNTIME_VARS.BASE_FILES, 'top.sls.bak')
  90. shutil.move(src, dst)
  91. expected_comment = 'No states found for this minion'
  92. try:
  93. stdout, retcode = self.run_call(
  94. '-l quiet --retcode-passthrough state.highstate',
  95. with_retcode=True
  96. )
  97. finally:
  98. shutil.move(dst, src)
  99. self.assertIn(expected_comment, ''.join(stdout))
  100. self.assertNotEqual(0, retcode)
  101. @skipIf(sys.platform.startswith('win'), 'This test does not apply on Win')
  102. @skipIf(True, 'to be re-enabled when #23623 is merged')
  103. def test_return(self):
  104. self.run_call('cmd.run "echo returnTOmaster"')
  105. jobs = [a for a in self.run_run('jobs.list_jobs')]
  106. self.assertTrue(True in ['returnTOmaster' in j for j in jobs])
  107. # lookback jid
  108. first_match = [(i, j)
  109. for i, j in enumerate(jobs)
  110. if 'returnTOmaster' in j][0]
  111. jid, idx = None, first_match[0]
  112. while idx > 0:
  113. jid = re.match("([0-9]+):", jobs[idx])
  114. if jid:
  115. jid = jid.group(1)
  116. break
  117. idx -= 1
  118. assert idx > 0
  119. assert jid
  120. master_out = [
  121. a for a in self.run_run('jobs.lookup_jid {0}'.format(jid))
  122. ]
  123. self.assertTrue(True in ['returnTOmaster' in a for a in master_out])
  124. @skipIf(salt.utils.platform.is_windows(), 'Skip on Windows')
  125. def test_syslog_file_not_found(self):
  126. '''
  127. test when log_file is set to a syslog file that does not exist
  128. '''
  129. old_cwd = os.getcwd()
  130. config_dir = os.path.join(RUNTIME_VARS.TMP, 'log_file_incorrect')
  131. if not os.path.isdir(config_dir):
  132. os.makedirs(config_dir)
  133. os.chdir(config_dir)
  134. with salt.utils.files.fopen(self.get_config_file_path('minion'), 'r') as fh_:
  135. minion_config = salt.utils.yaml.load(fh_.read())
  136. minion_config['log_file'] = 'file:///dev/doesnotexist'
  137. with salt.utils.files.fopen(os.path.join(config_dir, 'minion'), 'w') as fh_:
  138. fh_.write(
  139. salt.utils.yaml.dump(minion_config, default_flow_style=False)
  140. )
  141. ret = self.run_script(
  142. 'salt-call',
  143. '--config-dir {0} cmd.run "echo foo"'.format(
  144. config_dir
  145. ),
  146. timeout=120,
  147. catch_stderr=True,
  148. with_retcode=True
  149. )
  150. try:
  151. if sys.version_info >= (3, 5, 4):
  152. self.assertIn('local:', ret[0])
  153. self.assertIn('[WARNING ] The log_file does not exist. Logging not setup correctly or syslog service not started.', ret[1])
  154. self.assertEqual(ret[2], 0)
  155. else:
  156. self.assertIn(
  157. 'Failed to setup the Syslog logging handler', '\n'.join(ret[1])
  158. )
  159. self.assertEqual(ret[2], 2)
  160. finally:
  161. self.chdir(old_cwd)
  162. if os.path.isdir(config_dir):
  163. shutil.rmtree(config_dir)
  164. @skipIf(True, 'This test is unreliable. Need to investigate why more deeply.')
  165. @flaky
  166. def test_issue_15074_output_file_append(self):
  167. output_file_append = os.path.join(RUNTIME_VARS.TMP, 'issue-15074')
  168. try:
  169. # Let's create an initial output file with some data
  170. _ = self.run_script(
  171. 'salt-call',
  172. '-c {0} --output-file={1} test.versions'.format(
  173. self.config_dir,
  174. output_file_append
  175. ),
  176. catch_stderr=True,
  177. with_retcode=True
  178. )
  179. with salt.utils.files.fopen(output_file_append) as ofa:
  180. output = ofa.read()
  181. self.run_script(
  182. 'salt-call',
  183. '-c {0} --output-file={1} --output-file-append test.versions'.format(
  184. self.config_dir,
  185. output_file_append
  186. ),
  187. catch_stderr=True,
  188. with_retcode=True
  189. )
  190. with salt.utils.files.fopen(output_file_append) as ofa:
  191. self.assertEqual(ofa.read(), output + output)
  192. finally:
  193. if os.path.exists(output_file_append):
  194. os.unlink(output_file_append)
  195. @skipIf(True, 'This test is unreliable. Need to investigate why more deeply.')
  196. @flaky
  197. def test_issue_14979_output_file_permissions(self):
  198. output_file = os.path.join(RUNTIME_VARS.TMP, 'issue-14979')
  199. with salt.utils.files.set_umask(0o077):
  200. try:
  201. # Let's create an initial output file with some data
  202. self.run_script(
  203. 'salt-call',
  204. '-c {0} --output-file={1} -l trace -g'.format(
  205. self.config_dir,
  206. output_file
  207. ),
  208. catch_stderr=True,
  209. with_retcode=True
  210. )
  211. try:
  212. stat1 = os.stat(output_file)
  213. except OSError:
  214. self.fail('Failed to generate output file, see log for details')
  215. # Let's change umask
  216. os.umask(0o777) # pylint: disable=blacklisted-function
  217. self.run_script(
  218. 'salt-call',
  219. '-c {0} --output-file={1} --output-file-append -g'.format(
  220. self.config_dir,
  221. output_file
  222. ),
  223. catch_stderr=True,
  224. with_retcode=True
  225. )
  226. try:
  227. stat2 = os.stat(output_file)
  228. except OSError:
  229. self.fail('Failed to generate output file, see log for details')
  230. self.assertEqual(stat1.st_mode, stat2.st_mode)
  231. # Data was appeneded to file
  232. self.assertTrue(stat1.st_size < stat2.st_size)
  233. # Let's remove the output file
  234. os.unlink(output_file)
  235. # Not appending data
  236. self.run_script(
  237. 'salt-call',
  238. '-c {0} --output-file={1} -g'.format(
  239. self.config_dir,
  240. output_file
  241. ),
  242. catch_stderr=True,
  243. with_retcode=True
  244. )
  245. try:
  246. stat3 = os.stat(output_file)
  247. except OSError:
  248. self.fail('Failed to generate output file, see log for details')
  249. # Mode must have changed since we're creating a new log file
  250. self.assertNotEqual(stat1.st_mode, stat3.st_mode)
  251. finally:
  252. if os.path.exists(output_file):
  253. os.unlink(output_file)
  254. @skipIf(sys.platform.startswith('win'), 'This test does not apply on Win')
  255. def test_42116_cli_pillar_override(self):
  256. ret = self.run_call(
  257. 'state.apply issue-42116-cli-pillar-override '
  258. 'pillar=\'{"myhost": "localhost"}\''
  259. )
  260. for line in ret:
  261. line = line.lstrip()
  262. if line == 'Comment: Command "ping -c 2 localhost" run':
  263. # Successful test
  264. break
  265. else:
  266. log.debug('salt-call output:\n\n%s', '\n'.join(ret))
  267. self.fail('CLI pillar override not found in pillar data')
  268. def test_pillar_items_masterless(self):
  269. '''
  270. Test to ensure we get expected output
  271. from pillar.items with salt-call
  272. '''
  273. get_items = self.run_call('pillar.items', local=True)
  274. exp_out = [' - Lancelot', ' - Galahad', ' - Bedevere',
  275. ' monty:', ' python']
  276. for out in exp_out:
  277. self.assertIn(out, get_items)
  278. def tearDown(self):
  279. '''
  280. Teardown method to remove installed packages
  281. '''
  282. user = ''
  283. user_info = self.run_call(' grains.get username', local=True)
  284. if user_info and isinstance(user_info, (list, tuple)) and isinstance(user_info[-1], six.string_types):
  285. user = user_info[-1].strip()
  286. super(CallTest, self).tearDown()
  287. # pylint: disable=invalid-name
  288. def test_exit_status_unknown_argument(self):
  289. '''
  290. Ensure correct exit status when an unknown argument is passed to salt-call.
  291. '''
  292. call = testprogram.TestProgramSaltCall(
  293. name='unknown_argument',
  294. parent_dir=self._test_dir,
  295. )
  296. # Call setup here to ensure config and script exist
  297. call.setup()
  298. stdout, stderr, status = call.run(
  299. args=['--unknown-argument'],
  300. catch_stderr=True,
  301. with_retcode=True,
  302. )
  303. self.assert_exit_status(
  304. status, 'EX_USAGE',
  305. message='unknown argument',
  306. stdout=stdout, stderr=stderr
  307. )
  308. def test_masterless_highstate(self):
  309. '''
  310. test state.highstate in masterless mode
  311. '''
  312. ret = self.run_call('state.highstate', local=True)
  313. destpath = os.path.join(RUNTIME_VARS.TMP, 'testfile')
  314. exp_out = [' Function: file.managed', ' Result: True',
  315. ' ID: {0}'.format(destpath)]
  316. for out in exp_out:
  317. self.assertIn(out, ret)
  318. self.assertTrue(os.path.exists(destpath))
  319. def test_exit_status_correct_usage(self):
  320. '''
  321. Ensure correct exit status when salt-call starts correctly.
  322. '''
  323. call = testprogram.TestProgramSaltCall(
  324. name='correct_usage',
  325. parent_dir=self._test_dir,
  326. )
  327. # Call setup here to ensure config and script exist
  328. call.setup()
  329. stdout, stderr, status = call.run(
  330. args=['--local', 'test.true'],
  331. catch_stderr=True,
  332. with_retcode=True,
  333. )
  334. self.assert_exit_status(
  335. status, 'EX_OK',
  336. message='correct usage',
  337. stdout=stdout, stderr=stderr
  338. )