Ensure file_tree pillar returns unicode

This commit is contained in:
Megan Wilhite 2020-11-09 16:37:04 -05:00 committed by Daniel Wozniak
parent 3e269eda82
commit 6826255ab5
3 changed files with 33 additions and 9 deletions

1
changelog/58794.fixed Normal file
View file

@ -0,0 +1 @@
Ensure file_tree pillar returns unicode.

View file

@ -259,9 +259,9 @@ def _construct_pillar(
whitelist=renderer_whitelist,
)
if salt.utils.stringio.is_readable(data):
pillar_node[file_name] = data.getvalue()
pillar_node[file_name] = salt.utils.stringutils.to_unicode(data.getvalue())
else:
pillar_node[file_name] = data
pillar_node[file_name] = salt.utils.stringutils.to_unicode(data)
return pillar

View file

@ -27,18 +27,18 @@ MINION_ID = "test-host"
NODEGROUP_PATH = os.path.join("nodegroups", "test-group", "files")
HOST_PATH = os.path.join("hosts", MINION_ID, "files")
BASE_PILLAR_CONTENT = {"files": {"hostfile": b"base", "groupfile": b"base"}}
BASE_PILLAR_CONTENT = {"files": {"hostfile": "base", "groupfile": "base"}}
DEV_PILLAR_CONTENT = {
"files": {
"hostfile": b"base",
"groupfile": b"dev2",
"hostfile1": b"dev1",
"groupfile1": b"dev1",
"hostfile2": b"dev2",
"hostfile": "base",
"groupfile": "dev2",
"hostfile1": "dev1",
"groupfile1": "dev1",
"hostfile2": "dev2",
}
}
PARENT_PILLAR_CONTENT = {
"files": {"hostfile": b"base", "groupfile": b"base", "hostfile2": b"dev2"}
"files": {"hostfile": "base", "groupfile": "base", "hostfile2": "dev2"}
}
FILE_DATA = {
@ -159,3 +159,26 @@ class FileTreePillarTestCase(TestCase, LoaderModuleMockMixin):
break
else:
raise AssertionError("Did not find error message")
def test_file_tree_no_bytes(self):
"""
test file_tree pillar does not return bytes
"""
absolute_path = os.path.join(self.pillar_path, "base")
with patch(
"salt.utils.minions.CkMinions.check_minions",
MagicMock(return_value=_CHECK_MINIONS_RETURN),
):
mypillar = file_tree.ext_pillar(MINION_ID, None, absolute_path)
self.assertEqual(BASE_PILLAR_CONTENT, mypillar)
with patch.dict(file_tree.__opts__, {"pillarenv": "dev"}):
mypillar = file_tree.ext_pillar(MINION_ID, None, absolute_path)
for key,value in mypillar.items():
if isinstance(value, dict):
for ikey,ivalue in value.items():
self.assertTrue(isinstance(ikey, str))
self.assertTrue(isinstance(ivalue, str))
else:
self.assertTrue(isinstance(value, str))
self.assertTrue(isinstance(key, str))