1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
From 1e3d3b4d40e3f6c150b9d31965f8b007ef823fc7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ville=20Skytt=C3=A4?= <ville.skytta@iki.fi>
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."""
|