]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
cephadm/tests: add test coverage for find_executable
authorJohn Mulligan <jmulligan@redhat.com>
Thu, 26 Jan 2023 20:03:30 +0000 (15:03 -0500)
committerJohn Mulligan <jmulligan@redhat.com>
Thu, 26 Jan 2023 20:03:30 +0000 (15:03 -0500)
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 <jmulligan@redhat.com>
src/cephadm/cephadm.py
src/cephadm/tests/test_util_funcs.py

index 07dcecd06524513e1183bb6e3b097ae56f8377f1..ad3aa054d75b3deb4170ba6031513a18f94dfff7 100755 (executable)
@@ -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
index a6c2396c17fa15b3be6ac55aa3adaa9220be8305..7a922bcdd48c0588e14f5e4b4a384b7c88205974 100644 (file)
@@ -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