mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
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:
parent
3237cf6214
commit
a394c9bc03
3 changed files with 27 additions and 1 deletions
2
changelog/57918.fixed
Normal file
2
changelog/57918.fixed
Normal file
|
@ -0,0 +1,2 @@
|
|||
Use "use_bin_type" to differentiate between bytes and str when writing cache
|
||||
for pillar and grains.
|
2
salt/cache/localfs.py
vendored
2
salt/cache/localfs.py
vendored
|
@ -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:
|
||||
|
|
24
tests/unit/cache/test_localfs.py
vendored
24
tests/unit/cache/test_localfs.py
vendored
|
@ -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)
|
||||
|
|
Loading…
Add table
Reference in a new issue