Skip to content

Commit

Permalink
system: try to detect docker when systemd-detect-virt fails
Browse files Browse the repository at this point in the history
  • Loading branch information
orndorffgrant committed Jun 21, 2023
1 parent 658c38c commit 8b45f6f
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
14 changes: 13 additions & 1 deletion uaclient/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,19 @@ def get_virt_type() -> str:
out, _ = subp(["systemd-detect-virt"])
return out.strip()
except exceptions.ProcessExecutionError:
return ""
# The main known place where that will fail is in a docker/podman
# container that doesn't have it installed. So we look for hints
# of that situation to report it accurately.
try:
proc_1_cgroup = load_file("/proc/1/cgroup")
if "docker" in proc_1_cgroup or "buildkit" in proc_1_cgroup:
return "docker"
elif "buildah" in proc_1_cgroup:
return "podman"
else:
return ""
except Exception:
return ""


@lru_cache(maxsize=None)
Expand Down
58 changes: 58 additions & 0 deletions uaclient/tests/test_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,64 @@ def test_get_dpkg_arch(self, m_subp, stdout, expected):
]


class TestGetVirtType:
@pytest.mark.parametrize(
[
"subp_side_effect",
"load_file_side_effect",
"expected",
],
[
([("", "")], None, ""),
([("lxc\n", "")], None, "lxc"),
([("lxc\n", "anything")], None, "lxc"),
(
exceptions.ProcessExecutionError(cmd="", stdout="", stderr=""),
[""],
"",
),
(
exceptions.ProcessExecutionError(cmd="", stdout="", stderr=""),
["line\nline\nlinedockerline\nline"],
"docker",
),
(
exceptions.ProcessExecutionError(cmd="", stdout="", stderr=""),
["line\nline\nlinebuildkitline\nline"],
"docker",
),
(
exceptions.ProcessExecutionError(cmd="", stdout="", stderr=""),
["line\nline\nlinebuildahline\nline"],
"podman",
),
(
exceptions.ProcessExecutionError(cmd="", stdout="", stderr=""),
["line\nline\nline\nline"],
"",
),
(
exceptions.ProcessExecutionError(cmd="", stdout="", stderr=""),
FileNotFoundError(),
"",
),
],
)
@mock.patch("uaclient.system.load_file")
@mock.patch("uaclient.system.subp")
def test_get_virt_type(
self,
m_subp,
m_load_file,
subp_side_effect,
load_file_side_effect,
expected,
):
m_subp.side_effect = subp_side_effect
m_load_file.side_effect = load_file_side_effect
assert expected == system.get_virt_type.__wrapped__()


class TestIsLTS:
@pytest.mark.parametrize(
"series, supported_esm, expected",
Expand Down

0 comments on commit 8b45f6f

Please sign in to comment.