Skip to content

Commit

Permalink
Some more small tests fixes/enhancements (#661)
Browse files Browse the repository at this point in the history
* test_system: prevent errors when systemd-timesyncd service is masked

* test_custom_module: disable tests when running on Salt Bundle

* Fix debian 12 package tests

* pam: use sys.executable in case /usr/bin/python3 does not exist

* pam: add unit test to ensure sys.executable is used

* Use proper method to calculate the path to Python interpreter

---------

Co-authored-by: Megan Wilhite <[email protected]>
  • Loading branch information
meaksh and Megan Wilhite authored Jul 8, 2024
1 parent f6ad8fc commit e4333e2
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 2 deletions.
19 changes: 18 additions & 1 deletion salt/auth/pam.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,29 @@ def authenticate(username, password):
``password``: the password in plain text
"""

def __find_pyexe():
"""
Provides the path to the Python interpreter to use.
First option: the system's Python 3 interpreter
If not found, it fallback to use the running Python interpreter (sys.executable)
This can be overwritten via "auth.pam.python" configuration parameter.
"""
if __opts__.get("auth.pam.python"):
return __opts__.get("auth.pam.python")
elif os.path.exists("/usr/bin/python3"):
return "/usr/bin/python3"
else:
return sys.executable

env = os.environ.copy()
env["SALT_PAM_USERNAME"] = username
env["SALT_PAM_PASSWORD"] = password
env["SALT_PAM_SERVICE"] = __opts__.get("auth.pam.service", "login")
env["SALT_PAM_ENCODING"] = __salt_system_encoding__
pyexe = pathlib.Path(__opts__.get("auth.pam.python", "/usr/bin/python3")).resolve()
pyexe = pathlib.Path(__find_pyexe()).resolve()
pyfile = pathlib.Path(__file__).resolve()
if not pyexe.exists():
log.error("Error 'auth.pam.python' config value does not exist: %s", pyexe)
Expand Down
6 changes: 6 additions & 0 deletions tests/integration/cli/test_custom_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,18 @@ def recho(text):
olleh
"""

import sys

import pytest

from tests.support.case import SSHCase


@pytest.mark.skip_on_windows
@pytest.mark.skipif(
"venv-salt-minion" in sys.executable,
reason="Skipping for Salt Bundle (tests are not compatible)",
)
class SSHCustomModuleTest(SSHCase):
"""
Test sls with custom module functionality using ssh
Expand Down
4 changes: 3 additions & 1 deletion tests/pytests/functional/modules/test_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ def setup_teardown_vars(file, service, system):
_machine_info = False

try:
_systemd_timesyncd_available_ = service.available("systemd-timesyncd")
_systemd_timesyncd_available_ = service.available(
"systemd-timesyncd"
) and not service.masked("systemd-timesyncd")
if _systemd_timesyncd_available_:
res = service.stop("systemd-timesyncd")
assert res
Expand Down
4 changes: 4 additions & 0 deletions tests/pytests/functional/states/pkgrepo/test_debian.py
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,10 @@ def _default_alt_repo(self):
if (
self.grains["osfullname"] == "Ubuntu"
and self.grains["osrelease"] == "22.04"
or "Debian" in self.grains["osfullname"]
and self.grains["osrelease"] == "12"
# only need to use alt repo until
# we release Debian 12 salt packages
):
return True
return False
Expand Down
19 changes: 19 additions & 0 deletions tests/pytests/unit/auth/test_pam.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import tempfile

import pytest

import salt.auth.pam
Expand Down Expand Up @@ -45,3 +47,20 @@ def test_if_pam_acct_mgmt_returns_zero_authenticate_should_be_true(mock_pam):
)
is True
)


def test_if_sys_executable_is_used_to_call_pam_auth(mock_pam):
class Ret:
returncode = 0

with patch(
"salt.auth.pam.subprocess.run", return_value=Ret
) as run_mock, tempfile.NamedTemporaryFile() as f, patch(
"salt.auth.pam.sys.executable", f.name
), patch(
"os.path.exists", return_value=False
):
assert salt.auth.pam.auth(
username="fnord", password="fnord", service="login", encoding="utf-8"
)
assert f.name in run_mock.call_args_list[0][0][0]

0 comments on commit e4333e2

Please sign in to comment.