From 66967c1c8114dbde6cb534ea25d5790869fa25dc Mon Sep 17 00:00:00 2001 From: John Mulligan Date: Thu, 26 Jan 2023 15:03:30 -0500 Subject: [PATCH] cephadm/tests: add test coverage for find_executable Also sets no coverage pragma on a windows only line. I'm assuming cephadm isn't going to be run on windows any time soon. Signed-off-by: John Mulligan --- src/cephadm/cephadm.py | 2 +- src/cephadm/tests/test_util_funcs.py | 40 ++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/cephadm/cephadm.py b/src/cephadm/cephadm.py index 07dcecd0652..ad3aa054d75 100755 --- a/src/cephadm/cephadm.py +++ b/src/cephadm/cephadm.py @@ -2460,7 +2460,7 @@ def find_executable(executable: str, path: Optional[str] = None) -> Optional[str """ _, ext = os.path.splitext(executable) if (sys.platform == 'win32') and (ext != '.exe'): - executable = executable + '.exe' + executable = executable + '.exe' # pragma: no cover if os.path.isfile(executable): return executable diff --git a/src/cephadm/tests/test_util_funcs.py b/src/cephadm/tests/test_util_funcs.py index a6c2396c17f..7a922bcdd48 100644 --- a/src/cephadm/tests/test_util_funcs.py +++ b/src/cephadm/tests/test_util_funcs.py @@ -288,3 +288,43 @@ def test_recursive_chown(tmp_path): assert _chown.mock_calls[0].args == (str(d1), 500, 500) assert _chown.mock_calls[1].args == (str(d2), 500, 500) assert _chown.mock_calls[2].args == (str(f1), 500, 500) + + +class TestFindExecutable: + def test_standard_exe(self): + # pretty much every system will have `true` on the path. It's a safe choice + # for the first assertion + exe = _cephadm.find_executable("true") + assert exe.endswith("true") + + def test_custom_path(self, tmp_path): + foo_sh = tmp_path / "foo.sh" + with open(foo_sh, "w") as fh: + fh.write("#!/bin/sh\n") + fh.write("echo foo\n") + foo_sh.chmod(0o755) + + exe = _cephadm.find_executable(foo_sh) + assert str(exe) == str(foo_sh) + + def test_no_path(self, monkeypatch): + monkeypatch.delenv("PATH") + exe = _cephadm.find_executable("true") + assert exe.endswith("true") + + def test_no_path_no_confstr(self, monkeypatch): + def _fail(_): + raise ValueError("fail") + + monkeypatch.delenv("PATH") + monkeypatch.setattr("os.confstr", _fail) + exe = _cephadm.find_executable("true") + assert exe.endswith("true") + + def test_unset_path(self): + exe = _cephadm.find_executable("true", path="") + assert exe is None + + def test_no_such_exe(self): + exe = _cephadm.find_executable("foo_bar-baz.noway") + assert exe is None -- 2.39.5