test_args.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. # -*- coding: utf-8 -*-
  2. # Import python libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. import logging
  5. from collections import namedtuple
  6. import salt.utils.args
  7. # Import Salt Libs
  8. from salt.exceptions import SaltInvocationError
  9. from salt.ext import six
  10. from tests.support.mock import DEFAULT, patch
  11. # Import Salt Testing Libs
  12. from tests.support.unit import TestCase
  13. log = logging.getLogger(__name__)
  14. class ArgsTestCase(TestCase):
  15. """
  16. TestCase for salt.utils.args module
  17. """
  18. def test_condition_input_string(self):
  19. """
  20. Test passing a jid on the command line
  21. """
  22. cmd = salt.utils.args.condition_input(
  23. ["*", "foo.bar", 20141020201325675584], None
  24. )
  25. self.assertIsInstance(cmd[2], six.text_type)
  26. def test_clean_kwargs(self):
  27. self.assertDictEqual(salt.utils.args.clean_kwargs(foo="bar"), {"foo": "bar"})
  28. self.assertDictEqual(salt.utils.args.clean_kwargs(__pub_foo="bar"), {})
  29. self.assertDictEqual(salt.utils.args.clean_kwargs(__foo_bar="gwar"), {})
  30. self.assertDictEqual(
  31. salt.utils.args.clean_kwargs(foo_bar="gwar"), {"foo_bar": "gwar"}
  32. )
  33. def test_get_function_argspec(self):
  34. def dummy_func(first, second, third, fourth="fifth"):
  35. pass
  36. expected_argspec = namedtuple("ArgSpec", "args varargs keywords defaults")(
  37. args=["first", "second", "third", "fourth"],
  38. varargs=None,
  39. keywords=None,
  40. defaults=("fifth",),
  41. )
  42. ret = salt.utils.args.get_function_argspec(dummy_func)
  43. self.assertEqual(ret, expected_argspec)
  44. def test_parse_kwarg(self):
  45. ret = salt.utils.args.parse_kwarg("foo=bar")
  46. self.assertEqual(ret, ("foo", "bar"))
  47. ret = salt.utils.args.parse_kwarg("foobar")
  48. self.assertEqual(ret, (None, None))
  49. def test_arg_lookup(self):
  50. def dummy_func(first, second, third, fourth="fifth"):
  51. pass
  52. expected_dict = {
  53. "args": ["first", "second", "third"],
  54. "kwargs": {"fourth": "fifth"},
  55. }
  56. ret = salt.utils.args.arg_lookup(dummy_func)
  57. self.assertEqual(expected_dict, ret)
  58. def test_format_call(self):
  59. with patch("salt.utils.args.arg_lookup") as arg_lookup:
  60. def dummy_func(first=None, second=None, third=None):
  61. pass
  62. arg_lookup.return_value = {
  63. "args": ["first", "second", "third"],
  64. "kwargs": {},
  65. }
  66. get_function_argspec = DEFAULT
  67. get_function_argspec.return_value = namedtuple(
  68. "ArgSpec", "args varargs keywords defaults"
  69. )(
  70. args=["first", "second", "third", "fourth"],
  71. varargs=None,
  72. keywords=None,
  73. defaults=("fifth",),
  74. )
  75. # Make sure we raise an error if we don't pass in the requisite number of arguments
  76. self.assertRaises(
  77. SaltInvocationError, salt.utils.args.format_call, dummy_func, {"1": 2}
  78. )
  79. # Make sure we warn on invalid kwargs
  80. self.assertRaises(
  81. SaltInvocationError,
  82. salt.utils.args.format_call,
  83. dummy_func,
  84. {"first": 2, "seconds": 2, "third": 3},
  85. )
  86. ret = salt.utils.args.format_call(
  87. dummy_func,
  88. {"first": 2, "second": 2, "third": 3},
  89. expected_extra_kws=("first", "second", "third"),
  90. )
  91. self.assertDictEqual(ret, {"args": [], "kwargs": {}})
  92. def test_format_call_simple_args(self):
  93. def foo(one, two=2, three=3):
  94. pass
  95. self.assertEqual(
  96. salt.utils.args.format_call(foo, dict(one=10, two=20, three=30)),
  97. {"args": [10], "kwargs": dict(two=20, three=30)},
  98. )
  99. self.assertEqual(
  100. salt.utils.args.format_call(foo, dict(one=10, two=20)),
  101. {"args": [10], "kwargs": dict(two=20, three=3)},
  102. )
  103. self.assertEqual(
  104. salt.utils.args.format_call(foo, dict(one=2)),
  105. {"args": [2], "kwargs": dict(two=2, three=3)},
  106. )
  107. def test_format_call_mimic_typeerror_exceptions(self):
  108. def foo(one, two=2, three=3):
  109. pass
  110. def foo2(one, two, three=3):
  111. pass
  112. with self.assertRaisesRegex(
  113. SaltInvocationError, r"foo takes at least 1 argument \(0 given\)"
  114. ):
  115. salt.utils.args.format_call(foo, dict(two=3))
  116. with self.assertRaisesRegex(
  117. TypeError, r"foo2 takes at least 2 arguments \(1 given\)"
  118. ):
  119. salt.utils.args.format_call(foo2, dict(one=1))
  120. def test_argspec_report(self):
  121. def _test_spec(arg1, arg2, kwarg1=None):
  122. pass
  123. test_functions = {"test_module.test_spec": _test_spec}
  124. ret = salt.utils.args.argspec_report(test_functions, "test_module.test_spec")
  125. self.assertDictEqual(
  126. ret,
  127. {
  128. "test_module.test_spec": {
  129. "kwargs": None,
  130. "args": ["arg1", "arg2", "kwarg1"],
  131. "defaults": (None,),
  132. "varargs": None,
  133. }
  134. },
  135. )
  136. def test_test_mode(self):
  137. self.assertTrue(salt.utils.args.test_mode(test=True))
  138. self.assertTrue(salt.utils.args.test_mode(Test=True))
  139. self.assertTrue(salt.utils.args.test_mode(tEsT=True))
  140. def test_parse_function_no_args(self):
  141. fun, args, kwargs = salt.utils.args.parse_function("amod.afunc()")
  142. self.assertEqual(fun, "amod.afunc")
  143. self.assertEqual(args, [])
  144. self.assertEqual(kwargs, {})
  145. def test_parse_function_args_only(self):
  146. fun, args, kwargs = salt.utils.args.parse_function("amod.afunc(str1, str2)")
  147. self.assertEqual(fun, "amod.afunc")
  148. self.assertEqual(args, ["str1", "str2"])
  149. self.assertEqual(kwargs, {})
  150. def test_parse_function_kwargs_only(self):
  151. fun, args, kwargs = salt.utils.args.parse_function(
  152. "amod.afunc(kw1=val1, kw2=val2)"
  153. )
  154. self.assertEqual(fun, "amod.afunc")
  155. self.assertEqual(args, [])
  156. self.assertEqual(kwargs, {"kw1": "val1", "kw2": "val2"})
  157. def test_parse_function_args_kwargs(self):
  158. fun, args, kwargs = salt.utils.args.parse_function(
  159. "amod.afunc(str1, str2, kw1=val1, kw2=val2)"
  160. )
  161. self.assertEqual(fun, "amod.afunc")
  162. self.assertEqual(args, ["str1", "str2"])
  163. self.assertEqual(kwargs, {"kw1": "val1", "kw2": "val2"})
  164. def test_parse_function_malformed_no_name(self):
  165. fun, args, kwargs = salt.utils.args.parse_function(
  166. "(str1, str2, kw1=val1, kw2=val2)"
  167. )
  168. self.assertIsNone(fun)
  169. self.assertIsNone(args)
  170. self.assertIsNone(kwargs)
  171. def test_parse_function_malformed_not_fun_def(self):
  172. fun, args, kwargs = salt.utils.args.parse_function("foo bar, some=text")
  173. self.assertIsNone(fun)
  174. self.assertIsNone(args)
  175. self.assertIsNone(kwargs)
  176. def test_parse_function_wrong_bracket_style(self):
  177. fun, args, kwargs = salt.utils.args.parse_function(
  178. "amod.afunc[str1, str2, kw1=val1, kw2=val2]"
  179. )
  180. self.assertIsNone(fun)
  181. self.assertIsNone(args)
  182. self.assertIsNone(kwargs)
  183. def test_parse_function_brackets_unballanced(self):
  184. fun, args, kwargs = salt.utils.args.parse_function(
  185. "amod.afunc(str1, str2, kw1=val1, kw2=val2"
  186. )
  187. self.assertIsNone(fun)
  188. self.assertIsNone(args)
  189. self.assertIsNone(kwargs)
  190. fun, args, kwargs = salt.utils.args.parse_function(
  191. "amod.afunc(str1, str2, kw1=val1, kw2=val2]"
  192. )
  193. self.assertIsNone(fun)
  194. self.assertIsNone(args)
  195. self.assertIsNone(kwargs)
  196. fun, args, kwargs = salt.utils.args.parse_function(
  197. "amod.afunc(str1, str2, kw1=(val1[val2)], kw2=val2)"
  198. )
  199. self.assertIsNone(fun)
  200. self.assertIsNone(args)
  201. self.assertIsNone(kwargs)
  202. def test_parse_function_brackets_in_quotes(self):
  203. fun, args, kwargs = salt.utils.args.parse_function(
  204. 'amod.afunc(str1, str2, kw1="(val1[val2)]", kw2=val2)'
  205. )
  206. self.assertEqual(fun, "amod.afunc")
  207. self.assertEqual(args, ["str1", "str2"])
  208. self.assertEqual(kwargs, {"kw1": "(val1[val2)]", "kw2": "val2"})
  209. def test_parse_function_quotes(self):
  210. fun, args, kwargs = salt.utils.args.parse_function(
  211. 'amod.afunc("double \\" single \'", \'double " single \\\'\', kw1="equal=equal", kw2=val2)'
  212. )
  213. self.assertEqual(fun, "amod.afunc")
  214. self.assertEqual(args, ["double \" single '", "double \" single '"])
  215. self.assertEqual(kwargs, {"kw1": "equal=equal", "kw2": "val2"})
  216. def test_yamlify_arg(self):
  217. """
  218. Test that we properly yamlify CLI input. In several of the tests below
  219. assertIs is used instead of assertEqual. This is because we want to
  220. confirm that the return value is not a copy of the original, but the
  221. same instance as the original.
  222. """
  223. def _yamlify_arg(item):
  224. log.debug("Testing yamlify_arg with %r", item)
  225. return salt.utils.args.yamlify_arg(item)
  226. # Make sure non-strings are just returned back
  227. for item in (True, False, None, 123, 45.67, ["foo"], {"foo": "bar"}):
  228. self.assertIs(_yamlify_arg(item), item)
  229. # Make sure whitespace-only isn't loaded as None
  230. for item in ("", "\t", " "):
  231. self.assertIs(_yamlify_arg(item), item)
  232. # This value would be loaded as an int (123), the underscores would be
  233. # ignored. Test that we identify this case and return the original
  234. # value.
  235. item = "1_2_3"
  236. self.assertIs(_yamlify_arg(item), item)
  237. # The '#' is treated as a comment when not part of a data structure, we
  238. # don't want that behavior
  239. for item in ("# hash at beginning", "Hello world! # hash elsewhere"):
  240. self.assertIs(_yamlify_arg(item), item)
  241. # However we _do_ want the # to be intact if it _is_ within a data
  242. # structure.
  243. item = '["foo", "bar", "###"]'
  244. self.assertEqual(_yamlify_arg(item), ["foo", "bar", "###"])
  245. item = '{"foo": "###"}'
  246. self.assertEqual(_yamlify_arg(item), {"foo": "###"})
  247. # The string "None" should load _as_ None
  248. self.assertIs(_yamlify_arg("None"), None)
  249. # Leading dashes, or strings containing colons, will result in lists
  250. # and dicts, and we only want to load lists and dicts when the strings
  251. # look like data structures.
  252. for item in ("- foo", "foo: bar"):
  253. self.assertIs(_yamlify_arg(item), item)
  254. # Make sure we don't load '|' as ''
  255. item = "|"
  256. self.assertIs(_yamlify_arg(item), item)
  257. # Make sure we don't load '!' as something else (None in 2018.3, '' in newer)
  258. item = "!"
  259. self.assertIs(_yamlify_arg(item), item)
  260. # Make sure we load ints, floats, and strings correctly
  261. self.assertEqual(_yamlify_arg("123"), 123)
  262. self.assertEqual(_yamlify_arg("45.67"), 45.67)
  263. self.assertEqual(_yamlify_arg("foo"), "foo")
  264. # We tested list/dict loading above, but there is separate logic when
  265. # the string contains a '#', so we need to test again here.
  266. self.assertEqual(_yamlify_arg('["foo", "bar"]'), ["foo", "bar"])
  267. self.assertEqual(_yamlify_arg('{"foo": "bar"}'), {"foo": "bar"})
  268. # Make sure that an empty string is loaded properly.
  269. self.assertEqual(_yamlify_arg(" "), " ")
  270. # Make sure that we don't improperly load strings that would be
  271. # interpreted by PyYAML as YAML document start/end.
  272. self.assertEqual(_yamlify_arg("---"), "---")
  273. self.assertEqual(_yamlify_arg("--- "), "--- ")
  274. self.assertEqual(_yamlify_arg("..."), "...")
  275. self.assertEqual(_yamlify_arg(" ..."), " ...")
  276. # Make sure that non-printable whitespace is not YAML-loaded
  277. self.assertEqual(_yamlify_arg("foo\t\nbar"), "foo\t\nbar")
  278. class KwargRegexTest(TestCase):
  279. def test_arguments_regex(self):
  280. argument_matches = (
  281. ("pip=1.1", ("pip", "1.1")),
  282. ("pip==1.1", None),
  283. ("pip=1.2=1", ("pip", "1.2=1")),
  284. )
  285. for argument, match in argument_matches:
  286. if match is None:
  287. self.assertIsNone(salt.utils.args.KWARG_REGEX.match(argument))
  288. else:
  289. self.assertEqual(
  290. salt.utils.args.KWARG_REGEX.match(argument).groups(), match
  291. )