Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
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
5 changes: 5 additions & 0 deletions salt/modules/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,9 @@ def lchown(path, user, group):
else:
gid = -1

if err:
return err

return os.lchown(path, uid, gid)


Expand Down Expand Up @@ -3604,6 +3607,8 @@ def seek_write(path, data, offset):

salt '*' file.seek_write /path/to/file 'some data' 4096
"""
if isinstance(data, str):
data = data.encode()
path = os.path.expanduser(path)
seek_fh = os.open(path, os.O_WRONLY)
try:
Expand Down
78 changes: 76 additions & 2 deletions tests/pytests/unit/modules/file/test_file_basics.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import os
import pathlib
import shutil

import pytest
Expand All @@ -8,18 +9,20 @@
import salt.loader
import salt.modules.cmdmod as cmdmod
import salt.modules.config as configmod
import salt.modules.cp as cpmod
import salt.modules.file as filemod
import salt.utils.data
import salt.utils.files
import salt.utils.platform
import salt.utils.stringutils
from salt.exceptions import CommandExecutionError
from tests.support.mock import MagicMock, call, patch

log = logging.getLogger(__name__)


@pytest.fixture
def configure_loader_modules():
def configure_loader_modules(minion_opts):
return {
filemod: {
"__salt__": {
Expand All @@ -35,7 +38,8 @@ def configure_loader_modules():
"grains": {},
},
"__grains__": {"kernel": "Linux"},
}
},
cpmod: {"__context__": {}, "__opts__": minion_opts},
}


Expand Down Expand Up @@ -272,3 +276,73 @@ def test_source_list_for_list_returns_local_file_proto_from_dict(myfile):
):
ret = filemod.source_list([{"file://" + myfile: ""}], "filehash", "base")
assert list(ret) == ["file://" + myfile, "filehash"]


def test_set_mode_file_doesnotexist(tmp_path):
"""
Test set_mode when the file does not exist
"""
path = tmp_path / "path"
with pytest.raises(CommandExecutionError) as err:
filemod.set_mode(path, "0644")
assert err.value.message == f"{str(path)}: File not found"


def test_get_source_sum_cannot_cache(tmp_path):
"""
test get_source_sum when you cannot cache the source_hash
"""
path = tmp_path / "salt-3006.0rc3-onedir-linux-aarch64.tar.xz"
source = "https://repo.saltproject.io/salt_rc/salt/py3/onedir/latest/salt-3006.0rc3-onedir-linux-aarch64.tar.xz"
source_hash = "https://repo.saltproject.io/salt_rc/salt/py3/onedir/latest/salt-3006.0rc3-onedir-linux-aarch64.tar.xz.sha3_512"
with patch.dict(filemod.__salt__, {"cp.cache_file": MagicMock(return_value=False)}):
with pytest.raises(CommandExecutionError) as err:
ret = filemod.get_source_sum(path, source=source, source_hash=source_hash)
assert err.value.message == f"Source hash file {source_hash} not found"


def test_get_source_sum_invalid_proto(tmp_path):
"""
test get_source_sum when an invalid proto is being used
"""
path = tmp_path / "salt-3006.0rc3-onedir-linux-aarch64.tar.xz"
source = "https://repo.saltproject.io/salt_rc/salt/py3/onedir/latest/salt-3006.0rc3-onedir-linux-aarch64.tar.xz"
source_hash = "invalid://repo.saltproject.io/salt_rc/salt/py3/onedir/latest/salt-3006.0rc3-onedir-linux-aarch64.tar.xz.sha3_512"
with patch.dict(filemod.__salt__, {"cp.cache_file": MagicMock(return_value=False)}):
with pytest.raises(CommandExecutionError) as err:
ret = filemod.get_source_sum(path, source=source, source_hash=source_hash)
assert f"Source hash {source_hash} format is invalid." in err.value.message


def test_get_source_sum_invalid_proto_urllib(tmp_path):
"""
test get_source_sum when an invalid proto is being used
and urllib cannot parse it
"""
path = tmp_path / "salt-3006.0rc3-onedir-linux-aarch64.tar.xz"
source = "https://repo.saltproject.io/salt_rc/salt/py3/onedir/latest/salt-3006.0rc3-onedir-linux-aarch64.tar.xz"
source_hash = pathlib.Path("source_hash")
with patch.dict(filemod.__salt__, {"cp.cache_file": MagicMock(return_value=False)}):
with pytest.raises(CommandExecutionError) as err:
ret = filemod.get_source_sum(path, source=source, source_hash=source_hash)
assert f"Source hash {source_hash} format is invalid." in err.value.message


def test_get_source_sum_cannot_extract_hash(tmp_path):
"""
test get_source_sum when you cannot extract the hash
"""
path = tmp_path / "salt-3006.0rc3-onedir-linux-aarch64.tar.xz"
source = "https://repo.saltproject.io/salt_rc/salt/py3/onedir/latest/salt-3006.0rc3-onedir-linux-aarch64.tar.xz"
source_hash = "https://repo.saltproject.io/salt_rc/salt/py3/onedir/latest/salt-3006.0rc3-onedir-linux-aarch64.tar.xz.sha3_512"
mock_extract = MagicMock(return_value=None)
patch_extract = patch("salt.modules.file.extract_hash", mock_extract)
cache_path = tmp_path / "cache"
patch_salt = patch.dict(
filemod.__salt__, {"cp.cache_file": MagicMock(return_value=cache_path)}
)
with patch_extract, patch_salt:
with pytest.raises(CommandExecutionError) as err:
ret = filemod.get_source_sum(path, source=source, source_hash=source_hash)
assert mock_extract.call_args == call(cache_path, "", path, source, None)
assert f"Source hash {source_hash} format is invalid." in err.value.message
52 changes: 51 additions & 1 deletion tests/pytests/unit/modules/file/test_file_chattr.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

import salt.modules.cmdmod as cmdmod
import salt.modules.file as filemod
from tests.support.mock import MagicMock, Mock, patch
from salt.exceptions import SaltInvocationError
from tests.support.mock import MagicMock, Mock, call, patch

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -242,3 +243,52 @@ def fake_cmd(cmd, *args, **kwargs):
follow_symlinks=False,
)
assert actual_ret["changes"] == expected


@pytest.mark.parametrize(
"operator",
["addz", None, "doesnotexist", 1],
)
def test_file_chattr_invalid_operator(operator):
"""
Test file.chattr when passing in an invalid operator
"""
with pytest.raises(SaltInvocationError) as exc:
filemod.chattr(operator=operator)
assert (
exc.value.message == "Need an operator: 'add' or 'remove' to modify attributes."
)


def test_file_chattr_none_attributes():
"""
Test file.chattr when attributes is None
"""
with pytest.raises(SaltInvocationError) as exc:
filemod.chattr(operator="add")
assert exc.value.message == "Need attributes: [aAcCdDeijPsStTu]"


def test_file_chattr_remove():
"""
Test file.chattr when operator is remove
"""
mock_cmd = MagicMock()
patch_cmd = patch.dict(filemod.__salt__, {"cmd.run": mock_cmd})
with patch_cmd:
filemod.chattr(operator="remove", attributes="aCd")
assert mock_cmd.call_args_list == [call(["chattr", "-aCd"], python_shell=False)]


def test_file_chattr_flags():
"""
Test file.chattr when using flags
"""
mock_cmd = MagicMock(return_value="")
patch_cmd = patch.dict(filemod.__salt__, {"cmd.run": mock_cmd})
with patch_cmd:
ret = filemod.chattr(operator="remove", attributes="aCd", flags="R")
assert mock_cmd.call_args_list == [
call(["chattr", "-aCd", "-R"], python_shell=False)
]
assert ret is True
Loading
Loading