fix: more reliably validate Podman API version (#2016)
* fix: parse version strings that include dashes It's possible for some container engines to report their versions with a dash (e.g., "4.9.4-rhel"), which breaks packaging.version.Version's ability to parse the string. This commit introduces a version_from_string method which santizies the version string and returns an instance of Version. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * cleanup: pacify ruff * cleanup: further pacify ruff * fix: properly define _version_from_string method Also, lift the method up and prefix with a "_" to better match the existing conventions * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * refactor: more robust podman ver check Use the "podman --version" command instead of "podman version -f {{json .}}" for better reliability across distributions. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fix: oci engine version check Lower Docker API check to 1.41 Podman versions are not PEP440 compliant, remove distro specific suffixes before parsing. Add tests with real-world outputs and some made up ones. * fix: UX on OCIEngineTooOldError * Add FlexibleVersion per review comment --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: mayeut <mayeut@users.noreply.github.com>
This commit is contained in:
co-authored by
pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
mayeut
parent
735e88deba
commit
dfd01af9aa
@@ -8,13 +8,21 @@ import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import textwrap
|
||||
from contextlib import nullcontext
|
||||
from pathlib import Path, PurePath, PurePosixPath
|
||||
|
||||
import pytest
|
||||
import tomli_w
|
||||
|
||||
import cibuildwheel.oci_container
|
||||
from cibuildwheel.environment import EnvironmentAssignmentBash
|
||||
from cibuildwheel.oci_container import OCIContainer, OCIContainerEngineConfig, OCIPlatform
|
||||
from cibuildwheel.errors import OCIEngineTooOldError
|
||||
from cibuildwheel.oci_container import (
|
||||
OCIContainer,
|
||||
OCIContainerEngineConfig,
|
||||
OCIPlatform,
|
||||
_check_engine_version,
|
||||
)
|
||||
from cibuildwheel.util import CIProvider, detect_ci_provider
|
||||
|
||||
# Test utilities
|
||||
@@ -569,3 +577,64 @@ def test_multiarch_image(container_engine, platform):
|
||||
OCIPlatform.S390X: "s390x",
|
||||
}
|
||||
assert output_map[platform] == output.strip()
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("engine_name", "version", "context"),
|
||||
[
|
||||
(
|
||||
"docker",
|
||||
None, # 17.12.1-ce does supports "docker version --format '{{json . }}'" so a version before that
|
||||
pytest.raises(OCIEngineTooOldError),
|
||||
),
|
||||
(
|
||||
"docker",
|
||||
'{"Client":{"Version":"19.03.15","ApiVersion": "1.40"},"Server":{"ApiVersion": "1.40"}}',
|
||||
pytest.raises(OCIEngineTooOldError),
|
||||
),
|
||||
(
|
||||
"docker",
|
||||
'{"Client":{"Version":"20.10.0","ApiVersion":"1.41"},"Server":{"ApiVersion":"1.41"}}',
|
||||
nullcontext(),
|
||||
),
|
||||
(
|
||||
"docker",
|
||||
'{"Client":{"Version":"24.0.0","ApiVersion":"1.43"},"Server":{"ApiVersion":"1.43"}}',
|
||||
nullcontext(),
|
||||
),
|
||||
(
|
||||
"docker",
|
||||
'{"Client":{"ApiVersion":"1.43"},"Server":{"ApiVersion":"1.30"}}',
|
||||
pytest.raises(OCIEngineTooOldError),
|
||||
),
|
||||
(
|
||||
"docker",
|
||||
'{"Client":{"ApiVersion":"1.30"},"Server":{"ApiVersion":"1.43"}}',
|
||||
pytest.raises(OCIEngineTooOldError),
|
||||
),
|
||||
("podman", '{"Client":{"Version":"5.2.0"},"Server":{"Version":"5.1.2"}}', nullcontext()),
|
||||
("podman", '{"Client":{"Version":"4.9.4-rhel"}}', nullcontext()),
|
||||
(
|
||||
"podman",
|
||||
'{"Client":{"Version":"5.2.0"},"Server":{"Version":"2.1.2"}}',
|
||||
pytest.raises(OCIEngineTooOldError),
|
||||
),
|
||||
(
|
||||
"podman",
|
||||
'{"Client":{"Version":"2.2.0"},"Server":{"Version":"5.1.2"}}',
|
||||
pytest.raises(OCIEngineTooOldError),
|
||||
),
|
||||
("podman", '{"Client":{"Version":"3.0~rc1-rhel"}}', nullcontext()),
|
||||
("podman", '{"Client":{"Version":"2.1.0~rc1"}}', pytest.raises(OCIEngineTooOldError)),
|
||||
],
|
||||
)
|
||||
def test_engine_version(engine_name, version, context, monkeypatch):
|
||||
def mockcall(*args, **kwargs):
|
||||
if version is None:
|
||||
raise subprocess.CalledProcessError(1, " ".join(str(arg) for arg in args))
|
||||
return version
|
||||
|
||||
monkeypatch.setattr(cibuildwheel.oci_container, "call", mockcall)
|
||||
engine = OCIContainerEngineConfig.from_config_string(engine_name)
|
||||
with context:
|
||||
_check_engine_version(engine)
|
||||
|
||||
Reference in New Issue
Block a user