Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for empty pillar cache disk file and not call msgpack to load it, with test #63732

Merged
merged 4 commits into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/63729.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Check file is not empty before attempting to read pillar disk cache file
4 changes: 4 additions & 0 deletions salt/utils/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ def _read(self):
if not salt.utils.msgpack.HAS_MSGPACK or not os.path.exists(self._path):
return

if 0 == os.path.getsize(self._path):
# File exists but empty, treat as empty cache
return

try:
with salt.utils.files.fopen(self._path, "rb") as fp_:
cache = salt.utils.msgpack.load(
Expand Down
34 changes: 34 additions & 0 deletions tests/pytests/unit/pillar/test_pillar.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import logging
from pathlib import Path

import pytest

import salt.loader
import salt.pillar
import salt.utils.cache
from salt.utils.odict import OrderedDict


Expand Down Expand Up @@ -123,3 +127,33 @@ def test_pillar_envs_path_substitution(env, temp_salt_minion, tmp_path):

# The __env__ string in the path has been substituted for the actual env
assert pillar.opts["pillar_roots"] == expected


def test_pillar_get_cache_disk(temp_salt_minion, caplog):
# create faked path for cache
with pytest.helpers.temp_directory() as temp_path:
tmp_cachedir = Path(str(temp_path) + "/pillar_cache/")
tmp_cachedir.mkdir(parents=True)
assert tmp_cachedir.exists()
tmp_cachefile = Path(str(temp_path) + "/pillar_cache/" + temp_salt_minion.id)
tmp_cachefile.touch()
assert tmp_cachefile.exists()

opts = temp_salt_minion.config.copy()
opts["pillarenv"] = None
opts["pillar_cache"] = True
opts["cachedir"] = str(temp_path)

caplog.at_level(logging.DEBUG)
pillar = salt.pillar.PillarCache(
opts=opts,
grains=salt.loader.grains(opts),
minion_id=temp_salt_minion.id,
saltenv="base",
)
fresh_pillar = pillar.fetch_pillar()
assert not (
f"Error reading cache file at '{tmp_cachefile}': Unpack failed: incomplete input"
in caplog.messages
)
assert fresh_pillar == {}
15 changes: 12 additions & 3 deletions tests/pytests/unit/utils/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Test the salt cache objects
"""

import logging
import pathlib
import time

Expand Down Expand Up @@ -221,7 +222,7 @@ def test_everything(cache_dir):
b"\xc3\x83\xc2\xa6\xc3\x83\xc2\xb8\xc3\x83\xc2\xa5",
],
)
def test_unicode_error(cache_dir, data):
def test_unicode_error(cache_dir, data, caplog):
"""
Test when the data in the cache raises a UnicodeDecodeError
we do not raise an error.
Expand All @@ -240,9 +241,17 @@ def test_unicode_error(cache_dir, data):
}
}
}
with patch.object(salt.utils.msgpack, "load", return_value=cache_data):
with patch.object(
salt.utils.msgpack, "load", return_value=cache_data
), caplog.at_level(logging.DEBUG):
cd = cache.CacheDisk(0.3, str(path))
assert cd._dict == cache_data
# this test used to rely on msgpack throwing errors if attempt to read an empty file
# code now checks if file empty and returns, so we should never attempt msgpack load
assert cd._dict == {}
assert not (
f"Error reading cache file at '{path}': Unpack failed: incomplete input"
in caplog.messages
)


def test_cache_corruption(cache_dir):
Expand Down