test_kwarg.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. # -*- coding: utf-8 -*-
  2. # Import python libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. # Import Salt Testing libs
  5. from tests.support.case import ModuleCase
  6. # Import 3rd-party libs
  7. from salt.ext import six
  8. import salt.utils.platform
  9. class StdTest(ModuleCase):
  10. '''
  11. Test standard client calls
  12. '''
  13. def setUp(self):
  14. self.TIMEOUT = 600 if salt.utils.platform.is_windows() else 10
  15. def test_cli(self):
  16. '''
  17. Test cli function
  18. '''
  19. cmd_iter = self.client.cmd_cli(
  20. 'minion',
  21. 'test.arg',
  22. ['foo', 'bar', 'baz'],
  23. kwarg={'qux': 'quux'}
  24. )
  25. for ret in cmd_iter:
  26. data = ret['minion']['ret']
  27. self.assertEqual(data['args'], ['foo', 'bar', 'baz'])
  28. self.assertEqual(data['kwargs']['qux'], 'quux')
  29. def test_iter(self):
  30. '''
  31. test cmd_iter
  32. '''
  33. cmd_iter = self.client.cmd_iter(
  34. 'minion',
  35. 'test.arg',
  36. ['foo', 'bar', 'baz'],
  37. kwarg={'qux': 'quux'}
  38. )
  39. for ret in cmd_iter:
  40. data = ret['minion']['ret']
  41. self.assertEqual(data['args'], ['foo', 'bar', 'baz'])
  42. self.assertEqual(data['kwargs']['qux'], 'quux')
  43. def test_iter_no_block(self):
  44. '''
  45. test cmd_iter_no_block
  46. '''
  47. cmd_iter = self.client.cmd_iter_no_block(
  48. 'minion',
  49. 'test.arg',
  50. ['foo', 'bar', 'baz'],
  51. kwarg={'qux': 'quux'}
  52. )
  53. for ret in cmd_iter:
  54. if ret is None:
  55. continue
  56. data = ret['minion']['ret']
  57. self.assertEqual(data['args'], ['foo', 'bar', 'baz'])
  58. self.assertEqual(data['kwargs']['qux'], 'quux')
  59. def test_full_returns(self):
  60. '''
  61. test cmd_iter
  62. '''
  63. ret = self.client.cmd_full_return(
  64. 'minion',
  65. 'test.arg',
  66. ['foo', 'bar', 'baz'],
  67. timeout=self.TIMEOUT,
  68. kwarg={'qux': 'quux'}
  69. )
  70. data = ret['minion']['ret']
  71. self.assertEqual(data['args'], ['foo', 'bar', 'baz'])
  72. self.assertEqual(data['kwargs']['qux'], 'quux')
  73. def test_kwarg_type(self):
  74. '''
  75. Test that kwargs end up on the client as the same type
  76. '''
  77. terrible_yaml_string = 'foo: ""\n# \''
  78. ret = self.client.cmd_full_return(
  79. 'minion',
  80. 'test.arg_type',
  81. ['a', 1],
  82. kwarg={
  83. 'outer': {'a': terrible_yaml_string},
  84. 'inner': 'value'
  85. },
  86. timeout=self.TIMEOUT,
  87. )
  88. data = ret['minion']['ret']
  89. self.assertIn(six.text_type.__name__, data['args'][0])
  90. self.assertIn('int', data['args'][1])
  91. self.assertIn('dict', data['kwargs']['outer'])
  92. self.assertIn(six.text_type.__name__, data['kwargs']['inner'])
  93. def test_full_return_kwarg(self):
  94. ret = self.client.cmd(
  95. 'minion', 'test.ping', full_return=True, timeout=self.TIMEOUT,
  96. )
  97. for mid, data in ret.items():
  98. self.assertIn('retcode', data)
  99. def test_cmd_arg_kwarg_parsing(self):
  100. ret = self.client.cmd('minion', 'test.arg_clean',
  101. arg=[
  102. 'foo',
  103. 'bar=off',
  104. 'baz={qux: 123}'
  105. ],
  106. kwarg={
  107. 'quux': 'Quux',
  108. },
  109. timeout=self.TIMEOUT,
  110. )
  111. self.assertEqual(ret['minion'], {
  112. 'args': ['foo'],
  113. 'kwargs': {
  114. 'bar': False,
  115. 'baz': {
  116. 'qux': 123,
  117. },
  118. 'quux': 'Quux',
  119. },
  120. })