Skip to content

Commit

Permalink
check error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
cmcmarrow committed Oct 18, 2023
1 parent 2edd9c7 commit 15d6965
Showing 1 changed file with 122 additions and 2 deletions.
124 changes: 122 additions & 2 deletions tests/pytests/functional/modules/test_win_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,42 @@ def test_symlink(tmp_path):
assert os.path.isfile(file) is True
link = os.path.join(tmp_path, "l")
assert win_file.is_link(link) is False
win_file.symlink(file, link, force=True)
assert win_file.symlink(file, link) is True
assert os.path.isfile(file) is True
assert win_file.is_link(link) is True


@skip_not_windows_admin
def test_symlink_path_taken(tmp_path):
tmp_path = str(tmp_path)
file = os.path.join(tmp_path, "t.txt")
with salt.utils.files.fopen(file, "w"):
pass
assert os.path.isfile(file) is True
link = os.path.join(tmp_path, "l")
with salt.utils.files.fopen(link, "w"):
pass
# symlink should raise error if path name is all ready taken
with pytest.raises(CommandExecutionError):
win_file.symlink(file, link)


@skip_not_windows_admin
def test_symlink_force(tmp_path):
tmp_path = str(tmp_path)
file = os.path.join(tmp_path, "t.txt")
with salt.utils.files.fopen(file, "w"):
pass
assert os.path.isfile(file) is True
link = os.path.join(tmp_path, "l")
assert win_file.is_link(link) is False
assert win_file.symlink(file, link, force=True) is True
assert os.path.isfile(file) is True
assert win_file.is_link(link) is True
# check that symlink returns ture if link is all ready-made
assert win_file.symlink(file, link, force=True) is True


@skip_not_windows_admin
def test_symlink_atomic(tmp_path):
tmp_path = str(tmp_path)
Expand All @@ -60,9 +91,11 @@ def test_symlink_atomic(tmp_path):
assert os.path.isfile(file) is True
link = os.path.join(tmp_path, "l")
assert win_file.is_link(link) is False
win_file.symlink(file, link, force=True, atomic=True)
assert win_file.symlink(file, link, force=True, atomic=True) is True
assert os.path.isfile(file) is True
assert win_file.is_link(link) is True
# check that symlink returns ture if link is all ready-made
assert win_file.symlink(file, link, force=True, atomic=True) is True


def test_is_not_link(tmp_path):
Expand Down Expand Up @@ -94,6 +127,10 @@ def test_uid_user(tmp_path):
assert win_file.user_to_uid(win_file.uid_to_user(uid)) == uid


def test_uid_to_user_none(tmp_path):
assert win_file.uid_to_user(None) == ""


def test_gid_group(tmp_path, windows_user):
path = str(tmp_path)
gid = win_file.get_gid(path)
Expand All @@ -104,6 +141,10 @@ def test_gid_group(tmp_path, windows_user):
assert win_file.group_to_gid(group) == gid


def test_gid_group_none(tmp_path):
assert win_file.group_to_gid(None) == ""


def test_get_pgroup(tmp_path):
path = str(tmp_path)
pgroup = win_file.get_pgroup(path)
Expand All @@ -116,11 +157,32 @@ def test_get_pgid(tmp_path):
assert "-" in pgid


def test_get_uid_path_not_found(tmp_path):
tmp_path = str(tmp_path)
path = os.path.join(tmp_path, "dir")
with pytest.raises(CommandExecutionError):
win_file.get_uid(path)


def test_get_user_path_not_found(tmp_path):
tmp_path = str(tmp_path)
path = os.path.join(tmp_path, "dir")
with pytest.raises(CommandExecutionError):
win_file.get_user(path)


def test_mode(tmp_path):
tmp_path = str(tmp_path)
assert win_file.get_mode(tmp_path) is None


def test_mode_path_not_found(tmp_path):
tmp_path = str(tmp_path)
path = os.path.join(tmp_path, "dir1")
with pytest.raises(CommandExecutionError):
win_file.get_mode(path)


def test_lchown(tmp_path, windows_user):
path = str(tmp_path)
assert win_file.lchown(path, windows_user) is True
Expand Down Expand Up @@ -163,6 +225,13 @@ def test_stats(tmp_path, windows_user):
assert isinstance(stats["target"], str)


def test_stats_path_not_found(tmp_path, windows_user):
tmp_path = str(tmp_path)
path = os.path.join(tmp_path, "dir1")
with pytest.raises(CommandExecutionError):
win_file.stats(path)


def test_version():
assert len(win_file.version("C:\\Windows\\System32\\wow64.dll").split(".")) == 4

Expand All @@ -176,6 +245,21 @@ def test_version_empty(tmp_path):
assert win_file.version(file) == ""


def test_version_path_not_found(tmp_path):
tmp_path = str(tmp_path)
file = os.path.join(tmp_path, "t.txt")
with pytest.raises(CommandExecutionError):
win_file.version(file)


def test_version_dir(tmp_path):
tmp_path = str(tmp_path)
path = os.path.join(tmp_path, "dir")
os.mkdir(path)
with pytest.raises(CommandExecutionError):
win_file.version(path)


def test_version_details():
details = win_file.version_details("C:\\Windows\\System32\\wow64.dll")
assert isinstance(details, dict) is True
Expand Down Expand Up @@ -290,6 +374,20 @@ def test_mkdir(tmp_path):
assert os.path.isdir(path)


def test_mkdir_error(tmp_path):
tmp_path = str(tmp_path)
# dirs cant be named CON in windows
path = os.path.join(tmp_path, "CON")
with pytest.raises(CommandExecutionError):
win_file.mkdir(path)
assert os.path.isdir(path) is False
# cant make dir if parent is not made
path = os.path.join(tmp_path, "a", "b", "c", "salt")
with pytest.raises(CommandExecutionError):
win_file.mkdir(path)
assert os.path.isdir(path) is False


def test_makedirs_(tmp_path):
tmp_path = str(tmp_path)
parent = os.path.join(tmp_path, "dir1\\dir2")
Expand All @@ -299,11 +397,25 @@ def test_makedirs_(tmp_path):
assert os.path.isdir(path) is False


def test_makedirs__path_exists(tmp_path):
tmp_path = str(tmp_path)
parent = os.path.join(tmp_path, "dir1\\dir2")
path = os.path.join(parent, "dir3")
assert win_file.makedirs_(path) is True
assert os.path.isdir(parent) is True
# makdrirs_ should return message that path has all ready been made
assert isinstance(win_file.makedirs_(path), str) is True
assert os.path.isdir(parent) is True


def test_makedirs_perms(tmp_path):
tmp_path = str(tmp_path)
path = os.path.join(tmp_path, "dir1\\dir2")
assert win_file.makedirs_perms(path) is True
assert os.path.isdir(path)
# make sure makedirs does not fail if path is all ready-made
assert win_file.makedirs_perms(path) is True
assert os.path.isdir(path)


def test_check_perms(tmp_path, windows_user):
Expand All @@ -317,6 +429,14 @@ def test_check_perms(tmp_path, windows_user):
assert perms["result"] is True


def test_check_perms_path_not_found(tmp_path, windows_user):
tmp_path = str(tmp_path)
path = os.path.join(tmp_path, "dir1")
# check_perms will fail due to path not being made
with pytest.raises(CommandExecutionError):
win_file.check_perms(path, {}, windows_user)


def test_set_perms(tmp_path):
path = str(tmp_path)
assert win_file.set_perms(path) == {}

0 comments on commit 15d6965

Please sign in to comment.