mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Don't mkdir -p
on empty paths
This commit is contained in:
parent
5e16d84834
commit
fa469ebc24
3 changed files with 34 additions and 14 deletions
|
@ -1517,7 +1517,7 @@ ARGS = {arguments}\n'''.format(
|
|||
if self.winrm:
|
||||
target_shim_file = saltwinshell.get_target_shim_file(self, target_shim_file)
|
||||
stdout, stderr, retcode = self.shell.send(
|
||||
shim_tmp_file.name, target_shim_file, makedirs=True
|
||||
shim_tmp_file.name, target_shim_file, makedirs=self.winrm
|
||||
)
|
||||
if retcode != 0:
|
||||
log.error("Could not copy the shim script to target")
|
||||
|
|
|
@ -87,7 +87,7 @@ class Shell:
|
|||
ssh_options=None,
|
||||
):
|
||||
self.opts = opts
|
||||
# ssh <ipv6>, but scp [<ipv6]:/path
|
||||
# ssh <ipv6>, but scp [<ipv6>]:/path
|
||||
self.host = host.strip("[]")
|
||||
self.user = user
|
||||
self.port = port
|
||||
|
@ -339,11 +339,17 @@ class Shell:
|
|||
scp a file or files to a remote system
|
||||
"""
|
||||
if makedirs:
|
||||
ret = self.exec_cmd(f"mkdir -p {os.path.dirname(remote)}")
|
||||
if ret[2]:
|
||||
return ret
|
||||
pardir = os.path.dirname(remote)
|
||||
if not pardir:
|
||||
log.warning(
|
||||
f"Makedirs called on relative filename: '{remote}'. Skipping."
|
||||
)
|
||||
else:
|
||||
ret = self.exec_cmd("mkdir -p " + shlex.quote(pardir))
|
||||
if ret[2]:
|
||||
return ret
|
||||
|
||||
# scp needs [<ipv6}
|
||||
# scp needs [<ipv6>]
|
||||
host = self.host
|
||||
if ":" in host:
|
||||
host = f"[{host}]"
|
||||
|
|
|
@ -1,10 +1,11 @@
|
|||
import logging
|
||||
import subprocess
|
||||
import types
|
||||
|
||||
import pytest
|
||||
|
||||
import salt.client.ssh.shell as shell
|
||||
from tests.support.mock import MagicMock, PropertyMock, patch
|
||||
from tests.support.mock import MagicMock, PropertyMock, call, patch
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
|
@ -100,14 +101,27 @@ def test_ssh_shell_exec_cmd_returns_status_code_with_highest_bit_set_if_process_
|
|||
assert retcode == 137
|
||||
|
||||
|
||||
def test_ssh_shell_send_makedirs_failure_returns_immediately():
|
||||
def exec_cmd(cmd):
|
||||
if cmd.startswith("mkdir -p"):
|
||||
return "", "Not a directory", 1
|
||||
return "", "", 0
|
||||
def exec_cmd(cmd):
|
||||
if cmd.startswith("mkdir -p"):
|
||||
return "", "Not a directory", 1
|
||||
return "OK", "", 0
|
||||
|
||||
|
||||
def test_ssh_shell_send_makedirs_failure_returns_immediately():
|
||||
with patch("salt.client.ssh.shell.Shell.exec_cmd", side_effect=exec_cmd):
|
||||
shl = shell.Shell({}, "localhost")
|
||||
stdout, stderr, retcode = shl.send("/tmp/file", "/tmp/file", True)
|
||||
assert retcode == 1
|
||||
assert "Not a directory" in stderr
|
||||
assert retcode == 1
|
||||
assert "Not a directory" in stderr
|
||||
|
||||
|
||||
def test_ssh_shell_send_makedirs_on_relative_filename_skips_exec(caplog):
|
||||
with patch("salt.client.ssh.shell.Shell.exec_cmd", side_effect=exec_cmd) as cmd:
|
||||
with patch("salt.client.ssh.shell.Shell._run_cmd", return_value=("", "", 0)):
|
||||
shl = shell.Shell({}, "localhost")
|
||||
with caplog.at_level(logging.WARNING):
|
||||
stdout, stderr, retcode = shl.send("/tmp/file", "targetfile", True)
|
||||
assert retcode == 0
|
||||
assert "Not a directory" not in stderr
|
||||
assert call("mkdir -p ''") not in cmd.mock_calls
|
||||
assert "Makedirs called on relative filename" in caplog.text
|
||||
|
|
Loading…
Add table
Reference in a new issue