示例#1
0
    def test_pillar_get_int_key(self):
        """
        Confirm that we can access pillar keys that are integers
        """
        with patch.dict(pillarmod.__pillar__, {12345: "luggage_code"}):

            self.assertEqual(
                pillarmod.get(key=12345, default=None, merge=True), "luggage_code",
            )

        with patch.dict(
            pillarmod.__pillar__, {12345: {"l2": {"l3": "my_luggage_code"}}}
        ):

            res = pillarmod.get(key=12345)
            self.assertEqual({"l2": {"l3": "my_luggage_code"}}, res)

            default = {"l2": {"l3": "your_luggage_code"}}

            res = pillarmod.get(key=12345, default=default)
            self.assertEqual({"l2": {"l3": "my_luggage_code"}}, res)
            self.assertEqual({"l2": {"l3": "your_luggage_code"}}, default)

            res = pillarmod.get(key=12345, default=default, merge=True)
            self.assertEqual({"l2": {"l3": "my_luggage_code"}}, res)
            self.assertEqual({"l2": {"l3": "your_luggage_code"}}, default)
示例#2
0
def test_pillar_get_int_key():
    """
    Confirm that we can access pillar keys that are integers
    """
    with patch.dict(pillarmod.__pillar__, {12345: "luggage_code"}):

        assert pillarmod.get(key=12345, default=None,
                             merge=True) == "luggage_code"

    with patch.dict(pillarmod.__pillar__,
                    {12345: {
                        "l2": {
                            "l3": "my_luggage_code"
                        }
                    }}):

        res = pillarmod.get(key=12345)
        assert {"l2": {"l3": "my_luggage_code"}} == res

        default = {"l2": {"l3": "your_luggage_code"}}

        res = pillarmod.get(key=12345, default=default)
        assert {"l2": {"l3": "my_luggage_code"}} == res
        assert {"l2": {"l3": "your_luggage_code"}} == default

        res = pillarmod.get(key=12345, default=default, merge=True)
        assert {"l2": {"l3": "my_luggage_code"}} == res
        assert {"l2": {"l3": "your_luggage_code"}} == default
示例#3
0
    def test_pillar_get_default_merge(self):
        with patch.dict(pillarmod.__pillar__, {'key': 'value'}):
            default = {'default': 'plop'}

            res = pillarmod.get(key='key', default=default)
            self.assertEqual("value", res)

            res = pillarmod.get(key='missing pillar', default=default)
            self.assertEqual({'default': 'plop'}, res)
示例#4
0
    def test_pillar_get_default_merge(self):
        defaults = {
            "int": 1,
            "string": "foo",
            "list": ["foo"],
            "dict": {"foo": "bar", "subkey": {"foo": "bar"}},
        }

        pillar_mock = {
            "int": 2,
            "string": "bar",
            "list": ["bar", "baz"],
            "dict": {"baz": "qux", "subkey": {"baz": "qux"}},
        }

        # Test that we raise a KeyError when pillar_raise_on_missing is True
        with patch.dict(pillarmod.__opts__, {"pillar_raise_on_missing": True}):
            self.assertRaises(KeyError, pillarmod.get, "missing")
        # Test that we return an empty string when it is not
        with patch.dict(pillarmod.__opts__, {}):
            self.assertEqual(pillarmod.get("missing"), "")

        with patch.dict(pillarmod.__pillar__, pillar_mock):
            # Test with no default passed (it should be KeyError) and merge=True.
            # The merge should be skipped and the value returned from __pillar__
            # should be returned.
            for item in pillarmod.__pillar__:
                self.assertEqual(
                    pillarmod.get(item, merge=True), pillarmod.__pillar__[item]
                )

            # Test merging when the type of the default value is not the same as
            # what was returned. Merging should be skipped and the value returned
            # from __pillar__ should be returned.
            for default_type in defaults:
                for data_type in ("dict", "list"):
                    if default_type == data_type:
                        continue
                    self.assertEqual(
                        pillarmod.get(
                            data_type, default=defaults[default_type], merge=True
                        ),
                        pillarmod.__pillar__[data_type],
                    )

            # Test recursive dict merging
            self.assertEqual(
                pillarmod.get("dict", default=defaults["dict"], merge=True),
                {"foo": "bar", "baz": "qux", "subkey": {"foo": "bar", "baz": "qux"}},
            )

            # Test list merging
            self.assertEqual(
                pillarmod.get("list", default=defaults["list"], merge=True),
                ["foo", "bar", "baz"],
            )
示例#5
0
    def test_pillar_get_default_merge(self):
        pillarmod.__opts__ = {}
        pillarmod.__pillar__ = {'key': 'value'}
        default = {'default': 'plop'}

        res = pillarmod.get(key='key', default=default)
        self.assertEqual("value", res)

        res = pillarmod.get(key='missing pillar', default=default)
        self.assertEqual({'default': 'plop'}, res)
示例#6
0
文件: test_pillar.py 项目: urbas/salt
    def test_pillar_get_default_merge(self):
        defaults = {'int': 1,
                    'string': 'foo',
                    'list': ['foo'],
                    'dict': {'foo': 'bar', 'subkey': {'foo': 'bar'}}}

        pillar_mock = {'int': 2,
                       'string': 'bar',
                       'list': ['bar', 'baz'],
                       'dict': {'baz': 'qux', 'subkey': {'baz': 'qux'}}}

        # Test that we raise a KeyError when pillar_raise_on_missing is True
        with patch.dict(pillarmod.__opts__, {'pillar_raise_on_missing': True}):
            self.assertRaises(KeyError, pillarmod.get, 'missing')
        # Test that we return an empty string when it is not
        with patch.dict(pillarmod.__opts__, {}):
            self.assertEqual(pillarmod.get('missing'), '')

        with patch.dict(pillarmod.__pillar__, pillar_mock):
            # Test with no default passed (it should be KeyError) and merge=True.
            # The merge should be skipped and the value returned from __pillar__
            # should be returned.
            for item in pillarmod.__pillar__:
                self.assertEqual(
                    pillarmod.get(item, merge=True),
                    pillarmod.__pillar__[item]
                )

            # Test merging when the type of the default value is not the same as
            # what was returned. Merging should be skipped and the value returned
            # from __pillar__ should be returned.
            for default_type in defaults:
                for data_type in ('dict', 'list'):
                    if default_type == data_type:
                        continue
                    self.assertEqual(
                        pillarmod.get(item, default=defaults[default_type], merge=True),
                        pillarmod.__pillar__[item]
                    )

            # Test recursive dict merging
            self.assertEqual(
                pillarmod.get('dict', default=defaults['dict'], merge=True),
                {'foo': 'bar', 'baz': 'qux', 'subkey': {'foo': 'bar', 'baz': 'qux'}}
            )

            # Test list merging
            self.assertEqual(
                pillarmod.get('list', default=defaults['list'], merge=True),
                ['foo', 'bar', 'baz']
            )
示例#7
0
def test_pillar_get_default_merge_regression_39062():
    """
    Confirm that we do not raise an exception if default is None and
    merge=True.
    See https://github.com/saltstack/salt/issues/39062 for more info.
    """
    with patch.dict(pillarmod.__pillar__, {"foo": "bar"}):

        assert pillarmod.get(key="foo", default=None, merge=True) == "bar"
示例#8
0
def test_pillar_get_default_merge_regression_38558():
    """Test for pillar.get(key=..., default=..., merge=True)
    Do not update the ``default`` value when using ``merge=True``.
    See: https://github.com/saltstack/salt/issues/38558
    """
    with patch.dict(pillarmod.__pillar__, {"l1": {"l2": {"l3": 42}}}):

        res = pillarmod.get(key="l1")
        assert {"l2": {"l3": 42}} == res

        default = {"l2": {"l3": 43}}

        res = pillarmod.get(key="l1", default=default)
        assert {"l2": {"l3": 42}} == res
        assert {"l2": {"l3": 43}} == default

        res = pillarmod.get(key="l1", default=default, merge=True)
        assert {"l2": {"l3": 42}} == res
        assert {"l2": {"l3": 43}} == default
示例#9
0
    def test_pillar_get_default_merge_regression_38558(self):
        """Test for pillar.get(key=..., default=..., merge=True)

        Do not update the ``default`` value when using ``merge=True``.

        See: https://github.com/saltstack/salt/issues/38558
        """
        with patch.dict(pillarmod.__pillar__, {'l1': {'l2': {'l3': 42}}}):

            res = pillarmod.get(key='l1')
            self.assertEqual({'l2': {'l3': 42}}, res)

            default = {'l2': {'l3': 43}}

            res = pillarmod.get(key='l1', default=default)
            self.assertEqual({'l2': {'l3': 42}}, res)
            self.assertEqual({'l2': {'l3': 43}}, default)

            res = pillarmod.get(key='l1', default=default, merge=True)
            self.assertEqual({'l2': {'l3': 42}}, res)
            self.assertEqual({'l2': {'l3': 43}}, default)
示例#10
0
def get(key, default=KeyError, *args, **kwargs):
    """
    Proxy function for stock :py:func:`salt.modules.pillar.get` which raises a
    ``KeyError`` if a default value is not provided and an non-existent pillar
    is requested.

    It also keeps a record of the requested pillars and the provided default
    values if :ref:`pillar-__test__` is set to ``True``, which is used in
    :mod:`_states.qa`
    """
    DEBUG = pillar.get('__test__', False)

    if DEBUG:
        calls = __salt__['pillar.get_calls']()
        if default not in calls[key]:
            calls[key].append(default)
            __salt__['pillar.get_calls'](value=calls)

    ret = pillar.get(key, default)
    if ret is KeyError:
        raise KeyError("Pillar key does not exist: %s" % key)
    return ret
示例#11
0
    def test_pillar_get_default_merge_regression_39062(self):
        '''
        Confirm that we do not raise an exception if default is None and
        merge=True.

        See https://github.com/saltstack/salt/issues/39062 for more info.
        '''
        with patch.dict(pillarmod.__pillar__, {'foo': 'bar'}):

            self.assertEqual(
                pillarmod.get(key='foo', default=None, merge=True),
                'bar',
            )
示例#12
0
文件: pillar.py 项目: Quarky9/states
def get(key, default=KeyError, *args, **kwargs):
    """
    Proxy function for stock :py:func:`salt.modules.pillar.get` which raises a
    ``KeyError`` if a default value is not provided and an non-existent pillar
    is requested.

    It also keeps a record of the requested pillars and the provided default
    values if :ref:`pillar-__test__` is set to ``True``, which is used in
    :mod:`_states.qa`
    """
    DEBUG = pillar.get('__test__', False)

    if DEBUG:
        calls = __salt__['pillar.get_calls']()
        if default not in calls[key]:
            calls[key].append(default)
            __salt__['pillar.get_calls'](value=calls)

    ret = pillar.get(key, default)
    if ret is KeyError:
        raise KeyError("Pillar key does not exist: %s" % key)
    return ret