Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e2bdac0793 | ||
|
|
982862725e |
No files matched your search
@@ -1,5 +1,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from .shell import BackupError, execute_shell_command
|
||||
@@ -8,6 +9,8 @@ if TYPE_CHECKING:
|
||||
from collections.abc import Sequence
|
||||
|
||||
EXEC_NOT_FOUND = 127
|
||||
EXEC_START_FAILED = 126
|
||||
MISSING_EXECUTABLE = re.compile(rb"executable file\b.*\bnot found")
|
||||
|
||||
|
||||
def docker_exec_argv(
|
||||
@@ -63,8 +66,12 @@ def has_tool(container: str, tool: str) -> bool:
|
||||
tool it ships.
|
||||
|
||||
Returns:
|
||||
False only on exit code 127, which dockerd sets when the runtime
|
||||
reports the executable missing from the image.
|
||||
False only when the executable is missing from the image: exit code
|
||||
127, or 126 with the runtime's "executable file ... not found" on
|
||||
stderr. dockerd overwrites every failed exec start with 126
|
||||
(moby/moby#45795), so on daemons whose CLI does not map the missing
|
||||
executable to 127 the exit code alone cannot tell it from a
|
||||
transient failure.
|
||||
|
||||
Raises:
|
||||
BackupError: the probe failed for any reason but a missing executable,
|
||||
@@ -77,6 +84,10 @@ def has_tool(container: str, tool: str) -> bool:
|
||||
except BackupError as error:
|
||||
if error.returncode == EXEC_NOT_FOUND:
|
||||
return False
|
||||
if error.returncode == EXEC_START_FAILED and MISSING_EXECUTABLE.search(
|
||||
error.stderr
|
||||
):
|
||||
return False
|
||||
raise
|
||||
return True
|
||||
|
||||
|
||||
@@ -31,11 +31,16 @@ class BackupError(Exception):
|
||||
message: what failed.
|
||||
returncode: the exit code of the command that failed, None when the
|
||||
error did not come from a command's exit.
|
||||
stderr: what the failed command wrote to stderr, empty when it wrote
|
||||
nothing or the error did not come from a command.
|
||||
"""
|
||||
|
||||
def __init__(self, message: str, returncode: int | None = None) -> None:
|
||||
def __init__(
|
||||
self, message: str, returncode: int | None = None, stderr: bytes = b""
|
||||
) -> None:
|
||||
super().__init__(message)
|
||||
self.returncode = returncode
|
||||
self.stderr = stderr
|
||||
|
||||
|
||||
def _child_env(env: Mapping[str, str] | None) -> dict[str, str] | None:
|
||||
@@ -48,6 +53,7 @@ def _fail(command: Sequence[str], returncode: int, out: bytes, err: bytes) -> No
|
||||
f"Output: {out}\nError: {err}\n"
|
||||
f"Exit code: {returncode}",
|
||||
returncode,
|
||||
err,
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -7,6 +7,14 @@ from unittest.mock import patch
|
||||
from baudolo.backup import docker as docker_mod
|
||||
from baudolo.backup.shell import BackupError
|
||||
|
||||
RUNC_NOT_FOUND = (
|
||||
b"OCI runtime exec failed: exec failed: unable to start container process: "
|
||||
b'exec: "pg_dumpall": executable file not found in $PATH'
|
||||
)
|
||||
CRUN_NOT_FOUND = (
|
||||
b"crun: executable file `pg_dumpall` not found in $PATH: No such file or directory"
|
||||
)
|
||||
|
||||
|
||||
class TestImageId(unittest.TestCase):
|
||||
def test_the_id_is_returned_without_surrounding_whitespace(self) -> None:
|
||||
@@ -28,12 +36,21 @@ class TestHasTool(unittest.TestCase):
|
||||
with patch.object(docker_mod, "execute_shell_command", side_effect=not_found):
|
||||
self.assertFalse(docker_mod.has_tool("c1", "mysqldump"))
|
||||
|
||||
def test_a_missing_tool_reported_as_126_is_absent(self) -> None:
|
||||
for runtime_message in (RUNC_NOT_FOUND, CRUN_NOT_FOUND):
|
||||
with self.subTest(runtime_message=runtime_message):
|
||||
not_found = BackupError("exec failed", 126, runtime_message)
|
||||
with patch.object(
|
||||
docker_mod, "execute_shell_command", side_effect=not_found
|
||||
):
|
||||
self.assertFalse(docker_mod.has_tool("c1", "pg_dumpall"))
|
||||
|
||||
def test_a_failed_exec_is_not_read_as_a_missing_tool(self) -> None:
|
||||
"""containerd timed out on the io pipes of a mariadb that ships the tool."""
|
||||
transient = BackupError(
|
||||
"DeadlineExceeded: failed to start io pipe copy: unable to copy pipes",
|
||||
126,
|
||||
pipe_timeout = (
|
||||
b"DeadlineExceeded: failed to start io pipe copy: unable to copy pipes"
|
||||
)
|
||||
transient = BackupError(pipe_timeout.decode(), 126, pipe_timeout)
|
||||
with (
|
||||
patch.object(docker_mod, "execute_shell_command", side_effect=transient),
|
||||
self.assertRaises(BackupError),
|
||||
@@ -57,8 +74,12 @@ class TestHasTool(unittest.TestCase):
|
||||
|
||||
|
||||
class TestProbeExitCode(unittest.TestCase):
|
||||
def _probe_exiting(self, code: int) -> bool:
|
||||
argv = [sys.executable, "-c", f"import sys; sys.exit({code})"]
|
||||
def _probe_exiting(self, code: int, stderr: str = "") -> bool:
|
||||
argv = [
|
||||
sys.executable,
|
||||
"-c",
|
||||
f"import sys; sys.stderr.write({stderr!r}); sys.exit({code})",
|
||||
]
|
||||
with (
|
||||
patch.object(docker_mod, "docker_exec_argv", return_value=argv),
|
||||
contextlib.redirect_stdout(io.StringIO()),
|
||||
@@ -73,6 +94,15 @@ class TestProbeExitCode(unittest.TestCase):
|
||||
self._probe_exiting(126)
|
||||
self.assertEqual(caught.exception.returncode, 126)
|
||||
|
||||
def test_exit_126_with_the_runtime_not_found_message_reads_as_absent(
|
||||
self,
|
||||
) -> None:
|
||||
self.assertFalse(
|
||||
self._probe_exiting(
|
||||
126, 'exec: "mariadb-dump": executable file not found in $PATH'
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user