fix(backup): treat only a missing executable as an absent dump tool

has_tool() read every failed `<tool> --version` probe as "tool absent".
In Infinito.Nexus CI (https://github.com/infinito-nexus/core/actions/runs/46742115623,
job 143242383123) the second backup's `docker exec mariadb mariadb-dump
--version` hung and dockerd failed it with "DeadlineExceeded: failed to
start io pipe copy: unable to copy pipes: containerd". baudolo took that
for a missing mariadb-dump, cached the image as no database and copied
mariadb_data as plain files instead of dumping it; the disaster-recovery
drill then found no marker in the generation.

BackupError now carries the command's exit code, and has_tool() returns
False only on 127, the code docker exec sets when the runtime reports the
executable missing; any other failure re-raises, so the run fails loudly
before container_engine() caches anything. The probe test that encoded
the old behaviour is replaced by a not-found case (127 -> False), a
transient case (126 -> raises) and the same two from a real child
process; engine detection gains a test that a failed probe caches
nothing. Full unit suite: 275 tests OK. Against the old has_tool the
two raise cases fail.

🎯 Fix: 92 %
💥 Break: 5 %
💸 Tokens: 100 %
⏱️ Velocity: 100 %
🛡️ Security: 100 %
🏆 Score: 92
🚦 Gate: unit suite 275 OK + negative control; ruff clean; Docker integration suite not run

Created-with: https://s.veen.world/skills

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Kevin Veen-BirkenbachandClaude Opus 5.5 committed 2026-12-02 15:12:55 +00:00
1 parent 2a36d01f95
commit ab717debf1
4 files changed
+91 -8

No files matched your search

+16 -2
View File
@@ -7,6 +7,8 @@ from .shell import BackupError, execute_shell_command
if TYPE_CHECKING:
from collections.abc import Sequence
EXEC_NOT_FOUND = 127
def docker_exec_argv(
container: str,
@@ -59,11 +61,23 @@ def has_tool(container: str, tool: str) -> bool:
Executes the binary rather than asking a shell for it: a distroless image
has no shell, and `sh -c 'command -v'` would answer "absent" for every
tool it ships.
Returns:
False only on exit code 127, which dockerd sets when the runtime
reports the executable missing from the image.
Raises:
BackupError: the probe failed for any reason but a missing executable,
e.g. a containerd io-pipe timeout. Reading that as "absent" would
cache the image as no database and copy its files live instead of
dumping them.
"""
try:
execute_shell_command(docker_exec_argv(container, [tool, "--version"]))
except BackupError:
return False
except BackupError as error:
if error.returncode == EXEC_NOT_FOUND:
return False
raise
return True
+13 -2
View File
@@ -25,7 +25,17 @@ SYNC_BYTES = 1 << 16
class BackupError(Exception):
"""Generic exception for backup errors."""
"""Generic exception for backup errors.
Args:
message: what failed.
returncode: the exit code of the command that failed, None when the
error did not come from a command's exit.
"""
def __init__(self, message: str, returncode: int | None = None) -> None:
super().__init__(message)
self.returncode = returncode
def _child_env(env: Mapping[str, str] | None) -> dict[str, str] | None:
@@ -36,7 +46,8 @@ def _fail(command: Sequence[str], returncode: int, out: bytes, err: bytes) -> No
raise BackupError(
f"Error in command: {' '.join(command)}\n"
f"Output: {out}\nError: {err}\n"
f"Exit code: {returncode}"
f"Exit code: {returncode}",
returncode,
)
+38 -4
View File
@@ -1,3 +1,6 @@
import contextlib
import io
import sys
import unittest
from unittest.mock import patch
@@ -18,11 +21,24 @@ class TestHasTool(unittest.TestCase):
with patch.object(docker_mod, "execute_shell_command", return_value=[]):
self.assertTrue(docker_mod.has_tool("c1", "pg_dumpall"))
def test_a_tool_that_exits_non_zero_is_absent(self) -> None:
with patch.object(
docker_mod, "execute_shell_command", side_effect=BackupError("127")
def test_a_tool_missing_from_the_image_is_absent(self) -> None:
not_found = BackupError(
'exec: "mysqldump": executable file not found in $PATH', 127
)
with patch.object(docker_mod, "execute_shell_command", side_effect=not_found):
self.assertFalse(docker_mod.has_tool("c1", "mysqldump"))
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,
)
with (
patch.object(docker_mod, "execute_shell_command", side_effect=transient),
self.assertRaises(BackupError),
):
self.assertFalse(docker_mod.has_tool("c1", "mariadb-dump"))
docker_mod.has_tool("c1", "mariadb-dump")
def test_the_probe_needs_no_shell_in_the_image(self) -> None:
"""A distroless database ships no shell; `sh -c` would deny every tool."""
@@ -40,5 +56,23 @@ 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})"]
with (
patch.object(docker_mod, "docker_exec_argv", return_value=argv),
contextlib.redirect_stdout(io.StringIO()),
):
return docker_mod.has_tool("c1", "mariadb-dump")
def test_exit_127_of_the_real_command_reads_as_absent(self) -> None:
self.assertFalse(self._probe_exiting(127))
def test_any_other_exit_of_the_real_command_raises(self) -> None:
with self.assertRaises(BackupError) as caught:
self._probe_exiting(126)
self.assertEqual(caught.exception.returncode, 126)
if __name__ == "__main__":
unittest.main()
@@ -4,6 +4,7 @@ from unittest.mock import patch
import pandas as pd
from baudolo.backup import dumps as dumps_mod
from baudolo.backup.shell import BackupError
def _df(rows):
@@ -73,6 +74,29 @@ class TestProbeCache(unittest.TestCase):
self.assertEqual(first, second)
self.assertEqual(len(probe.calls), 1)
def test_a_failed_probe_caches_nothing_for_the_image(self):
probe = _Probe(["mariadb-dump"])
dumps_mod._ENGINE_BY_IMAGE.clear()
def _timed_out(container, tool):
raise BackupError("DeadlineExceeded", 126)
with (
patch.object(dumps_mod, "has_tool", _timed_out),
patch.object(dumps_mod, "image_id", probe.image_id),
self.assertRaises(BackupError),
):
dumps_mod.container_engine("c1")
self.assertEqual(dumps_mod._ENGINE_BY_IMAGE, {})
with (
patch.object(dumps_mod, "has_tool", probe.has_tool),
patch.object(dumps_mod, "image_id", probe.image_id),
):
self.assertEqual(
dumps_mod.container_engine("c1"), ("mariadb", "mariadb-dump")
)
def test_a_second_image_is_probed_separately(self):
probe = _Probe(["pg_dumpall"], image={"pg": "sha256:aaa", "app": "sha256:bbb"})
dumps_mod._ENGINE_BY_IMAGE.clear()