def test_in_docker_docker_not_in_file(): non_docker_cgroup_example = b'''\ 12:perf_event:/ 11:hugetlb:/ 10:devices:/ 9:blkio:/ 8:rdma:/ 7:cpuset:/ 6:cpu,cpuacct:/ 5:freezer:/ 4:memory:/ 3:pids:/ 2:net_cls,net_prio:/ 1:name=systemd:/init.scope 0::/init.scope ''' with _mock_open(non_docker_cgroup_example): assert docker._is_in_docker() is False
def test_in_docker_docker_in_file(): docker_cgroup_example = b'''\ 12:hugetlb:/docker/c33988ec7651ebc867cb24755eaf637a6734088bc7eef59d5799293a9e5450f7 11:blkio:/docker/c33988ec7651ebc867cb24755eaf637a6734088bc7eef59d5799293a9e5450f7 10:freezer:/docker/c33988ec7651ebc867cb24755eaf637a6734088bc7eef59d5799293a9e5450f7 9:cpu,cpuacct:/docker/c33988ec7651ebc867cb24755eaf637a6734088bc7eef59d5799293a9e5450f7 8:pids:/docker/c33988ec7651ebc867cb24755eaf637a6734088bc7eef59d5799293a9e5450f7 7:rdma:/ 6:net_cls,net_prio:/docker/c33988ec7651ebc867cb24755eaf637a6734088bc7eef59d5799293a9e5450f7 5:cpuset:/docker/c33988ec7651ebc867cb24755eaf637a6734088bc7eef59d5799293a9e5450f7 4:devices:/docker/c33988ec7651ebc867cb24755eaf637a6734088bc7eef59d5799293a9e5450f7 3:memory:/docker/c33988ec7651ebc867cb24755eaf637a6734088bc7eef59d5799293a9e5450f7 2:perf_event:/docker/c33988ec7651ebc867cb24755eaf637a6734088bc7eef59d5799293a9e5450f7 1:name=systemd:/docker/c33988ec7651ebc867cb24755eaf637a6734088bc7eef59d5799293a9e5450f7 0::/system.slice/containerd.service ''' # noqa: E501 with _mock_open(docker_cgroup_example): assert docker._is_in_docker() is True
def test_in_docker_docker_not_in_file(): with _mock_open(NON_DOCKER_CGROUP_EXAMPLE): assert docker._is_in_docker() is False
def test_in_docker_docker_in_file(): with _mock_open(DOCKER_CGROUP_EXAMPLE): assert docker._is_in_docker() is True
def test_in_docker_no_file(): with mock.patch.object(builtins, 'open', side_effect=FileNotFoundError): assert docker._is_in_docker() is False