Skip to content

Commit

Permalink
Allow kwargs for fileserver roots update (bsc#1218482) (#618)
Browse files Browse the repository at this point in the history
* Allow kwargs for fileserver roots update (bsc#1218482)

* Prevent exceptions with fileserver.update when called via state

* Fix wrong logic and enhance tests around fileserver.update

* Remove test which is not longer valid

---------

Co-authored-by: Pablo Suárez Hernández <[email protected]>
  • Loading branch information
ycedres and meaksh authored Jan 23, 2024
1 parent da938aa commit 8ae54e8
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 10 deletions.
1 change: 1 addition & 0 deletions changelog/65819.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Prevent exceptions with fileserver.update when called via state
8 changes: 3 additions & 5 deletions salt/fileserver/roots.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,7 @@ def update():
os.makedirs(mtime_map_path_dir)
with salt.utils.files.fopen(mtime_map_path, "wb") as fp_:
for file_path, mtime in new_mtime_map.items():
fp_.write(
salt.utils.stringutils.to_bytes("{}:{}\n".format(file_path, mtime))
)
fp_.write(salt.utils.stringutils.to_bytes(f"{file_path}:{mtime}\n"))

if __opts__.get("fileserver_events", False):
# if there is a change, fire an event
Expand Down Expand Up @@ -326,11 +324,11 @@ def _file_lists(load, form):
return []
list_cache = os.path.join(
list_cachedir,
"{}.p".format(salt.utils.files.safe_filename_leaf(actual_saltenv)),
f"{salt.utils.files.safe_filename_leaf(actual_saltenv)}.p",
)
w_lock = os.path.join(
list_cachedir,
".{}.w".format(salt.utils.files.safe_filename_leaf(actual_saltenv)),
f".{salt.utils.files.safe_filename_leaf(actual_saltenv)}.w",
)
cache_match, refresh_cache, save_cache = salt.fileserver.check_file_list_cache(
__opts__, form, list_cache, w_lock
Expand Down
6 changes: 6 additions & 0 deletions salt/runners/fileserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,12 @@ def update(backend=None, **kwargs):
salt-run fileserver.update backend=git remotes=myrepo,yourrepo
"""
fileserver = salt.fileserver.Fileserver(__opts__)

# Remove possible '__pub_user' in kwargs as it is not expected
# on "update" function for the different fileserver backends.
if "__pub_user" in kwargs:
del kwargs["__pub_user"]

fileserver.update(back=backend, **kwargs)
return True

Expand Down
40 changes: 36 additions & 4 deletions tests/integration/runners/test_fileserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,15 +202,31 @@ def test_update(self):
fileserver.update
"""
ret = self.run_run_plus(fun="fileserver.update")
self.assertTrue(ret["return"])
self.assertTrue(ret["return"] is True)

# Backend submitted as a string
ret = self.run_run_plus(fun="fileserver.update", backend="roots")
self.assertTrue(ret["return"])
self.assertTrue(ret["return"] is True)

# Backend submitted as a list
ret = self.run_run_plus(fun="fileserver.update", backend=["roots"])
self.assertTrue(ret["return"])
self.assertTrue(ret["return"] is True)

# Possible '__pub_user' is removed from kwargs
ret = self.run_run_plus(
fun="fileserver.update", backend=["roots"], __pub_user="foo"
)
self.assertTrue(ret["return"] is True)

# Unknown arguments
ret = self.run_run_plus(
fun="fileserver.update", backend=["roots"], unknown_arg="foo"
)
self.assertIn(
"Passed invalid arguments: update() got an unexpected keyword argument"
" 'unknown_arg'",
ret["return"],
)

# Other arguments are passed to backend
def mock_gitfs_update(remotes=None):
Expand All @@ -225,7 +241,23 @@ def mock_gitfs_update(remotes=None):
ret = self.run_run_plus(
fun="fileserver.update", backend="gitfs", remotes="myrepo,yourrepo"
)
self.assertTrue(ret["return"])
self.assertTrue(ret["return"] is True)
mock_backend_func.assert_called_once_with(remotes="myrepo,yourrepo")

# Possible '__pub_user' arguments are removed from kwargs
mock_backend_func = create_autospec(mock_gitfs_update)
mock_return_value = {
"gitfs.envs": None, # This is needed to activate the backend
"gitfs.update": mock_backend_func,
}
with patch("salt.loader.fileserver", MagicMock(return_value=mock_return_value)):
ret = self.run_run_plus(
fun="fileserver.update",
backend="gitfs",
remotes="myrepo,yourrepo",
__pub_user="foo",
)
self.assertTrue(ret["return"] is True)
mock_backend_func.assert_called_once_with(remotes="myrepo,yourrepo")

# Unknown arguments are passed to backend
Expand Down
2 changes: 1 addition & 1 deletion tests/pytests/unit/fileserver/test_roots.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ def test_update_mtime_map():
# between Python releases.
lines_written = sorted(mtime_map_mock.write_calls())
expected = sorted(
salt.utils.stringutils.to_bytes("{key}:{val}\n".format(key=key, val=val))
salt.utils.stringutils.to_bytes(f"{key}:{val}\n")
for key, val in new_mtime_map.items()
)
assert lines_written == expected, lines_written
Expand Down

0 comments on commit 8ae54e8

Please sign in to comment.