2026-12-02 08:22:04 +00:00
|
|
|
import contextlib
|
|
|
|
|
import io
|
|
|
|
|
import sys
|
2026-08-17 13:51:26 +02:00
|
|
|
import unittest
|
|
|
|
|
from unittest.mock import patch
|
|
|
|
|
|
|
|
|
|
from baudolo.backup import docker as docker_mod
|
2026-08-18 01:47:38 +02:00
|
|
|
from baudolo.backup.shell import BackupError
|
2026-08-17 13:51:26 +02:00
|
|
|
|
2026-12-02 16:03:34 +00:00
|
|
|
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"
|
|
|
|
|
)
|
|
|
|
|
|
2026-08-17 13:51:26 +02:00
|
|
|
|
|
|
|
|
class TestImageId(unittest.TestCase):
|
|
|
|
|
def test_the_id_is_returned_without_surrounding_whitespace(self) -> None:
|
|
|
|
|
with patch.object(
|
|
|
|
|
docker_mod, "execute_shell_command", return_value=["sha256:abc \n"]
|
|
|
|
|
):
|
|
|
|
|
self.assertEqual(docker_mod.image_id("c1"), "sha256:abc")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestHasTool(unittest.TestCase):
|
|
|
|
|
def test_a_tool_that_runs_is_present(self) -> None:
|
|
|
|
|
with patch.object(docker_mod, "execute_shell_command", return_value=[]):
|
|
|
|
|
self.assertTrue(docker_mod.has_tool("c1", "pg_dumpall"))
|
|
|
|
|
|
2026-12-02 08:22:04 +00:00
|
|
|
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"))
|
|
|
|
|
|
2026-12-02 16:03:34 +00:00
|
|
|
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"))
|
|
|
|
|
|
2026-12-02 08:22:04 +00:00
|
|
|
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."""
|
2026-12-02 16:03:34 +00:00
|
|
|
pipe_timeout = (
|
|
|
|
|
b"DeadlineExceeded: failed to start io pipe copy: unable to copy pipes"
|
2026-12-02 08:22:04 +00:00
|
|
|
)
|
2026-12-02 16:03:34 +00:00
|
|
|
transient = BackupError(pipe_timeout.decode(), 126, pipe_timeout)
|
2026-12-02 08:22:04 +00:00
|
|
|
with (
|
|
|
|
|
patch.object(docker_mod, "execute_shell_command", side_effect=transient),
|
|
|
|
|
self.assertRaises(BackupError),
|
2026-08-17 13:51:26 +02:00
|
|
|
):
|
2026-12-02 08:22:04 +00:00
|
|
|
docker_mod.has_tool("c1", "mariadb-dump")
|
2026-08-17 13:51:26 +02:00
|
|
|
|
|
|
|
|
def test_the_probe_needs_no_shell_in_the_image(self) -> None:
|
|
|
|
|
"""A distroless database ships no shell; `sh -c` would deny every tool."""
|
|
|
|
|
captured = []
|
|
|
|
|
|
|
|
|
|
def _capture(cmd):
|
|
|
|
|
captured.append(cmd)
|
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
|
with patch.object(docker_mod, "execute_shell_command", side_effect=_capture):
|
|
|
|
|
docker_mod.has_tool("c1", "pg_dumpall")
|
|
|
|
|
|
2026-08-17 16:30:25 +02:00
|
|
|
self.assertEqual(
|
|
|
|
|
captured, [["docker", "exec", "c1", "pg_dumpall", "--version"]]
|
|
|
|
|
)
|
2026-08-17 13:51:26 +02:00
|
|
|
|
|
|
|
|
|
2026-12-02 08:22:04 +00:00
|
|
|
class TestProbeExitCode(unittest.TestCase):
|
2026-12-02 16:03:34 +00:00
|
|
|
def _probe_exiting(self, code: int, stderr: str = "") -> bool:
|
|
|
|
|
argv = [
|
|
|
|
|
sys.executable,
|
|
|
|
|
"-c",
|
|
|
|
|
f"import sys; sys.stderr.write({stderr!r}); sys.exit({code})",
|
|
|
|
|
]
|
2026-12-02 08:22:04 +00:00
|
|
|
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)
|
|
|
|
|
|
2026-12-02 16:03:34 +00:00
|
|
|
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'
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
2026-12-02 08:22:04 +00:00
|
|
|
|
2026-08-17 13:51:26 +02:00
|
|
|
if __name__ == "__main__":
|
|
|
|
|
unittest.main()
|