Skip to content

Commit

Permalink
Ensure the correct argument is passed to hostname command
Browse files Browse the repository at this point in the history
  • Loading branch information
Megan Wilhite committed Oct 19, 2023
1 parent 13bd411 commit 69c714b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 705 deletions.
1 change: 1 addition & 0 deletions changelog/63438.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Ensure the correct argument is passed to hostname command depending on the systemd version for both the network and system module.
29 changes: 28 additions & 1 deletion tests/pytests/unit/modules/test_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import salt.utils.path
from salt._compat import ipaddress
from salt.exceptions import CommandExecutionError
from tests.support.mock import MagicMock, mock_open, patch
from tests.support.mock import MagicMock, call, mock_open, patch

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -378,6 +378,33 @@ def test_mod_hostname():
assert networkmod.mod_hostname("hostname")


def test_mod_hostname_hostnamectl():
"""
Test for Modify hostname when hostnamectl is used
"""
assert not networkmod.mod_hostname(None)
file_d = "\n".join(["#", "A B C D,E,F G H"])

mock_run_all = MagicMock(
return_value={"retcode": 0, "stdout": "Static hostname: testhostname"}
)
patch_run_all = patch.dict(networkmod.__salt__, {"cmd.run_all": mock_run_all})
cmd = "hostname" if salt.utils.systemd.version() >= 249 else "set-hostname"
with patch.dict(
networkmod.__utils__,
{
"path.which": MagicMock(return_value="hostnamectl"),
"files.fopen": mock_open(read_data=file_d),
},
), patch.dict(
networkmod.__salt__, {"cmd.run": MagicMock(return_value=None)}
), patch.dict(
networkmod.__grains__, {"os_family": "A"}
), patch_run_all:
assert networkmod.mod_hostname("hostname")
assert mock_run_all.call_args_list[1] == call(f"hostnamectl {cmd} hostname")


def test_mod_hostname_quoted():
"""
Test for correctly quoted hostname on rh-style distro
Expand Down
Loading

0 comments on commit 69c714b

Please sign in to comment.