Fix s3fs cache byte/str mismatch

This commit is contained in:
Ken Crowell 2020-05-20 21:41:13 -03:00 committed by Daniel Wozniak
parent e648f231d2
commit 22b9e30ad9
3 changed files with 44 additions and 3 deletions

1
changelog/53244.fixed Normal file
View file

@ -0,0 +1 @@
Fix s3fs cache byte/str mismatch

View file

@ -568,16 +568,23 @@ def _refresh_buckets_cache_file(cache_file):
metadata[saltenv].append({bucket_name: env_files})
# write the metadata to disk
_write_buckets_cache_file(metadata, cache_file)
return metadata
def _write_buckets_cache_file(metadata, cache_file):
"""
Write the contents of the buckets cache file
"""
if os.path.isfile(cache_file):
os.remove(cache_file)
log.debug("Writing buckets cache file")
with salt.utils.files.fopen(cache_file, "w") as fp_:
with salt.utils.files.fopen(cache_file, "wb") as fp_:
pickle.dump(metadata, fp_)
return metadata
def _read_buckets_cache_file(cache_file):
"""

View file

@ -0,0 +1,33 @@
# -*- coding: utf-8 -*-
# Import Python libs
from __future__ import absolute_import, print_function, unicode_literals
import tempfile
# Import Salt libs
import salt.fileserver.s3fs as s3fs
# Import Salt Testing libs
from tests.support.mixins import LoaderModuleMockMixin
from tests.support.runtests import RUNTIME_VARS
from tests.support.unit import TestCase
class S3fsFileTest(TestCase, LoaderModuleMockMixin):
def setup_loader_modules(self):
opts = {
"cachedir": self.tmp_cachedir,
}
return {s3fs: {"__opts__": opts}}
@classmethod
def setUpClass(cls):
cls.tmp_cachedir = tempfile.mkdtemp(dir=RUNTIME_VARS.TMP)
def test_cache_round_trip(self):
metadata = {"foo": "bar"}
cache_file = s3fs._get_cached_file_name("base", "fake_bucket", "some_file")
s3fs._write_buckets_cache_file(metadata, cache_file)
assert s3fs._read_buckets_cache_file(cache_file) == metadata