Does podman work with the original host args

This commit is contained in:
joncrall
2022-06-26 16:57:26 -04:00
parent 90945a38d2
commit 4a7d89b3c8
2 changed files with 46 additions and 28 deletions
+16 -16
View File
@@ -65,7 +65,6 @@ class DockerContainer:
self.cwd = cwd
self.name: Optional[str] = None
self.container_engine = container_engine
self.env = env # If specified, overwrite environment variables
def __enter__(self) -> "DockerContainer":
@@ -80,6 +79,21 @@ class DockerContainer:
network_args = ["--network=host"]
shell_args = ["linux32", "/bin/bash"] if self.simulate_32_bit else ["/bin/bash"]
# volume args are ignored on CircleCI
# For a discussion of if :Z should be included or not:
# https://github.com/pypa/cibuildwheel/pull/966#discussion_r906707824
# https://stackoverflow.com/questions/35218194/what-is-z-flag-in-docker-containers-volumes-from-option/35222815#35222815
# https://github.com/moby/moby/issues/30934
# The Z option indicates that the bind mount content is private and
# unshared. Use extreme caution with these options. Bind-mounting a
# system directory such as /home or /usr with the Z option renders your
# host machine inoperable and you may need to relabel the host machine
# files by hand.
volume_args = ['--volume=/:/host']
# volume_args = ['--volume=/:/host:Z']
# volume_args = []
subprocess.run(
[
self.container_engine,
@@ -88,14 +102,10 @@ class DockerContainer:
f"--name={self.name}",
"--interactive",
*network_args,
# Do we need the hostmout?
# Z-flags is for SELinux
# "--volume=/:/host:Z", # ignored on CircleCI
# "--volume=/:/host",
*volume_args,
self.docker_image,
*shell_args,
],
env=self.env,
check=True,
)
@@ -109,7 +119,6 @@ class DockerContainer:
],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
env=self.env,
)
assert self.process.stdin and self.process.stdout
@@ -152,7 +161,6 @@ class DockerContainer:
subprocess.run(
[self.container_engine, "rm", "--force", "-v", self.name],
stdout=subprocess.DEVNULL,
env=self.env,
check=False,
)
self.name = None
@@ -170,7 +178,6 @@ class DockerContainer:
shell=True,
check=True,
cwd=from_path,
env=self.env,
)
else:
with subprocess.Popen(
@@ -183,7 +190,6 @@ class DockerContainer:
"-c",
f"cat > {shell_quote(to_path)}",
],
env=self.env,
stdin=subprocess.PIPE,
) as docker:
docker.stdin = cast(IO[bytes], docker.stdin)
@@ -212,7 +218,6 @@ class DockerContainer:
shell=True,
check=True,
cwd=to_path,
env=self.env,
)
elif self.container_engine == "podman":
# The copy out logic that works for docker does not seem to
@@ -224,7 +229,6 @@ class DockerContainer:
shell=True,
check=True,
cwd=to_path,
env=self.env,
)
command = f"{self.container_engine} cp {self.name}:/tmp/output-{self.name}.tar output-{self.name}.tar"
@@ -233,7 +237,6 @@ class DockerContainer:
shell=True,
check=True,
cwd=to_path,
env=self.env,
)
command = f"tar -xvf output-{self.name}.tar"
subprocess.run(
@@ -241,7 +244,6 @@ class DockerContainer:
shell=True,
check=True,
cwd=to_path,
env=self.env,
)
os.unlink(to_path / f"output-{self.name}.tar")
elif self.container_engine == "docker":
@@ -251,7 +253,6 @@ class DockerContainer:
shell=True,
check=True,
cwd=to_path,
env=self.env,
)
else:
raise KeyError(self.container_engine)
@@ -380,7 +381,6 @@ class DockerContainer:
shell=True,
check=True,
cwd=self.cwd,
env=self.env,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
)
+30 -12
View File
@@ -178,21 +178,27 @@ def _setup_podman_vfs(dpath):
@pytest.mark.docker
@pytest.mark.parametrize("container_kwargs", basis_container_kwargs())
def test_simple(container_kwargs):
def test_simple(container_kwargs, monkeypatch):
for k, v in container_kwargs.pop('env', {}).items():
monkeypatch.setenv(k, v)
with DockerContainer(**container_kwargs) as container:
assert container.call(["echo", "hello"], capture_output=True) == "hello\n"
@pytest.mark.docker
@pytest.mark.parametrize("container_kwargs", basis_container_kwargs())
def test_no_lf(container_kwargs):
def test_no_lf(container_kwargs, monkeypatch):
for k, v in container_kwargs.pop('env', {}).items():
monkeypatch.setenv(k, v)
with DockerContainer(**container_kwargs) as container:
assert container.call(["printf", "hello"], capture_output=True) == "hello"
@pytest.mark.docker
@pytest.mark.parametrize("container_kwargs", basis_container_kwargs())
def test_debug_info(container_kwargs):
def test_debug_info(container_kwargs, monkeypatch):
for k, v in container_kwargs.pop('env', {}).items():
monkeypatch.setenv(k, v)
container = DockerContainer(**container_kwargs)
print(container.debug_info())
with container:
@@ -201,7 +207,9 @@ def test_debug_info(container_kwargs):
@pytest.mark.docker
@pytest.mark.parametrize("container_kwargs", basis_container_kwargs())
def test_environment(container_kwargs):
def test_environment(container_kwargs, monkeypatch):
for k, v in container_kwargs.pop('env', {}).items():
monkeypatch.setenv(k, v)
with DockerContainer(**container_kwargs) as container:
assert (
container.call(
@@ -221,7 +229,9 @@ def test_cwd(container_kwargs):
@pytest.mark.docker
@pytest.mark.parametrize("container_kwargs", basis_container_kwargs())
def test_container_removed(container_kwargs):
def test_container_removed(container_kwargs, monkeypatch):
for k, v in container_kwargs.pop('env', {}).items():
monkeypatch.setenv(k, v)
with DockerContainer(**container_kwargs) as container:
docker_containers_listing = subprocess.run(
f"{container.container_engine} container ls",
@@ -229,7 +239,6 @@ def test_container_removed(container_kwargs):
check=True,
stdout=subprocess.PIPE,
universal_newlines=True,
env=container.env,
).stdout
assert container.name is not None
assert container.name in docker_containers_listing
@@ -241,14 +250,15 @@ def test_container_removed(container_kwargs):
check=True,
stdout=subprocess.PIPE,
universal_newlines=True,
env=container.env,
).stdout
assert old_container_name not in docker_containers_listing
@pytest.mark.docker
@pytest.mark.parametrize("container_kwargs", basis_container_kwargs())
def test_large_environment(container_kwargs):
def test_large_environment(container_kwargs, monkeypatch):
for k, v in container_kwargs.pop('env', {}).items():
monkeypatch.setenv(k, v)
# max environment variable size is 128kB
long_env_var_length = 127 * 1024
large_environment = {
@@ -268,7 +278,9 @@ def test_large_environment(container_kwargs):
@pytest.mark.docker
@pytest.mark.parametrize("container_kwargs", basis_container_kwargs())
def test_binary_output(container_kwargs):
def test_binary_output(container_kwargs, monkeypatch):
for k, v in container_kwargs.pop('env', {}).items():
monkeypatch.setenv(k, v)
with DockerContainer(**container_kwargs) as container:
# note: the below embedded snippets are in python2
@@ -320,7 +332,9 @@ def test_binary_output(container_kwargs):
@pytest.mark.docker
@pytest.mark.parametrize("container_kwargs", basis_container_kwargs())
def test_file_operation(tmp_path: Path, container_kwargs):
def test_file_operation(tmp_path: Path, container_kwargs, monkeypatch):
for k, v in container_kwargs.pop('env', {}).items():
monkeypatch.setenv(k, v)
with DockerContainer(**container_kwargs) as container:
# test copying a file in
test_binary_data = bytes(random.randrange(256) for _ in range(1000))
@@ -337,7 +351,9 @@ def test_file_operation(tmp_path: Path, container_kwargs):
@pytest.mark.docker
@pytest.mark.parametrize("container_kwargs", basis_container_kwargs())
def test_dir_operations(tmp_path: Path, container_kwargs):
def test_dir_operations(tmp_path: Path, container_kwargs, monkeypatch):
for k, v in container_kwargs.pop('env', {}).items():
monkeypatch.setenv(k, v)
with DockerContainer(**container_kwargs) as container:
test_binary_data = bytes(random.randrange(256) for _ in range(1000))
original_test_file = tmp_path / "test.dat"
@@ -368,7 +384,9 @@ def test_dir_operations(tmp_path: Path, container_kwargs):
@pytest.mark.docker
@pytest.mark.parametrize("container_kwargs", basis_container_kwargs())
def test_environment_executor(container_kwargs):
def test_environment_executor(container_kwargs, monkeypatch):
for k, v in container_kwargs.pop('env', {}).items():
monkeypatch.setenv(k, v)
with DockerContainer(**container_kwargs) as container:
assignment = EnvironmentAssignmentBash("TEST=$(echo 42)")
assert assignment.evaluated_value({}, container.environment_executor) == "42"