mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Merge 3006.x into master
This commit is contained in:
commit
5cba721cf9
18 changed files with 421 additions and 116 deletions
2
changelog/65411.fixed.md
Normal file
2
changelog/65411.fixed.md
Normal file
|
@ -0,0 +1,2 @@
|
|||
Fixes an issue setting user or machine policy on Windows when the Group Policy
|
||||
directory is missing
|
|
@ -4,6 +4,8 @@ Salt compatibility code
|
|||
# pylint: disable=unused-import
|
||||
import sys
|
||||
|
||||
# pragma: no cover
|
||||
|
||||
# The ipaddress module included in Salt is from Python 3.9.5.
|
||||
# When running from Py3.9.5+ use the standard library module, use ours otherwise
|
||||
if sys.version_info >= (3, 9, 5):
|
||||
|
|
|
@ -16,8 +16,10 @@ import stat
|
|||
import sys
|
||||
import tempfile
|
||||
|
||||
import salt.utils.files
|
||||
import salt.utils.path
|
||||
import salt.utils.platform
|
||||
import salt.utils.user
|
||||
from salt.exceptions import CommandExecutionError, SaltInvocationError
|
||||
from salt.modules.file import (
|
||||
__clean_tmp,
|
||||
|
@ -107,6 +109,15 @@ try:
|
|||
except ImportError:
|
||||
HAS_WINDOWS_MODULES = False
|
||||
|
||||
HAS_WIN_DACL = False
|
||||
try:
|
||||
if salt.utils.platform.is_windows():
|
||||
import salt.utils.win_dacl
|
||||
|
||||
HAS_WIN_DACL = True
|
||||
except ImportError:
|
||||
HAS_WIN_DACL = False
|
||||
|
||||
if salt.utils.platform.is_windows():
|
||||
if HAS_WINDOWS_MODULES:
|
||||
# namespace functions from file.py
|
||||
|
@ -194,6 +205,8 @@ def __virtual__():
|
|||
"""
|
||||
if not salt.utils.platform.is_windows() or not HAS_WINDOWS_MODULES:
|
||||
return False, "Module win_file: Missing Win32 modules"
|
||||
if not HAS_WIN_DACL:
|
||||
return False, "Module win_file: Unable to load salt.utils.win_dacl"
|
||||
return __virtualname__
|
||||
|
||||
|
||||
|
@ -305,7 +318,7 @@ def group_to_gid(group):
|
|||
if group is None:
|
||||
return ""
|
||||
|
||||
return __utils__["dacl.get_sid_string"](group)
|
||||
return salt.utils.win_dacl.get_sid_string(group)
|
||||
|
||||
|
||||
def get_pgid(path, follow_symlinks=True):
|
||||
|
@ -346,8 +359,8 @@ def get_pgid(path, follow_symlinks=True):
|
|||
if follow_symlinks and sys.getwindowsversion().major >= 6:
|
||||
path = _resolve_symlink(path)
|
||||
|
||||
group_name = __utils__["dacl.get_primary_group"](path)
|
||||
return __utils__["dacl.get_sid_string"](group_name)
|
||||
group_name = salt.utils.win_dacl.get_primary_group(path)
|
||||
return salt.utils.win_dacl.get_sid_string(group_name)
|
||||
|
||||
|
||||
def get_pgroup(path, follow_symlinks=True):
|
||||
|
@ -498,7 +511,7 @@ def uid_to_user(uid):
|
|||
if uid is None or uid == "":
|
||||
return ""
|
||||
|
||||
return __utils__["dacl.get_name"](uid)
|
||||
return salt.utils.win_dacl.get_name(uid)
|
||||
|
||||
|
||||
def user_to_uid(user):
|
||||
|
@ -518,9 +531,9 @@ def user_to_uid(user):
|
|||
salt '*' file.user_to_uid myusername
|
||||
"""
|
||||
if user is None:
|
||||
user = __utils__["user.get_user"]()
|
||||
user = salt.utils.user.get_user()
|
||||
|
||||
return __utils__["dacl.get_sid_string"](user)
|
||||
return salt.utils.win_dacl.get_sid_string(user)
|
||||
|
||||
|
||||
def get_uid(path, follow_symlinks=True):
|
||||
|
@ -558,8 +571,8 @@ def get_uid(path, follow_symlinks=True):
|
|||
if follow_symlinks and sys.getwindowsversion().major >= 6:
|
||||
path = _resolve_symlink(path)
|
||||
|
||||
owner_sid = __utils__["dacl.get_owner"](path)
|
||||
return __utils__["dacl.get_sid_string"](owner_sid)
|
||||
owner_sid = salt.utils.win_dacl.get_owner(path)
|
||||
return salt.utils.win_dacl.get_sid_string(owner_sid)
|
||||
|
||||
|
||||
def get_user(path, follow_symlinks=True):
|
||||
|
@ -597,7 +610,7 @@ def get_user(path, follow_symlinks=True):
|
|||
if follow_symlinks and sys.getwindowsversion().major >= 6:
|
||||
path = _resolve_symlink(path)
|
||||
|
||||
return __utils__["dacl.get_owner"](path)
|
||||
return salt.utils.win_dacl.get_owner(path)
|
||||
|
||||
|
||||
def get_mode(path):
|
||||
|
@ -735,9 +748,9 @@ def chown(path, user, group=None, pgroup=None, follow_symlinks=True):
|
|||
if not os.path.exists(path):
|
||||
raise CommandExecutionError(f"Path not found: {path}")
|
||||
|
||||
__utils__["dacl.set_owner"](path, user)
|
||||
salt.utils.win_dacl.set_owner(path, user)
|
||||
if pgroup:
|
||||
__utils__["dacl.set_primary_group"](path, pgroup)
|
||||
salt.utils.win_dacl.set_primary_group(path, pgroup)
|
||||
|
||||
return True
|
||||
|
||||
|
@ -767,7 +780,7 @@ def chpgrp(path, group):
|
|||
salt '*' file.chpgrp c:\\temp\\test.txt Administrators
|
||||
salt '*' file.chpgrp c:\\temp\\test.txt "'None'"
|
||||
"""
|
||||
return __utils__["dacl.set_primary_group"](path, group)
|
||||
return salt.utils.win_dacl.set_primary_group(path, group)
|
||||
|
||||
|
||||
def chgrp(path, group):
|
||||
|
@ -802,7 +815,7 @@ def chgrp(path, group):
|
|||
|
||||
.. code-block:: bash
|
||||
|
||||
salt '*' file.chpgrp c:\\temp\\test.txt administrators
|
||||
salt '*' file.chgrp c:\\temp\\test.txt administrators
|
||||
"""
|
||||
func_name = f"{__virtualname__}.chgrp"
|
||||
if __opts__.get("fun", "") == func_name:
|
||||
|
@ -871,7 +884,7 @@ def stats(path, hash_type="sha256", follow_symlinks=True):
|
|||
ret["mtime"] = pstat.st_mtime
|
||||
ret["ctime"] = pstat.st_ctime
|
||||
ret["size"] = pstat.st_size
|
||||
ret["mode"] = __utils__["files.normalize_mode"](oct(stat.S_IMODE(pstat.st_mode)))
|
||||
ret["mode"] = salt.utils.files.normalize_mode(oct(stat.S_IMODE(pstat.st_mode)))
|
||||
if hash_type:
|
||||
ret["sum"] = get_sum(path, hash_type)
|
||||
ret["type"] = "file"
|
||||
|
@ -1513,7 +1526,7 @@ def is_link(path):
|
|||
)
|
||||
|
||||
try:
|
||||
return __utils__["path.islink"](path)
|
||||
return salt.utils.path.islink(path)
|
||||
except Exception as exc: # pylint: disable=broad-except
|
||||
raise CommandExecutionError(exc)
|
||||
|
||||
|
@ -1604,10 +1617,10 @@ def mkdir(
|
|||
|
||||
# Set owner
|
||||
if owner:
|
||||
__utils__["dacl.set_owner"](obj_name=path, principal=owner)
|
||||
salt.utils.win_dacl.set_owner(obj_name=path, principal=owner)
|
||||
|
||||
# Set permissions
|
||||
__utils__["dacl.set_perms"](
|
||||
salt.utils.win_dacl.set_perms(
|
||||
obj_name=path,
|
||||
obj_type="file",
|
||||
grant_perms=grant_perms,
|
||||
|
@ -1926,7 +1939,7 @@ def check_perms(
|
|||
|
||||
path = os.path.expanduser(path)
|
||||
|
||||
return __utils__["dacl.check_perms"](
|
||||
return salt.utils.win_dacl.check_perms(
|
||||
obj_name=path,
|
||||
obj_type="file",
|
||||
ret=ret,
|
||||
|
@ -1935,6 +1948,7 @@ def check_perms(
|
|||
deny_perms=deny_perms,
|
||||
inheritance=inheritance,
|
||||
reset=reset,
|
||||
test_mode=__opts__["test"],
|
||||
)
|
||||
|
||||
|
||||
|
@ -2012,7 +2026,7 @@ def set_perms(path, grant_perms=None, deny_perms=None, inheritance=True, reset=F
|
|||
# Specify advanced attributes with a list
|
||||
salt '*' file.set_perms C:\\Temp\\ "{'jsnuffy': {'perms': ['read_attributes', 'read_ea'], 'applies_to': 'this_folder_only'}}"
|
||||
"""
|
||||
return __utils__["dacl.set_perms"](
|
||||
return salt.utils.win_dacl.set_perms(
|
||||
obj_name=path,
|
||||
obj_type="file",
|
||||
grant_perms=grant_perms,
|
||||
|
|
|
@ -2349,7 +2349,7 @@ def check_perms(
|
|||
deny_perms=None,
|
||||
inheritance=True,
|
||||
reset=False,
|
||||
test_mode=None,
|
||||
test_mode=False,
|
||||
):
|
||||
"""
|
||||
Check owner and permissions for the passed directory. This function checks
|
||||
|
@ -2429,9 +2429,6 @@ def check_perms(
|
|||
}
|
||||
})
|
||||
"""
|
||||
if test_mode is None:
|
||||
test_mode = __opts__["test"]
|
||||
|
||||
# Validate obj_type
|
||||
if obj_type.lower() not in flags().obj_type:
|
||||
raise SaltInvocationError('Invalid "obj_type" passed: {}'.format(obj_type))
|
||||
|
|
|
@ -230,7 +230,7 @@ class MacPowerModuleTestRestartPowerFailure(ModuleCase):
|
|||
Reset to original value
|
||||
"""
|
||||
if self.RESTART_POWER is not None:
|
||||
self.run_function("power.set_sleep_on_power_button", [self.SLEEP_ON_BUTTON])
|
||||
self.run_function("power.set_sleep_on_power_button", [self.RESTART_POWER])
|
||||
|
||||
def test_restart_power_failure(self):
|
||||
"""
|
||||
|
|
|
@ -56,6 +56,8 @@ def macports_package_url(macports_package_filename):
|
|||
|
||||
@pytest.fixture(scope="module")
|
||||
def pkg_name(grains):
|
||||
if grains["osrelease_info"][0] >= 12:
|
||||
return "com.apple.pkg.XcodeSystemResources"
|
||||
if grains["osrelease_info"][0] >= 11:
|
||||
return "com.apple.pkg.InstallAssistant.macOSBigSur"
|
||||
if grains["osrelease_info"][:2] == (10, 15):
|
||||
|
|
|
@ -21,9 +21,9 @@ def configure_loader_modules():
|
|||
"__utils__": {
|
||||
"dacl.check_perms": win_dacl.check_perms,
|
||||
"dacl.set_perms": win_dacl.set_perms,
|
||||
}
|
||||
},
|
||||
"__opts__": {"test": False},
|
||||
},
|
||||
win_dacl: {"__opts__": {"test": False}},
|
||||
}
|
||||
|
||||
|
||||
|
@ -43,7 +43,7 @@ def test_check_perms_set_owner_test_true(test_file):
|
|||
"name": str(test_file),
|
||||
"result": None,
|
||||
}
|
||||
with patch.dict(win_dacl.__opts__, {"test": True}):
|
||||
with patch.dict(win_file.__opts__, {"test": True}):
|
||||
result = win_file.check_perms(
|
||||
path=str(test_file), owner="Backup Operators", inheritance=None
|
||||
)
|
||||
|
@ -76,7 +76,7 @@ def test_check_perms_deny_test_true(test_file):
|
|||
"name": str(test_file),
|
||||
"result": None,
|
||||
}
|
||||
with patch.dict(win_dacl.__opts__, {"test": True}):
|
||||
with patch.dict(win_file.__opts__, {"test": True}):
|
||||
result = win_file.check_perms(
|
||||
path=str(test_file),
|
||||
deny_perms={"Users": {"perms": "read_execute"}},
|
||||
|
@ -113,7 +113,7 @@ def test_check_perms_grant_test_true(test_file):
|
|||
"name": str(test_file),
|
||||
"result": None,
|
||||
}
|
||||
with patch.dict(win_dacl.__opts__, {"test": True}):
|
||||
with patch.dict(win_file.__opts__, {"test": True}):
|
||||
result = win_file.check_perms(
|
||||
path=str(test_file),
|
||||
grant_perms={"Users": {"perms": "read_execute"}},
|
||||
|
@ -150,7 +150,7 @@ def test_check_perms_inheritance_false_test_true(test_file):
|
|||
"name": str(test_file),
|
||||
"result": None,
|
||||
}
|
||||
with patch.dict(win_dacl.__opts__, {"test": True}):
|
||||
with patch.dict(win_file.__opts__, {"test": True}):
|
||||
result = win_file.check_perms(path=str(test_file), inheritance=False)
|
||||
assert result == expected
|
||||
|
||||
|
@ -214,7 +214,7 @@ def test_check_perms_reset_test_true(test_file):
|
|||
"name": str(test_file),
|
||||
"result": None,
|
||||
}
|
||||
with patch.dict(win_dacl.__opts__, {"test": True}):
|
||||
with patch.dict(win_file.__opts__, {"test": True}):
|
||||
result = win_file.check_perms(
|
||||
path=str(test_file),
|
||||
grant_perms={
|
||||
|
|
|
@ -2,8 +2,6 @@ import os
|
|||
|
||||
import pytest
|
||||
|
||||
import salt.modules.win_file as win_file
|
||||
import salt.states.file as file
|
||||
import salt.utils.win_dacl as win_dacl
|
||||
import salt.utils.win_functions as win_functions
|
||||
|
||||
|
@ -20,28 +18,7 @@ pytestmark = [
|
|||
]
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def configure_loader_modules():
|
||||
return {
|
||||
file: {
|
||||
"__opts__": {"test": False},
|
||||
"__salt__": {
|
||||
"file.mkdir": win_file.mkdir,
|
||||
"file.check_perms": win_file.check_perms,
|
||||
},
|
||||
},
|
||||
win_file: {
|
||||
"__utils__": {
|
||||
"dacl.check_perms": win_dacl.check_perms,
|
||||
"dacl.set_owner": win_dacl.set_owner,
|
||||
"dacl.set_perms": win_dacl.set_perms,
|
||||
},
|
||||
},
|
||||
win_dacl: {"__opts__": {"test": False}},
|
||||
}
|
||||
|
||||
|
||||
def test_directory_new(tmp_path):
|
||||
def test_directory_new(file, tmp_path):
|
||||
"""
|
||||
Test file.directory when the directory does not exist
|
||||
Should just return "New Dir"
|
||||
|
@ -107,7 +84,7 @@ def test_directory_new(tmp_path):
|
|||
assert permissions == expected
|
||||
|
||||
|
||||
def test_directory_new_no_inherit(tmp_path):
|
||||
def test_directory_new_no_inherit(file, tmp_path):
|
||||
"""
|
||||
Test file.directory when the directory does not exist
|
||||
Should just return "New Dir"
|
||||
|
@ -127,7 +104,7 @@ def test_directory_new_no_inherit(tmp_path):
|
|||
assert permissions["Inherited"] == {}
|
||||
|
||||
|
||||
def test_directory_new_reset(tmp_path):
|
||||
def test_directory_new_reset(file, tmp_path):
|
||||
"""
|
||||
Test file.directory when the directory does not exist
|
||||
Should just return "New Dir"
|
||||
|
@ -182,7 +159,7 @@ def test_directory_new_reset(tmp_path):
|
|||
assert permissions == expected
|
||||
|
||||
|
||||
def test_directory_new_reset_no_inherit(tmp_path):
|
||||
def test_directory_new_reset_no_inherit(file, tmp_path):
|
||||
"""
|
||||
Test file.directory when the directory does not exist
|
||||
Should just return "New Dir"
|
||||
|
@ -219,7 +196,7 @@ def test_directory_new_reset_no_inherit(tmp_path):
|
|||
assert permissions == expected
|
||||
|
||||
|
||||
def test_directory_existing(tmp_path):
|
||||
def test_directory_existing(file, tmp_path):
|
||||
path = str(tmp_path)
|
||||
ret = file.directory(
|
||||
name=path,
|
||||
|
@ -293,7 +270,7 @@ def test_directory_existing(tmp_path):
|
|||
assert permissions == expected
|
||||
|
||||
|
||||
def test_directory_existing_existing_user(tmp_path):
|
||||
def test_directory_existing_existing_user(file, tmp_path):
|
||||
path = str(tmp_path)
|
||||
win_dacl.set_permissions(
|
||||
obj_name=path,
|
||||
|
@ -374,7 +351,7 @@ def test_directory_existing_existing_user(tmp_path):
|
|||
assert permissions == expected
|
||||
|
||||
|
||||
def test_directory_existing_no_inherit(tmp_path):
|
||||
def test_directory_existing_no_inherit(file, tmp_path):
|
||||
path = str(tmp_path)
|
||||
ret = file.directory(
|
||||
name=path,
|
||||
|
@ -398,7 +375,7 @@ def test_directory_existing_no_inherit(tmp_path):
|
|||
assert permissions["Inherited"] == {}
|
||||
|
||||
|
||||
def test_directory_existing_reset(tmp_path):
|
||||
def test_directory_existing_reset(file, tmp_path):
|
||||
path = str(tmp_path)
|
||||
win_dacl.set_permissions(
|
||||
obj_name=path,
|
||||
|
@ -462,7 +439,7 @@ def test_directory_existing_reset(tmp_path):
|
|||
assert permissions == expected
|
||||
|
||||
|
||||
def test_directory_existing_reset_no_inherit(tmp_path):
|
||||
def test_directory_existing_reset_no_inherit(file, tmp_path):
|
||||
path = str(tmp_path)
|
||||
ret = file.directory(
|
||||
name=path,
|
||||
|
|
|
@ -49,7 +49,6 @@ def configure_loader_modules():
|
|||
"dacl.check_perms": win_dacl.check_perms,
|
||||
},
|
||||
},
|
||||
win_dacl: {"__opts__": {"test": False}},
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -138,6 +138,19 @@ def test_destroy(sreq, echo_server):
|
|||
"""
|
||||
Test the __del__ capabilities
|
||||
"""
|
||||
# ensure we actually have an open socket and not just testing against
|
||||
# no actual sockets created.
|
||||
assert sreq.send("clear", "foo") == {"enc": "clear", "load": "foo"}
|
||||
# ensure no exceptions when we go to destroy the sreq, since __del__
|
||||
# swallows exceptions, we have to call destroy directly
|
||||
sreq.destroy()
|
||||
|
||||
|
||||
@pytest.mark.slow_test
|
||||
def test_clear_socket(sreq, echo_server):
|
||||
# ensure we actually have an open socket and not just testing against
|
||||
# no actual sockets created.
|
||||
assert sreq.send("clear", "foo") == {"enc": "clear", "load": "foo"}
|
||||
assert hasattr(sreq, "_socket")
|
||||
sreq.clear_socket()
|
||||
assert hasattr(sreq, "_socket") is False
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
import pytest
|
||||
|
||||
import salt.utils.win_dacl as win_dacl
|
||||
from tests.support.mock import patch
|
||||
|
||||
pytestmark = [
|
||||
pytest.mark.windows_whitelisted,
|
||||
|
@ -819,22 +818,22 @@ def test_check_perms(test_file):
|
|||
|
||||
|
||||
def test_check_perms_test_true(test_file):
|
||||
with patch.dict(win_dacl.__opts__, {"test": True}):
|
||||
result = win_dacl.check_perms(
|
||||
obj_name=str(test_file),
|
||||
obj_type="file",
|
||||
ret=None,
|
||||
owner="Users",
|
||||
grant_perms={"Backup Operators": {"perms": "read"}},
|
||||
deny_perms={
|
||||
"NETWORK SERVICE": {
|
||||
"perms": ["delete", "set_value", "write_dac", "write_owner"]
|
||||
},
|
||||
"Backup Operators": {"perms": ["delete"]},
|
||||
result = win_dacl.check_perms(
|
||||
obj_name=str(test_file),
|
||||
obj_type="file",
|
||||
ret=None,
|
||||
owner="Users",
|
||||
grant_perms={"Backup Operators": {"perms": "read"}},
|
||||
deny_perms={
|
||||
"NETWORK SERVICE": {
|
||||
"perms": ["delete", "set_value", "write_dac", "write_owner"]
|
||||
},
|
||||
inheritance=True,
|
||||
reset=False,
|
||||
)
|
||||
"Backup Operators": {"perms": ["delete"]},
|
||||
},
|
||||
inheritance=True,
|
||||
reset=False,
|
||||
test_mode=True,
|
||||
)
|
||||
|
||||
expected = {
|
||||
"changes": {
|
||||
|
|
|
@ -3,7 +3,6 @@ from saltfactories.utils import random_string
|
|||
|
||||
import salt.utils.win_dacl as win_dacl
|
||||
import salt.utils.win_reg as win_reg
|
||||
from tests.support.mock import patch
|
||||
|
||||
pytestmark = [
|
||||
pytest.mark.windows_whitelisted,
|
||||
|
@ -12,15 +11,6 @@ pytestmark = [
|
|||
]
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def configure_loader_modules(minion_opts):
|
||||
return {
|
||||
win_dacl: {
|
||||
"__opts__": minion_opts,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def fake_key():
|
||||
return "SOFTWARE\\{}".format(random_string("SaltTesting-", lowercase=False))
|
||||
|
@ -433,22 +423,22 @@ def test_check_perms(reg_key):
|
|||
|
||||
|
||||
def test_check_perms_test_true(reg_key):
|
||||
with patch.dict(win_dacl.__opts__, {"test": True}):
|
||||
result = win_dacl.check_perms(
|
||||
obj_name=reg_key,
|
||||
obj_type="registry",
|
||||
ret=None,
|
||||
owner="Users",
|
||||
grant_perms={"Backup Operators": {"perms": "read"}},
|
||||
deny_perms={
|
||||
"NETWORK SERVICE": {
|
||||
"perms": ["delete", "set_value", "write_dac", "write_owner"]
|
||||
},
|
||||
"Backup Operators": {"perms": ["delete"]},
|
||||
result = win_dacl.check_perms(
|
||||
obj_name=reg_key,
|
||||
obj_type="registry",
|
||||
ret=None,
|
||||
owner="Users",
|
||||
grant_perms={"Backup Operators": {"perms": "read"}},
|
||||
deny_perms={
|
||||
"NETWORK SERVICE": {
|
||||
"perms": ["delete", "set_value", "write_dac", "write_owner"]
|
||||
},
|
||||
inheritance=True,
|
||||
reset=False,
|
||||
)
|
||||
"Backup Operators": {"perms": ["delete"]},
|
||||
},
|
||||
inheritance=True,
|
||||
reset=False,
|
||||
test_mode=True,
|
||||
)
|
||||
|
||||
expected = {
|
||||
"changes": {
|
||||
|
|
|
@ -150,3 +150,14 @@ def test_invalid_kwargs_are_ignored(client, auth_creds):
|
|||
ret = client.cmd_sync(low.copy())
|
||||
assert ret
|
||||
assert ret[0] == "foo"
|
||||
|
||||
|
||||
def test_get_docs(client):
|
||||
ret = client.get_docs(arg="*")
|
||||
assert "auth.del_token" in ret
|
||||
assert "auth.mk_token" in ret
|
||||
assert "cache.clear_pillar" in ret
|
||||
assert "cache.grains" in ret
|
||||
assert "state.soft_kill" in ret
|
||||
assert "virt.start" in ret
|
||||
assert "test.arg" in ret
|
||||
|
|
|
@ -57,7 +57,10 @@ def configure_loader_modules():
|
|||
ret.update(
|
||||
{
|
||||
win_dacl: {"__opts__": opts},
|
||||
win_file: {"__utils__": {"dacl.check_perms": win_dacl.check_perms}},
|
||||
win_file: {
|
||||
"__utils__": {"dacl.check_perms": win_dacl.check_perms},
|
||||
"__opts__": opts,
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
|
|
|
@ -1,13 +1,43 @@
|
|||
import os
|
||||
import re
|
||||
|
||||
import pytest
|
||||
|
||||
import salt.modules.win_file as win_file
|
||||
import salt.utils.user
|
||||
import salt.utils.win_dacl
|
||||
from salt.exceptions import CommandExecutionError
|
||||
from tests.support.mock import patch
|
||||
|
||||
pytestmark = [pytest.mark.windows_whitelisted, pytest.mark.skip_unless_on_windows]
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def configure_loader_modules():
|
||||
return {
|
||||
win_file: {},
|
||||
salt.utils.win_dacl: {},
|
||||
}
|
||||
|
||||
|
||||
def test__virtual__not_windows():
|
||||
with patch("salt.utils.platform.is_windows", autospec=True, return_value=False):
|
||||
expected = (False, "Module win_file: Missing Win32 modules")
|
||||
result = win_file.__virtual__()
|
||||
assert result == expected
|
||||
with patch("salt.modules.win_file.HAS_WINDOWS_MODULES", False):
|
||||
expected = (False, "Module win_file: Missing Win32 modules")
|
||||
result = win_file.__virtual__()
|
||||
assert result == expected
|
||||
|
||||
|
||||
def test__virtual__no_dacl():
|
||||
with patch("salt.modules.win_file.HAS_WIN_DACL", False):
|
||||
expected = (False, "Module win_file: Unable to load salt.utils.win_dacl")
|
||||
result = win_file.__virtual__()
|
||||
assert result == expected
|
||||
|
||||
|
||||
def test__get_version_os():
|
||||
expected = ["32-bit Windows", "Windows NT"]
|
||||
result = win_file._get_version_os(0x00040004)
|
||||
|
@ -56,6 +86,187 @@ def test__get_version_sys():
|
|||
assert regex.search(result)
|
||||
|
||||
|
||||
def test_get_pgid_error():
|
||||
with pytest.raises(CommandExecutionError):
|
||||
win_file.get_pgid("C:\\Path\\That\\Does\\Not\\Exist.txt")
|
||||
|
||||
|
||||
def test_get_pgid():
|
||||
"""
|
||||
We can't know what this value is, so we're just making sure it found
|
||||
something
|
||||
"""
|
||||
result = win_file.get_pgid(os.getenv("COMSPEC"))
|
||||
assert result != ""
|
||||
|
||||
|
||||
def test_group_to_gid():
|
||||
with patch.dict(win_file.__opts__, {}):
|
||||
result = win_file.group_to_gid("Administrators")
|
||||
expected = "S-1-5-32-544"
|
||||
assert result == expected
|
||||
|
||||
|
||||
def test_group_to_gid_empty():
|
||||
with patch.dict(win_file.__opts__, {}):
|
||||
result = win_file.group_to_gid("")
|
||||
expected = "S-1-5-32"
|
||||
assert result == expected
|
||||
|
||||
|
||||
def test_uid_to_user():
|
||||
result = win_file.uid_to_user("S-1-5-32-544")
|
||||
expected = "Administrators"
|
||||
assert result == expected
|
||||
|
||||
|
||||
def test_uid_to_user_empty():
|
||||
result = win_file.uid_to_user("")
|
||||
expected = ""
|
||||
assert result == expected
|
||||
|
||||
|
||||
def test_user_to_uid():
|
||||
result = win_file.user_to_uid("Administrator")
|
||||
expected = salt.utils.win_dacl.get_sid_string("Administrator")
|
||||
assert result == expected
|
||||
|
||||
|
||||
def test_user_to_uid_none():
|
||||
result = win_file.user_to_uid(None)
|
||||
expected = salt.utils.win_dacl.get_sid_string(salt.utils.user.get_user())
|
||||
assert result == expected
|
||||
|
||||
|
||||
def test_get_uid():
|
||||
"""
|
||||
We can't know what this value is, so we're just making sure it found
|
||||
something
|
||||
"""
|
||||
result = win_file.get_uid(os.getenv("WINDIR"))
|
||||
assert result != ""
|
||||
|
||||
|
||||
def test_get_uid_error():
|
||||
with pytest.raises(CommandExecutionError):
|
||||
win_file.get_uid("C:\\fake\\path")
|
||||
|
||||
|
||||
def test_chown(tmp_path):
|
||||
test_file = tmp_path / "test_file.txt"
|
||||
test_file.touch()
|
||||
win_file.chown(path=str(test_file), user="Administrators", pgroup="Guests")
|
||||
assert win_file.get_user(str(test_file)) == "Administrators"
|
||||
assert win_file.get_pgroup(str(test_file)) == "Guests"
|
||||
|
||||
|
||||
def test_chpgrp(tmp_path):
|
||||
test_file = tmp_path / "test_file.txt"
|
||||
test_file.touch()
|
||||
win_file.chown(path=str(test_file), user="Administrators", pgroup="Guests")
|
||||
win_file.chpgrp(path=str(test_file), group="Administrators")
|
||||
assert win_file.get_pgroup(str(test_file)) == "Administrators"
|
||||
|
||||
|
||||
def test_stats_mode(tmp_path):
|
||||
test_file = tmp_path / "test_file.txt"
|
||||
test_file.touch()
|
||||
results = win_file.stats(str(test_file))
|
||||
assert results["mode"] == "0666"
|
||||
|
||||
|
||||
def test_is_link_true(tmp_path):
|
||||
test_source = tmp_path / "test_source.txt"
|
||||
test_link = tmp_path / "test_link.txt"
|
||||
test_source.touch()
|
||||
test_link.symlink_to(test_source)
|
||||
results = win_file.is_link(str(test_link))
|
||||
expected = True
|
||||
assert results == expected
|
||||
|
||||
|
||||
def test_is_link_false(tmp_path):
|
||||
test_file = tmp_path / "test_not_link.txt"
|
||||
test_file.touch()
|
||||
results = win_file.is_link(str(test_file))
|
||||
expected = False
|
||||
assert results == expected
|
||||
|
||||
|
||||
def test_mkdir(tmp_path):
|
||||
test_dir = tmp_path / "test_dir"
|
||||
grant_perms = {"Guests": {"perms": "full_control"}}
|
||||
win_file.mkdir(
|
||||
path=str(test_dir),
|
||||
owner="Administrators",
|
||||
grant_perms=grant_perms,
|
||||
)
|
||||
owner = win_file.get_user(str(test_dir))
|
||||
assert owner == "Administrators"
|
||||
perms = salt.utils.win_dacl.get_permissions(str(test_dir))
|
||||
assert perms["Not Inherited"]["Guests"]["grant"]["permissions"] == "Full control"
|
||||
|
||||
|
||||
def test_check_perms(tmp_path):
|
||||
test_dir = tmp_path / "test_dir"
|
||||
test_dir.mkdir()
|
||||
grant_perms = {"Guests": {"perms": "full_control"}}
|
||||
ret = {}
|
||||
with patch.dict(salt.utils.win_dacl.__opts__, {"test": False}):
|
||||
result = win_file.check_perms(
|
||||
path=str(test_dir),
|
||||
ret=ret,
|
||||
owner="Guests",
|
||||
grant_perms=grant_perms,
|
||||
)
|
||||
|
||||
expected = {
|
||||
"changes": {
|
||||
"grant_perms": {
|
||||
"Guests": {
|
||||
"permissions": "full_control",
|
||||
},
|
||||
},
|
||||
"owner": "Guests",
|
||||
},
|
||||
"comment": "",
|
||||
"name": str(test_dir),
|
||||
"result": True,
|
||||
}
|
||||
|
||||
assert result == expected
|
||||
owner = win_file.get_user(str(test_dir))
|
||||
assert owner == "Guests"
|
||||
perms = salt.utils.win_dacl.get_permissions(str(test_dir))
|
||||
assert perms["Not Inherited"]["Guests"]["grant"]["permissions"] == "Full control"
|
||||
|
||||
|
||||
def test_set_perms(tmp_path):
|
||||
test_dir = tmp_path / "test_dir"
|
||||
test_dir.mkdir()
|
||||
grant_perms = {"Guests": {"perms": "full_control"}}
|
||||
win_file.set_perms(
|
||||
path=str(test_dir),
|
||||
grant_perms=grant_perms,
|
||||
)
|
||||
perms = salt.utils.win_dacl.get_permissions(str(test_dir))
|
||||
assert perms["Not Inherited"]["Guests"]["grant"]["permissions"] == "Full control"
|
||||
|
||||
|
||||
def test_get_user():
|
||||
"""
|
||||
We can't know what this value is, so we're just making sure it found
|
||||
something
|
||||
"""
|
||||
result = win_file.get_user(os.getenv("WINDIR"))
|
||||
assert result != ""
|
||||
|
||||
|
||||
def test_get_user_error():
|
||||
with pytest.raises(CommandExecutionError):
|
||||
win_file.get_user("C:\\fake\\path")
|
||||
|
||||
|
||||
def test_version_missing_file():
|
||||
with pytest.raises(CommandExecutionError):
|
||||
win_file.version("C:\\Windows\\bogus.exe")
|
||||
|
|
|
@ -7,8 +7,11 @@ import copy
|
|||
import datetime
|
||||
import logging
|
||||
|
||||
import zmq
|
||||
|
||||
import salt.exceptions
|
||||
import salt.payload
|
||||
import salt.utils.msgpack
|
||||
from salt.defaults import _Constant
|
||||
from salt.utils import immutabletypes
|
||||
from salt.utils.odict import OrderedDict
|
||||
|
@ -210,3 +213,93 @@ def test_constants():
|
|||
sdata = salt.payload.dumps(constant)
|
||||
odata = salt.payload.loads(sdata)
|
||||
assert odata == constant
|
||||
|
||||
|
||||
def test_package():
|
||||
value = salt.utils.msgpack.dumps("test")
|
||||
assert salt.payload.package("test") == value
|
||||
|
||||
|
||||
def test_unpackage():
|
||||
value = [b"test"]
|
||||
packed = salt.utils.msgpack.dumps(value)
|
||||
assert salt.payload.unpackage(packed) == value
|
||||
|
||||
|
||||
def test_format_payload():
|
||||
expected = salt.utils.msgpack.dumps(
|
||||
{"enc": [b"test"], "load": {"kwargs": {"foo": "bar"}}}
|
||||
)
|
||||
enc = [b"test"]
|
||||
kwargs = {"foo": "bar"}
|
||||
payload = salt.payload.format_payload(enc=enc, kwargs=kwargs)
|
||||
assert payload == expected
|
||||
|
||||
|
||||
def test_SREQ_init():
|
||||
req = salt.payload.SREQ(
|
||||
"tcp://salt:3434", id_=b"id", serial="msgpack", linger=1, opts=None
|
||||
)
|
||||
assert req.master == "tcp://salt:3434"
|
||||
assert req.id_ == b"id"
|
||||
assert req.linger == 1
|
||||
assert req.opts is None
|
||||
assert isinstance(req.context, zmq.Context)
|
||||
assert isinstance(req.poller, zmq.Poller)
|
||||
|
||||
|
||||
def test_SREQ_socket():
|
||||
req = salt.payload.SREQ(
|
||||
"tcp://salt:3434", id_=b"id", serial="msgpack", linger=1, opts=None
|
||||
)
|
||||
# socket() is a property that auto creates a socket if a socket is wanted.
|
||||
socket = req.socket
|
||||
assert isinstance(socket, zmq.Socket)
|
||||
|
||||
req = salt.payload.SREQ(
|
||||
"tcp://[2001:db8:85a3:8d3:1319:8a2e:370:7348]:3434",
|
||||
id_=b"id",
|
||||
serial="msgpack",
|
||||
linger=1,
|
||||
opts=None,
|
||||
)
|
||||
# socket() is a property that auto creates a socket if a socket is wanted.
|
||||
socket = req.socket
|
||||
assert isinstance(socket, zmq.Socket)
|
||||
|
||||
req = salt.payload.SREQ(
|
||||
"tcp://salt:3434", id_=None, serial="msgpack", linger=1, opts=None
|
||||
)
|
||||
# socket() is a property that auto creates a socket if a socket is wanted.
|
||||
socket = req.socket
|
||||
assert isinstance(socket, zmq.Socket)
|
||||
|
||||
|
||||
def test_SREQ_set_tcp_keepalive():
|
||||
opts = {"tcp_keepalive": True}
|
||||
req = salt.payload.SREQ(
|
||||
"tcp://salt:3434", id_=b"id", serial="msgpack", linger=1, opts=opts
|
||||
)
|
||||
socket = req.socket
|
||||
assert req._socket.getsockopt(zmq.TCP_KEEPALIVE)
|
||||
|
||||
opts = {"tcp_keepalive_idle": 100}
|
||||
req = salt.payload.SREQ(
|
||||
"tcp://salt:3434", id_=b"id", serial="msgpack", linger=1, opts=opts
|
||||
)
|
||||
socket = req.socket
|
||||
assert req._socket.getsockopt(zmq.TCP_KEEPALIVE_IDLE) == 100
|
||||
|
||||
opts = {"tcp_keepalive_cnt": 100}
|
||||
req = salt.payload.SREQ(
|
||||
"tcp://salt:3434", id_=b"id", serial="msgpack", linger=1, opts=opts
|
||||
)
|
||||
socket = req.socket
|
||||
assert req._socket.getsockopt(zmq.TCP_KEEPALIVE_CNT) == 100
|
||||
|
||||
opts = {"tcp_keepalive_intvl": 100}
|
||||
req = salt.payload.SREQ(
|
||||
"tcp://salt:3434", id_=b"id", serial="msgpack", linger=1, opts=opts
|
||||
)
|
||||
socket = req.socket
|
||||
assert req._socket.getsockopt(zmq.TCP_KEEPALIVE_INTVL) == 100
|
||||
|
|
|
@ -4,7 +4,6 @@ import salt.modules.file as file_
|
|||
import salt.modules.heat as heat
|
||||
import salt.modules.win_file as win_file
|
||||
import salt.utils.platform
|
||||
import salt.utils.win_dacl as dacl
|
||||
from tests.support.mixins import LoaderModuleMockMixin
|
||||
from tests.support.mock import MagicMock, patch
|
||||
from tests.support.runtests import RUNTIME_VARS
|
||||
|
@ -78,10 +77,7 @@ class HeatTestCase(TestCase, LoaderModuleMockMixin):
|
|||
"config.backup_mode": MagicMock(return_value=False),
|
||||
},
|
||||
},
|
||||
win_file: {
|
||||
"__utils__": {"dacl.check_perms": salt.utils.win_dacl.check_perms}
|
||||
},
|
||||
dacl: {"__opts__": {"test": False}},
|
||||
win_file: {"__opts__": {"test": False}},
|
||||
}
|
||||
|
||||
def setUp(self):
|
||||
|
|
|
@ -5,7 +5,6 @@ import salt.modules.heat
|
|||
import salt.modules.win_file as win_file
|
||||
import salt.states.heat as heat
|
||||
import salt.utils.platform
|
||||
import salt.utils.win_dacl as dacl
|
||||
import tests.unit.modules.test_heat
|
||||
from tests.support.mixins import LoaderModuleMockMixin
|
||||
from tests.support.mock import MagicMock, patch
|
||||
|
@ -38,10 +37,7 @@ class HeatTestCase(TestCase, LoaderModuleMockMixin):
|
|||
"config.backup_mode": MagicMock(return_value=False),
|
||||
},
|
||||
},
|
||||
win_file: {
|
||||
"__utils__": {"dacl.check_perms": salt.utils.win_dacl.check_perms}
|
||||
},
|
||||
dacl: {"__opts__": {"test": False}},
|
||||
win_file: {"__opts__": {"test": False}},
|
||||
}
|
||||
|
||||
def setUp(self):
|
||||
|
|
Loading…
Add table
Reference in a new issue