From 1e3d3b4d40e3f6c150b9d31965f8b007ef823fc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Tue, 30 Apr 2019 17:47:18 +0300 Subject: [PATCH] test: generalize check whether we're being run in a container Refs https://github.com/scop/bash-completion/issues/312 --- test/t/conftest.py | 26 ++++++++++++++++++++++++-- test/t/test_ifdown.py | 4 ++-- test/t/test_ifup.py | 4 ++-- test/t/test_make.py | 4 ++-- test/t/test_man.py | 4 ++-- test/t/unit/test_unit_ip_addresses.py | 4 ++-- 6 files changed, 34 insertions(+), 12 deletions(-) diff --git a/test/t/conftest.py b/test/t/conftest.py index f3c28e30c3..f65856288e 100644 --- a/test/t/conftest.py +++ b/test/t/conftest.py @@ -2,6 +2,7 @@ import os import re import shlex +import subprocess from typing import Iterable, List, Optional, Tuple, Union import pexpect @@ -442,8 +443,29 @@ def completion(request, bash: pexpect.spawn) -> CompletionResult: return assert_complete(bash, marker.args[0], **marker.kwargs) -def in_docker() -> bool: - return os.path.exists("/.dockerenv") +def in_container() -> bool: + try: + container = subprocess.check_output( + "virt-what || systemd-detect-virt --container", + stderr=subprocess.DEVNULL, + shell=True, + ).strip() + except subprocess.CalledProcessError: + container = None + if container and container != b"none": + return True + if os.path.exists("/.dockerenv"): + return True + try: + with open("/proc/1/environ", "rb") as f: + # LXC, others? + if any( + x.startswith(b"container=") for x in f.readline().split(b"\0") + ): + return True + except OSError: + pass + return False class TestUnitBase: diff --git a/test/t/test_ifdown.py b/test/t/test_ifdown.py index 16447be5f3..e91e4bacd1 100644 --- a/test/t/test_ifdown.py +++ b/test/t/test_ifdown.py @@ -1,10 +1,10 @@ import pytest -from conftest import in_docker +from conftest import in_container class TestIfdown: - @pytest.mark.xfail(in_docker(), reason="Probably fails in docker") + @pytest.mark.xfail(in_container(), reason="Probably fails in a container") @pytest.mark.complete("ifdown ") def test_1(self, completion): assert completion diff --git a/test/t/test_ifup.py b/test/t/test_ifup.py index 62d8eb4ac1..60391e478b 100644 --- a/test/t/test_ifup.py +++ b/test/t/test_ifup.py @@ -1,10 +1,10 @@ import pytest -from conftest import in_docker +from conftest import in_container class TestIfup: - @pytest.mark.xfail(in_docker(), reason="Probably fails in docker") + @pytest.mark.xfail(in_container(), reason="Probably fails in a container") @pytest.mark.complete("ifup ") def test_1(self, completion): assert completion diff --git a/test/t/test_make.py b/test/t/test_make.py index 9c76f83c1d..ae468fa93e 100644 --- a/test/t/test_make.py +++ b/test/t/test_make.py @@ -2,7 +2,7 @@ import pytest -from conftest import in_docker +from conftest import in_container class TestMake: @@ -35,7 +35,7 @@ def test_6(self, bash, completion): os.remove("%s/make/%s" % (bash.cwd, "extra_makefile")) @pytest.mark.xfail( - in_docker() and os.environ.get("DIST") == "centos6", + in_container() and os.environ.get("DIST") == "centos6", reason="Fails for some unknown reason on CentOS 6, " "even though the behavior appears to be correct", ) diff --git a/test/t/test_man.py b/test/t/test_man.py index 60021d9958..2da3087e55 100644 --- a/test/t/test_man.py +++ b/test/t/test_man.py @@ -2,7 +2,7 @@ import pytest -from conftest import assert_bash_exec, in_docker +from conftest import assert_bash_exec, in_container @pytest.mark.bashcomp(ignore_env=r"^[+-]MANPATH=") @@ -43,7 +43,7 @@ def test_3(self, completion): assert completion == "man/quux.8" @pytest.mark.xfail( - in_docker() and os.environ.get("DIST") == "centos6", + in_container() and os.environ.get("DIST") == "centos6", reason="TODO: Fails in CentOS for some reason, unknown " "how to trigger same behavior as tests show (is " "different and correct when tried manually, but here " diff --git a/test/t/unit/test_unit_ip_addresses.py b/test/t/unit/test_unit_ip_addresses.py index cd7a38abc1..8120c88216 100644 --- a/test/t/unit/test_unit_ip_addresses.py +++ b/test/t/unit/test_unit_ip_addresses.py @@ -1,6 +1,6 @@ import pytest -from conftest import assert_bash_exec, in_docker +from conftest import assert_bash_exec, in_container @pytest.mark.bashcomp(cmd=None, ignore_env=r"^\+COMPREPLY=") @@ -41,7 +41,7 @@ def test_3(self, functions, completion): assert completion assert all("." in x for x in completion) - @pytest.mark.xfail(in_docker(), reason="Probably fails in docker") + @pytest.mark.xfail(in_container(), reason="Probably fails in a container") @pytest.mark.complete("ia6 ") def test_4(self, functions, completion): """_ip_addresses -6 should complete ipv6 addresses."""