Beispiel #1
0
    def test_format_call(self, arg_lookup, get_function_argspec):
        def dummy_func(first=None, second=None, third=None):
            pass

        arg_lookup.return_value = {
            'args': ['first', 'second', 'third'],
            'kwargs': {}
        }
        get_function_argspec.return_value = namedtuple(
            'ArgSpec', 'args varargs keywords defaults')(
                args=['first', 'second', 'third', 'fourth'],
                varargs=None,
                keywords=None,
                defaults=('fifth', ))

        # Make sure we raise an error if we don't pass in the requisite number of arguments
        self.assertRaises(SaltInvocationError, utils.format_call, dummy_func,
                          {'1': 2})

        # Make sure we warn on invalid kwargs
        ret = utils.format_call(dummy_func, {
            'first': 2,
            'second': 2,
            'third': 3
        })
        self.assertGreaterEqual(len(ret['warnings']), 1)

        ret = utils.format_call(dummy_func, {
            'first': 2,
            'second': 2,
            'third': 3
        },
                                expected_extra_kws=('first', 'second',
                                                    'third'))
        self.assertDictEqual(ret, {'args': [], 'kwargs': {}})
Beispiel #2
0
    def test_mimic_typeerror_exceptions(self):
        def foo(one, two=2, three=3):
            pass

        def foo2(one, two, three=3):
            pass

        with self.assertRaisesRegexp(
                SaltInvocationError,
                r'foo takes at least 1 argument \(0 given\)'):
            format_call(foo, dict(two=3))

        with self.assertRaisesRegexp(
                TypeError, r'foo2 takes at least 2 arguments \(1 given\)'):
            format_call(foo2, dict(one=1))
Beispiel #3
0
    def test_mimic_typeerror_exceptions(self):
        def foo(one, two=2, three=3):
            pass

        def foo2(one, two, three=3):
            pass

        with self.assertRaisesRegexp(
                SaltInvocationError,
                r'foo takes at least 1 argument \(0 given\)'):
            format_call(foo, dict(two=3))

        with self.assertRaisesRegexp(
                TypeError,
                r'foo2 takes at least 2 arguments \(1 given\)'):
            format_call(foo2, dict(one=1))
Beispiel #4
0
    def test_simple_args_passing(self):
        def foo(one, two=2, three=3):
            pass

        self.assertEqual(
            format_call(foo, dict(one=10, two=20, three=30)),
            {'args': [10], 'kwargs': dict(two=20, three=30)}
        )
        self.assertEqual(
            format_call(foo, dict(one=10, two=20)),
            {'args': [10], 'kwargs': dict(two=20, three=3)}
        )
        self.assertEqual(
            format_call(foo, dict(one=2)),
            {'args': [2], 'kwargs': dict(two=2, three=3)}
        )
Beispiel #5
0
    def test_simple_args_passing(self):
        def foo(one, two=2, three=3):
            pass

        self.assertEqual(
            format_call(foo, dict(one=10, two=20, three=30)),
            {'args': [10], 'kwargs': dict(two=20, three=30)}
        )
        self.assertEqual(
            format_call(foo, dict(one=10, two=20)),
            {'args': [10], 'kwargs': dict(two=20, three=3)}
        )
        self.assertEqual(
            format_call(foo, dict(one=2)),
            {'args': [2], 'kwargs': dict(two=2, three=3)}
        )
Beispiel #6
0
    def test_format_call(self, arg_lookup):
        def dummy_func(first=None, second=None, third=None):
            pass
        arg_lookup.return_value = {'args': ['first', 'second', 'third'], 'kwargs': {}}
        get_function_argspec = DEFAULT
        get_function_argspec.return_value = namedtuple('ArgSpec', 'args varargs keywords defaults')(
            args=['first', 'second', 'third', 'fourth'], varargs=None, keywords=None, defaults=('fifth',))

        # Make sure we raise an error if we don't pass in the requisite number of arguments
        self.assertRaises(SaltInvocationError, utils.format_call, dummy_func, {'1': 2})

        # Make sure we warn on invalid kwargs
        ret = utils.format_call(dummy_func, {'first': 2, 'second': 2, 'third': 3})
        self.assertGreaterEqual(len(ret['warnings']), 1)

        ret = utils.format_call(dummy_func, {'first': 2, 'second': 2, 'third': 3},
                                expected_extra_kws=('first', 'second', 'third'))
        self.assertDictEqual(ret, {'args': [], 'kwargs': {}})
Beispiel #7
0
    def test_format_call(self, arg_lookup, get_function_argspec):
        # def test_format_call(self):
        def dummy_func(first=None, second=None, third=None):
            pass

        arg_lookup.return_value = {"args": ["first", "second", "third"], "kwargs": {}}
        get_function_argspec.return_value = namedtuple("ArgSpec", "args varargs keywords defaults")(
            args=["first", "second", "third", "fourth"], varargs=None, keywords=None, defaults=("fifth",)
        )

        # Make sure we raise an error if we don't pass in the requisite number of arguments
        self.assertRaises(SaltInvocationError, utils.format_call, dummy_func, {"1": 2})

        # Make sure we warn on invalid kwargs
        ret = utils.format_call(dummy_func, {"first": 2, "second": 2, "third": 3})
        self.assertGreaterEqual(len(ret["warnings"]), 1)

        ret = utils.format_call(
            dummy_func, {"first": 2, "second": 2, "third": 3}, expected_extra_kws=("first", "second", "third")
        )
        self.assertDictEqual(ret, {"args": [], "kwargs": {}})