Skip to content

Commit

Permalink
fixes saltstack#64477 file.symlink will not replace/update existing s…
Browse files Browse the repository at this point in the history
…ymlink
  • Loading branch information
nicholasmhughes authored and s0undt3ch committed Jun 14, 2023
1 parent 4ab503d commit 30043e9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/64477.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix file.symlink will not replace/update existing symlink
4 changes: 3 additions & 1 deletion salt/states/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -1799,9 +1799,11 @@ def symlink(

if __salt__["file.is_link"](name):
# The link exists, verify that it matches the target
if os.path.normpath(__salt__["file.readlink"](name)) == os.path.normpath(
if os.path.normpath(__salt__["file.readlink"](name)) != os.path.normpath(
target
):
__salt__["file.remove"](name)
else:
if _check_symlink_ownership(name, user, group, win_owner):
# The link looks good!
if salt.utils.platform.is_windows():
Expand Down
22 changes: 22 additions & 0 deletions tests/pytests/functional/states/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,25 @@ def test_file_managed_web_source_etag_operation(

# The modified time of the cached file now changes
assert cached_file_mtime != os.path.getmtime(cached_file)


def test_file_symlink_replace_existing_link(states, tmp_path):
# symlink name and target for state
name = tmp_path / "foo"
target = tmp_path / "baz"

# create existing symlink to replace
old_target = tmp_path / "bar"
name.symlink_to(old_target)

ret = states.file.symlink(
name=str(name),
target=str(target),
)

assert ret.filtered == {
"name": str(name),
"changes": {"new": str(name)},
"comment": f"Created new symlink {str(name)} -> {str(target)}",
"result": True,
}

0 comments on commit 30043e9

Please sign in to comment.