Fix caching pillar/grains which contain bytes

Use dump rather than dumps when storing to the cache, this makes it
symmetric with fetch and more importantly sets use_bin_type so that
loading will be able to distinguish between bytes and str correctly.
This commit is contained in:
Michael Birtwell 2020-07-10 12:28:32 +01:00 committed by Daniel Wozniak
parent 3237cf6214
commit a394c9bc03
3 changed files with 27 additions and 1 deletions

2
changelog/57918.fixed Normal file
View file

@ -0,0 +1,2 @@
Use "use_bin_type" to differentiate between bytes and str when writing cache
for pillar and grains.

View file

@ -60,7 +60,7 @@ def store(bank, key, data, cachedir):
os.close(tmpfh)
try:
with salt.utils.files.fopen(tmpfname, "w+b") as fh_:
fh_.write(__context__["serial"].dumps(data))
__context__["serial"].dump(data, fh_)
# On Windows, os.rename will fail if the destination file exists.
salt.utils.atomicfile.atomic_rename(tmpfname, outfile)
except IOError as exc:

View file

@ -296,3 +296,27 @@ class LocalFSTest(TestCase, LoaderModuleMockMixin):
# Now test the return of the contains function when key='key'
with patch.dict(localfs.__opts__, {"cachedir": tmp_dir}):
self.assertTrue(localfs.contains(bank="bank", key="key", cachedir=tmp_dir))
def test_mix_of_utf8_and_non_utf8_can_be_round_tripped(self):
tmp_dir = tempfile.mkdtemp(dir=RUNTIME_VARS.TMP)
data = {
# Any unicode, which ideally is invalid ascii.
"unicode": "áéí",
# Any bytes so long as they're not valid utf-8
"bytes": b"\xfe\x99\x00\xff",
}
bank = "bank"
key = "key"
self.addCleanup(shutil.rmtree, tmp_dir)
with patch.dict(localfs.__opts__, {"cachedir": tmp_dir}):
with patch.dict(
localfs.__context__, {"serial": salt.payload.Serial("msgpack")}
):
localfs.store(bank, key, data, tmp_dir)
actual = localfs.fetch(bank, key, tmp_dir)
self.assertEqual(data, actual)