diff --git a/salt/pillar/pillar_ldap.py b/salt/pillar/pillar_ldap.py index 6dbbaa759112..b2321ebc1b44 100644 --- a/salt/pillar/pillar_ldap.py +++ b/salt/pillar/pillar_ldap.py @@ -163,15 +163,17 @@ def _render_template(config_file): return template.render(__grains__) -def _config(name, conf): +def _config(name, conf, default=None): ''' - Return a value for 'name' from the config file options. + Return a value for 'name' from the config file options. If the 'name' is + not in the config, the 'default' value is returned. This method converts + unicode values to str type under python 2. ''' try: - value = salt.utils.data.decode(conf[name], to_str=True) + value = conf[name] except KeyError: - value = None - return value + value = default + return salt.utils.data.decode(value, to_str=True) def _result_to_dict(data, result, conf, source): @@ -285,7 +287,7 @@ def _do_search(conf): scope = _config('scope', conf) _lists = _config('lists', conf) or [] _attrs = _config('attrs', conf) or [] - _dict_key_attr = _config('dict_key_attr', conf) or 'dn' + _dict_key_attr = _config('dict_key_attr', conf, 'dn') attrs = _lists + _attrs + [_dict_key_attr] if not attrs: attrs = None diff --git a/tests/unit/pillar/test_pillar_ldap.py b/tests/unit/pillar/test_pillar_ldap.py new file mode 100644 index 000000000000..53ae293dcdf7 --- /dev/null +++ b/tests/unit/pillar/test_pillar_ldap.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- + +# Import python libs +from __future__ import absolute_import, print_function, unicode_literals + +from tests.support.unit import TestCase +import salt.utils.stringutils + + +from salt.pillar.pillar_ldap import _config + + +class LdapPillarTestCase(TestCase): + + def test__config_returns_str(self): + conf = {'foo': 'bar'} + assert _config('foo', conf) == salt.utils.stringutils.to_str('bar') + + def test__conf_defaults_to_none(self): + conf = {'foo': 'bar'} + assert _config('bang', conf) is None + + def test__conf_returns_str_from_unicode_default(self): + conf = {'foo': 'bar'} + default = salt.utils.stringutils.to_unicode('bam') + assert _config('bang', conf, default) == salt.utils.stringutils.to_str('bam')