test_kwarg.py 3.9 KB

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