Add support for test=True to file.cached

This commit is contained in:
Twangboy 2023-04-28 10:18:16 -06:00 committed by Pedro Algarvio
parent 34f2ed4b4e
commit 3448a6c990
3 changed files with 116 additions and 0 deletions

1
changelog/63785.fixed.md Normal file
View file

@ -0,0 +1 @@
Added support for ``test=True`` to the ``file.cached`` state module

View file

@ -8937,6 +8937,25 @@ def cached(
else:
source_sum = {}
if __opts__["test"]:
local_copy = __salt__["cp.is_cached"](name, saltenv=saltenv)
if local_copy:
if source_sum:
hash = __salt__["file.get_hash"](local_copy, __opts__["hash_type"])
if hash == source_sum["hsum"]:
ret["comment"] = "File already cached: {}".format(name)
else:
ret[
"comment"
] = "Hashes don't match.\nFile will be cached: {}".format(name)
else:
ret["comment"] = "No hash found. File will be cached: {}".format(name)
else:
ret["comment"] = "File will be cached: {}".format(name)
ret["changes"] = {}
ret["result"] = None
return ret
if parsed.scheme in salt.utils.files.LOCAL_PROTOS:
# Source is a local file path
full_path = os.path.realpath(os.path.expanduser(parsed.path))

View file

@ -0,0 +1,96 @@
import secrets
import pytest
import salt.states.file as file
from tests.support.mock import MagicMock, patch
pytestmark = [
pytest.mark.windows_whitelisted,
]
@pytest.fixture
def configure_loader_modules():
return {
file: {"__opts__": {"test": False}},
}
def test_cached_test_true():
name = "salt://test/file.exe"
source_hash = secrets.token_hex(nbytes=32)
expected = {
"changes": {},
"comment": "File will be cached: {}".format(name),
"name": name,
"result": None,
}
salt = {
"cp.is_cached": MagicMock(return_value=""),
"file.get_source_sum": MagicMock(return_value={"hsum": source_hash}),
}
opts = {"test": True}
with patch.dict(file.__salt__, salt), patch.dict(file.__opts__, opts):
result = file.cached(name=name, source_hash=source_hash)
assert result == expected
def test_cached_present_test_true():
name = "salt://test/file.exe"
source_hash = secrets.token_hex(nbytes=32)
expected = {
"changes": {},
"comment": "File already cached: {}".format(name),
"name": name,
"result": None,
}
salt = {
"cp.is_cached": MagicMock(return_value="path/to/file"),
"file.get_hash": MagicMock(return_value=source_hash),
"file.get_source_sum": MagicMock(return_value={"hsum": source_hash}),
}
opts = {"test": True, "hash_type": "sha256"}
with patch.dict(file.__salt__, salt), patch.dict(file.__opts__, opts):
result = file.cached(name=name, source_hash=source_hash)
assert result == expected
def test_cached_present_different_hash_test_true():
name = "salt://test/file.exe"
source_hash = secrets.token_hex(nbytes=32)
existing_hash = secrets.token_hex(nbytes=32)
expected = {
"changes": {},
"comment": "Hashes don't match.\nFile will be cached: {}".format(name),
"name": name,
"result": None,
}
salt = {
"cp.is_cached": MagicMock(return_value="path/to/file"),
"file.get_hash": MagicMock(return_value=existing_hash),
"file.get_source_sum": MagicMock(return_value={"hsum": source_hash}),
}
opts = {"test": True, "hash_type": "sha256"}
with patch.dict(file.__salt__, salt), patch.dict(file.__opts__, opts):
result = file.cached(name=name, source_hash=source_hash)
assert result == expected
def test_cached_present_no_source_hash_test_true():
name = "salt://test/file.exe"
existing_hash = secrets.token_hex(nbytes=32)
expected = {
"changes": {},
"comment": "No hash found. File will be cached: {}".format(name),
"name": name,
"result": None,
}
salt = {
"cp.is_cached": MagicMock(return_value="path/to/file"),
"file.get_hash": MagicMock(return_value=existing_hash),
}
opts = {"test": True, "hash_type": "sha256"}
with patch.dict(file.__salt__, salt), patch.dict(file.__opts__, opts):
result = file.cached(name=name)
assert result == expected