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.cwd = cwd
self.name: Optional[str] = None self.name: Optional[str] = None
self.container_engine = container_engine self.container_engine = container_engine
self.env = env # If specified, overwrite environment variables
def __enter__(self) -> "DockerContainer": def __enter__(self) -> "DockerContainer":
@@ -80,6 +79,21 @@ class DockerContainer:
network_args = ["--network=host"] network_args = ["--network=host"]
shell_args = ["linux32", "/bin/bash"] if self.simulate_32_bit else ["/bin/bash"] 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( subprocess.run(
[ [
self.container_engine, self.container_engine,
@@ -88,14 +102,10 @@ class DockerContainer:
f"--name={self.name}", f"--name={self.name}",
"--interactive", "--interactive",
*network_args, *network_args,
# Do we need the hostmout? *volume_args,
# Z-flags is for SELinux
# "--volume=/:/host:Z", # ignored on CircleCI
# "--volume=/:/host",
self.docker_image, self.docker_image,
*shell_args, *shell_args,
], ],
env=self.env,
check=True, check=True,
) )
@@ -109,7 +119,6 @@ class DockerContainer:
], ],
stdin=subprocess.PIPE, stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
env=self.env,
) )
assert self.process.stdin and self.process.stdout assert self.process.stdin and self.process.stdout
@@ -152,7 +161,6 @@ class DockerContainer:
subprocess.run( subprocess.run(
[self.container_engine, "rm", "--force", "-v", self.name], [self.container_engine, "rm", "--force", "-v", self.name],
stdout=subprocess.DEVNULL, stdout=subprocess.DEVNULL,
env=self.env,
check=False, check=False,
) )
self.name = None self.name = None
@@ -170,7 +178,6 @@ class DockerContainer:
shell=True, shell=True,
check=True, check=True,
cwd=from_path, cwd=from_path,
env=self.env,
) )
else: else:
with subprocess.Popen( with subprocess.Popen(
@@ -183,7 +190,6 @@ class DockerContainer:
"-c", "-c",
f"cat > {shell_quote(to_path)}", f"cat > {shell_quote(to_path)}",
], ],
env=self.env,
stdin=subprocess.PIPE, stdin=subprocess.PIPE,
) as docker: ) as docker:
docker.stdin = cast(IO[bytes], docker.stdin) docker.stdin = cast(IO[bytes], docker.stdin)
@@ -212,7 +218,6 @@ class DockerContainer:
shell=True, shell=True,
check=True, check=True,
cwd=to_path, cwd=to_path,
env=self.env,
) )
elif self.container_engine == "podman": elif self.container_engine == "podman":
# The copy out logic that works for docker does not seem to # The copy out logic that works for docker does not seem to
@@ -224,7 +229,6 @@ class DockerContainer:
shell=True, shell=True,
check=True, check=True,
cwd=to_path, cwd=to_path,
env=self.env,
) )
command = f"{self.container_engine} cp {self.name}:/tmp/output-{self.name}.tar output-{self.name}.tar" 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, shell=True,
check=True, check=True,
cwd=to_path, cwd=to_path,
env=self.env,
) )
command = f"tar -xvf output-{self.name}.tar" command = f"tar -xvf output-{self.name}.tar"
subprocess.run( subprocess.run(
@@ -241,7 +244,6 @@ class DockerContainer:
shell=True, shell=True,
check=True, check=True,
cwd=to_path, cwd=to_path,
env=self.env,
) )
os.unlink(to_path / f"output-{self.name}.tar") os.unlink(to_path / f"output-{self.name}.tar")
elif self.container_engine == "docker": elif self.container_engine == "docker":
@@ -251,7 +253,6 @@ class DockerContainer:
shell=True, shell=True,
check=True, check=True,
cwd=to_path, cwd=to_path,
env=self.env,
) )
else: else:
raise KeyError(self.container_engine) raise KeyError(self.container_engine)
@@ -380,7 +381,6 @@ class DockerContainer:
shell=True, shell=True,
check=True, check=True,
cwd=self.cwd, cwd=self.cwd,
env=self.env,
stdin=subprocess.PIPE, stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
) )
+30 -12
View File
@@ -178,21 +178,27 @@ def _setup_podman_vfs(dpath):
@pytest.mark.docker @pytest.mark.docker
@pytest.mark.parametrize("container_kwargs", basis_container_kwargs()) @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: with DockerContainer(**container_kwargs) as container:
assert container.call(["echo", "hello"], capture_output=True) == "hello\n" assert container.call(["echo", "hello"], capture_output=True) == "hello\n"
@pytest.mark.docker @pytest.mark.docker
@pytest.mark.parametrize("container_kwargs", basis_container_kwargs()) @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: with DockerContainer(**container_kwargs) as container:
assert container.call(["printf", "hello"], capture_output=True) == "hello" assert container.call(["printf", "hello"], capture_output=True) == "hello"
@pytest.mark.docker @pytest.mark.docker
@pytest.mark.parametrize("container_kwargs", basis_container_kwargs()) @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) container = DockerContainer(**container_kwargs)
print(container.debug_info()) print(container.debug_info())
with container: with container:
@@ -201,7 +207,9 @@ def test_debug_info(container_kwargs):
@pytest.mark.docker @pytest.mark.docker
@pytest.mark.parametrize("container_kwargs", basis_container_kwargs()) @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: with DockerContainer(**container_kwargs) as container:
assert ( assert (
container.call( container.call(
@@ -221,7 +229,9 @@ def test_cwd(container_kwargs):
@pytest.mark.docker @pytest.mark.docker
@pytest.mark.parametrize("container_kwargs", basis_container_kwargs()) @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: with DockerContainer(**container_kwargs) as container:
docker_containers_listing = subprocess.run( docker_containers_listing = subprocess.run(
f"{container.container_engine} container ls", f"{container.container_engine} container ls",
@@ -229,7 +239,6 @@ def test_container_removed(container_kwargs):
check=True, check=True,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
universal_newlines=True, universal_newlines=True,
env=container.env,
).stdout ).stdout
assert container.name is not None assert container.name is not None
assert container.name in docker_containers_listing assert container.name in docker_containers_listing
@@ -241,14 +250,15 @@ def test_container_removed(container_kwargs):
check=True, check=True,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
universal_newlines=True, universal_newlines=True,
env=container.env,
).stdout ).stdout
assert old_container_name not in docker_containers_listing assert old_container_name not in docker_containers_listing
@pytest.mark.docker @pytest.mark.docker
@pytest.mark.parametrize("container_kwargs", basis_container_kwargs()) @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 # max environment variable size is 128kB
long_env_var_length = 127 * 1024 long_env_var_length = 127 * 1024
large_environment = { large_environment = {
@@ -268,7 +278,9 @@ def test_large_environment(container_kwargs):
@pytest.mark.docker @pytest.mark.docker
@pytest.mark.parametrize("container_kwargs", basis_container_kwargs()) @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: with DockerContainer(**container_kwargs) as container:
# note: the below embedded snippets are in python2 # note: the below embedded snippets are in python2
@@ -320,7 +332,9 @@ def test_binary_output(container_kwargs):
@pytest.mark.docker @pytest.mark.docker
@pytest.mark.parametrize("container_kwargs", basis_container_kwargs()) @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: with DockerContainer(**container_kwargs) as container:
# test copying a file in # test copying a file in
test_binary_data = bytes(random.randrange(256) for _ in range(1000)) 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.docker
@pytest.mark.parametrize("container_kwargs", basis_container_kwargs()) @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: with DockerContainer(**container_kwargs) as container:
test_binary_data = bytes(random.randrange(256) for _ in range(1000)) test_binary_data = bytes(random.randrange(256) for _ in range(1000))
original_test_file = tmp_path / "test.dat" 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.docker
@pytest.mark.parametrize("container_kwargs", basis_container_kwargs()) @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: with DockerContainer(**container_kwargs) as container:
assignment = EnvironmentAssignmentBash("TEST=$(echo 42)") assignment = EnvironmentAssignmentBash("TEST=$(echo 42)")
assert assignment.evaluated_value({}, container.environment_executor) == "42" assert assignment.evaluated_value({}, container.environment_executor) == "42"