make LDAP attr defaults string types on py2

This commit is contained in:
Daniel A. Wozniak 2019-03-04 21:35:17 +00:00
parent 0ca04ffbeb
commit 8c641c66fb
No known key found for this signature in database
GPG key ID: 166B9D2C06C82D61
3 changed files with 37 additions and 8 deletions

View file

@ -144,9 +144,9 @@ def search(filter, # pylint: disable=C0103
attrs = _config('attrs')
_ldap = _connect(**kwargs)
start = time.time()
log.debug(
log.warn(
'Running LDAP search with filter:%s, dn:%s, scope:%s, '
'attrs:%s', filter, dn, scope, attrs
'attrs:%r', filter, dn, scope, attrs
)
results = _ldap.search_s(dn, int(scope), filter, attrs)
elapsed = (time.time() - start)

View file

@ -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

View file

@ -0,0 +1,27 @@
# -*- 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) == 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')