This commit is contained in:
joncrall
2022-06-26 17:10:02 -04:00
parent 4a7d89b3c8
commit 1599383429
2 changed files with 24 additions and 36 deletions
+4 -19
View File
@@ -55,7 +55,6 @@ class DockerContainer:
simulate_32_bit: bool = False, simulate_32_bit: bool = False,
cwd: Optional[PathOrStr] = None, cwd: Optional[PathOrStr] = None,
container_engine: str = "docker", container_engine: str = "docker",
env: Optional[Dict[str, str]] = None,
): ):
if not docker_image: if not docker_image:
raise ValueError("Must have a non-empty docker image to run.") raise ValueError("Must have a non-empty docker image to run.")
@@ -80,20 +79,6 @@ class DockerContainer:
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,
@@ -101,8 +86,8 @@ class DockerContainer:
"--env=CIBUILDWHEEL", "--env=CIBUILDWHEEL",
f"--name={self.name}", f"--name={self.name}",
"--interactive", "--interactive",
"--volume=/:/host", # ignored on CircleCI
*network_args, *network_args,
*volume_args,
self.docker_image, self.docker_image,
*shell_args, *shell_args,
], ],
@@ -126,13 +111,13 @@ class DockerContainer:
self.bash_stdout = self.process.stdout self.bash_stdout = self.process.stdout
# run a noop command to block until the container is responding # run a noop command to block until the container is responding
self.call(["/bin/true"], cwd="/") self.call(["/bin/true"])
if self.cwd: if self.cwd:
# Although `docker create -w` does create the working dir if it # Although `docker create -w` does create the working dir if it
# does not exist, podman does not. There does not seem to be a way # does not exist, podman does not. There does not seem to be a way
# to setup a workdir for a container running in podman. # to setup a workdir for a container running in podman.
self.call(["mkdir", "-p", str(self.cwd)], cwd="/") self.call(["mkdir", "-p", os.fspath(self.cwd)])
return self return self
@@ -389,4 +374,4 @@ class DockerContainer:
def shell_quote(path: PurePath) -> str: def shell_quote(path: PurePath) -> str:
return shlex.quote(str(path)) return shlex.quote(os.fspath(path))
+20 -17
View File
@@ -36,7 +36,7 @@ _STATE = {
# @atexit.register # @atexit.register
def _cleanup_tempdir(): def _cleanup_podman_vfs_tempdir():
""" """
Cleans up any configuration written by :func:`basis_container_kwargs`. Cleans up any configuration written by :func:`basis_container_kwargs`.
@@ -58,10 +58,6 @@ def _cleanup_tempdir():
# unless you fake a UID of 0. The package rootlesskit helps with that. # unless you fake a UID of 0. The package rootlesskit helps with that.
if _STATE['using_podman']: if _STATE['using_podman']:
subprocess.call(['podman', 'unshare', 'rm', '-rf', temp_test_dir.name]) subprocess.call(['podman', 'unshare', 'rm', '-rf', temp_test_dir.name])
try:
temp_test_dir.cleanup()
except Exception as ex:
print(f"Issue cleaning up ex = {ex!r}")
_STATE['temp_test_dir'] = None _STATE['temp_test_dir'] = None
@@ -82,24 +78,31 @@ def basis_container_kwargs():
parameterized test. parameterized test.
""" """
if _STATE['temp_test_dir'] is None: # TODO: Pytest should be aware of if we are trying to test docker / podman
# Only setup the temp directory once for all tests # or not
_STATE['temp_test_dir'] = tempfile.TemporaryDirectory(prefix="cibw_test_")
# Register the special cleanup hook after the temp directory is created
# to ensure that it runs before the temp directory logic runs (which
# will not handle cases where there is a fake root UID).
atexit.register(_cleanup_tempdir)
temp_test_dir = _STATE['temp_test_dir']
HAVE_DOCKER = bool(shutil.which("docker")) HAVE_DOCKER = bool(shutil.which("docker"))
HAVE_PODMAN = bool(shutil.which("podman")) HAVE_PODMAN = bool(shutil.which("podman"))
if HAVE_DOCKER: REQUESTED_DOCKER = HAVE_DOCKER
REQUESTED_PODMAN = HAVE_PODMAN
if _STATE['temp_test_dir'] is None:
# Only setup the temp directory once for all tests
_STATE['temp_test_dir'] = tempfile.TemporaryDirectory(prefix="cibw_test_")
if REQUESTED_PODMAN:
# Register the special cleanup hook after the temp directory is
# created to ensure that it runs before the temp directory logic
# runs (which will not handle cases where there is a fake root
# UID).
atexit.register(_cleanup_podman_vfs_tempdir)
temp_test_dir = _STATE['temp_test_dir']
if REQUESTED_DOCKER:
# Basic podman configuration # Basic podman configuration
yield {"container_engine": "docker", "docker_image": DEFAULT_IMAGE} yield {"container_engine": "docker", "docker_image": DEFAULT_IMAGE}
if HAVE_PODMAN: if REQUESTED_PODMAN:
# Basic podman usage # Basic podman usage
_STATE['using_podman'] = True _STATE['using_podman'] = True
yield {"container_engine": "podman", "docker_image": DEFAULT_IMAGE} yield {"container_engine": "podman", "docker_image": DEFAULT_IMAGE}