Skip to content

Commit

Permalink
ci: Support docker compose v2.21 as it introduces a breaking change (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
achimnol authored Sep 17, 2023
1 parent b86f11c commit 7b6c700
Showing 1 changed file with 34 additions and 15 deletions.
49 changes: 34 additions & 15 deletions tests/common/redis_helper/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import aiohttp
import async_timeout
import pytest
from packaging.version import Version
from packaging.version import parse as parse_version

from ai.backend.testutils.pants import get_parallel_slot

Expand Down Expand Up @@ -116,29 +118,36 @@ async def is_snap_docker():


class DockerComposeRedisSentinelCluster(AbstractRedisSentinelCluster):
async def probe_docker_compose(self) -> list[str]:
async def probe_docker_compose(self) -> tuple[tuple[str, ...], Version]:
# Try v2 first and fallback to v1
p = await asyncio.create_subprocess_exec(
"docker",
"compose",
"version",
stdout=asyncio.subprocess.DEVNULL,
stderr=asyncio.subprocess.DEVNULL,
)
exit_code = await p.wait()
if exit_code == 0:
compose_cmd = ["docker", "compose"]
for compose_cmd in [("docker", "compose"), ("docker-compose",)]:
try:
p = await asyncio.create_subprocess_exec(
*compose_cmd,
"version",
"--format=json",
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.DEVNULL,
)
assert p.stdout is not None
stdout = await p.stdout.read()
version_data = json.loads(stdout)
compose_version = parse_version(version_data["version"])
exit_code = await p.wait()
if exit_code == 0:
return compose_cmd, compose_version
except FileNotFoundError:
continue
else:
compose_cmd = ["docker-compose"]
return compose_cmd
raise RuntimeError("Could not detect the docker compose version")

@contextlib.asynccontextmanager
async def make_cluster(self) -> AsyncIterator[RedisClusterInfo]:
template_cfg_dir = Path(__file__).parent
template_compose_file = template_cfg_dir / "redis-cluster.yml"
assert template_compose_file.exists()
project_name = f"{self.test_ns}_{self.test_case_ns}"
compose_cmd = await self.probe_docker_compose()
compose_cmd, compose_version = await self.probe_docker_compose()

template_cfg_files = [
"redis-cluster.yml",
Expand Down Expand Up @@ -230,7 +239,17 @@ async def make_cluster(self) -> AsyncIterator[RedisClusterInfo]:
)
try:
assert p.stdout is not None
ps_output = json.loads(await p.stdout.read())
ps_output = []
if compose_version >= Version("2.21.0"):
# Adapt to a breaking change in docker/compose#10918!
# Fortunately we only use the "ID" field from each container object.
while True:
line = await p.stdout.readline()
if not line:
break
ps_output.append(json.loads(line))
else:
ps_output = json.loads(await p.stdout.read())
pprint(f"{ps_output=}")
except json.JSONDecodeError:
pytest.fail(
Expand Down

0 comments on commit 7b6c700

Please sign in to comment.