Merge pull request #42474 from whiteinge/cmd-arg-kwarg-parsing-test

Cmd arg kwarg parsing test
This commit is contained in:
Nicole Thomas 2017-07-24 08:13:29 -06:00 committed by GitHub
commit 083ff00410
3 changed files with 44 additions and 8 deletions

View file

@ -306,14 +306,16 @@ def load_args_and_kwargs(func, args, data=None, ignore_invalid=False):
else:
string_kwarg = salt.utils.args.parse_input([arg], condition=False)[1] # pylint: disable=W0632
if string_kwarg:
log.critical(
'String kwarg(s) %s passed to '
'salt.minion.load_args_and_kwargs(). This is no longer '
'supported, so the kwarg(s) will be ignored. Arguments '
'passed to salt.minion.load_args_and_kwargs() should be '
'passed to salt.utils.args.parse_input() first to load '
'and condition them properly.', string_kwarg
)
if argspec.keywords or next(six.iterkeys(string_kwarg)) in argspec.args:
# Function supports **kwargs or is a positional argument to
# the function.
_kwargs.update(string_kwarg)
else:
# **kwargs not in argspec and parsed argument name not in
# list of positional arguments. This keyword argument is
# invalid.
for key, val in six.iteritems(string_kwarg):
invalid_kwargs.append('{0}={1}'.format(key, val))
else:
_args.append(arg)

View file

@ -311,6 +311,18 @@ def arg_repr(*args, **kwargs):
return {"args": repr(args), "kwargs": repr(kwargs)}
def arg_clean(*args, **kwargs):
'''
Like test.arg but cleans kwargs of the __pub* items
CLI Example:
.. code-block:: bash
salt '*' test.arg_clean 1 "two" 3.1 txt="hello" wow='{a: 1, b: "hello"}'
'''
return dict(args=args, kwargs=salt.utils.clean_kwargs(**kwargs))
def fib(num):
'''
Return the num-th Fibonacci number, and the time it took to compute in

View file

@ -94,3 +94,25 @@ class StdTest(ModuleCase):
ret = self.client.cmd('minion', 'test.ping', full_return=True)
for mid, data in ret.items():
self.assertIn('retcode', data)
def test_cmd_arg_kwarg_parsing(self):
ret = self.client.cmd('minion', 'test.arg_clean',
arg=[
'foo',
'bar=off',
'baz={qux: 123}'
],
kwarg={
'quux': 'Quux',
})
self.assertEqual(ret['minion'], {
'args': ['foo'],
'kwargs': {
'bar': False,
'baz': {
'qux': 123,
},
'quux': 'Quux',
},
})