Imrove the LXC detection (#2599)

This commit is contained in:
Pascal Vizeli 2021-02-22 11:39:18 +01:00 committed by GitHub
parent 7e7e3a7876
commit 785dc64787
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 11 deletions

View File

@ -2,7 +2,6 @@
from contextlib import suppress
from ipaddress import IPv4Address
import logging
from pathlib import Path
from typing import Any, Dict, Optional
import attr
@ -56,11 +55,6 @@ class DockerInfo:
"""Return true, if docker version is supported."""
return self.version >= MIN_SUPPORTED_DOCKER
@property
def inside_lxc(self) -> bool:
"""Return True if the docker run inside lxc."""
return Path("/dev/lxd/sock").exists()
class DockerConfig(JsonConfig):
"""Home Assistant core object for Docker configuration."""

View File

@ -1,4 +1,6 @@
"""Evaluation class for lxc."""
from contextlib import suppress
from pathlib import Path
from typing import List
from ...const import CoreState
@ -26,4 +28,7 @@ class EvaluateLxc(EvaluateBase):
async def evaluate(self):
"""Run evaluation."""
return self.sys_docker.info.inside_lxc
with suppress(OSError):
if "container=lxc" in Path("/proc/1/environ").read_text():
return True
return Path("/dev/lxd/sock").exists()

View File

@ -1,5 +1,6 @@
"""Test evaluation base."""
# pylint: disable=import-error,protected-access
from pathlib import Path
from unittest.mock import patch
from supervisor.const import CoreState
@ -14,12 +15,14 @@ async def test_evaluation(coresys: CoreSys):
assert lxc.reason not in coresys.resolution.unsupported
coresys.docker.info.inside_lxc = True
await lxc()
with patch.object(Path, "exists") as mock_exists:
mock_exists.return_value = True
await lxc()
assert lxc.reason in coresys.resolution.unsupported
coresys.docker.info.inside_lxc = False
await lxc()
with patch.object(Path, "exists") as mock_exists:
mock_exists.return_value = False
await lxc()
assert lxc.reason not in coresys.resolution.unsupported