Skip to content

Commit 758b9bd

Browse files
authored
Docker compose v2 (#34)
* Support docker compose v2 * black * changelog * python-sa * minor
1 parent 0bc9079 commit 758b9bd

File tree

4 files changed

+74
-27
lines changed

4 files changed

+74
-27
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55

66
The v1 release supports Cisco IOS-XR release versions from 7.7.1 to 7.9.1.
77

8+
### v1.1.12 (2023-06-30)
9+
10+
- Add Docker Compose v2 support to `host-check` and `xr-compose` scripts.
11+
812
### v1.1.11 (2023-06-23)
913

1014
- Add a check in `host-check` to verify the correct kernel modules parameters are being used.

scripts/host-check

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1364,21 +1364,27 @@ def check_supports_d_type() -> CheckFuncReturn:
13641364

13651365
def check_docker_compose() -> CheckFuncReturn:
13661366
"""Check that the docker-compose version is 1.18.x or higher."""
1367-
cmd = "docker-compose --version"
1368-
try:
1369-
# Use a longer than default timeout as this command has been known to
1370-
# time out.
1371-
version_str = run_cmd(shlex.split(cmd), timeout=10)[0].strip()
1372-
except (FileNotFoundError, subprocess.SubprocessError):
1367+
plugin_cmd = "docker compose version"
1368+
standalone_cmd = "docker-compose --version"
1369+
for cmd in (plugin_cmd, standalone_cmd):
1370+
try:
1371+
# Use a longer than default timeout as this command has been known to
1372+
# time out.
1373+
version_str = run_cmd(shlex.split(cmd), timeout=10)[0].strip()
1374+
break
1375+
except (FileNotFoundError, subprocess.SubprocessError):
1376+
pass
1377+
else:
13731378
return (
13741379
CheckState.FAILED,
1375-
f"Docker Compose not found (checked with {cmd!r}).\n"
1380+
f"Docker Compose not found (checked with {plugin_cmd!r} and {standalone_cmd!r}).\n"
13761381
f"Launching XRd topologies with xr-compose requires docker-compose.\n"
13771382
f"See installation instructions at https://docs.docker.com/compose/install/.",
13781383
)
13791384

13801385
version_match = re.match(
1381-
r"docker-compose version (\d+\.\d+(?:\.\d+)?)", version_str
1386+
r"(?:docker-compose|Docker Compose) version v?(\d+\.\d+(?:\.\d+)?)",
1387+
version_str,
13821388
)
13831389
if not version_match:
13841390
return (

scripts/xr-compose

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -926,6 +926,8 @@ class XRCompose:
926926

927927
self.image = image
928928

929+
self._docker_compose_cmd: Optional[List[str]] = None
930+
929931
self.check_input_and_output()
930932

931933
# Load the input YAML
@@ -939,6 +941,29 @@ class XRCompose:
939941
self.services: List[Service] = []
940942
self.errors: List[Exception] = []
941943

944+
@property
945+
def docker_compose_cmd(self) -> List[str]:
946+
if self._docker_compose_cmd is None:
947+
plugin = (["docker", "compose"], ["version"])
948+
standalone = (["docker-compose"], ["--version"])
949+
for docker_compose, version_cmd in (plugin, standalone):
950+
try:
951+
subprocess.check_output(
952+
docker_compose + version_cmd, stderr=subprocess.DEVNULL
953+
)
954+
# Cache result if no error is raised
955+
self._docker_compose_cmd = docker_compose
956+
break
957+
except (FileNotFoundError, subprocess.SubprocessError):
958+
pass
959+
else:
960+
raise Error(
961+
"Docker Compose not found.\n"
962+
"Launching XRd topologies with xr-compose requires docker-compose.\n"
963+
"See installation instructions at https://docs.docker.com/compose/install/.",
964+
)
965+
return self._docker_compose_cmd
966+
942967
# -------------------------------------------------------------------
943968
# Methods related to checking input arguments
944969
# -------------------------------------------------------------------
@@ -971,7 +996,7 @@ class XRCompose:
971996
# is not covered by the python API. Don't include the last empty line.
972997
try:
973998
docker_compose_out = subprocess.check_output(
974-
["docker-compose", "-f", self.output_file, "ps"],
999+
self.docker_compose_cmd + ["-f", self.output_file, "ps"],
9751000
universal_newlines=True,
9761001
)
9771002
except subprocess.CalledProcessError:
@@ -1889,7 +1914,8 @@ class XRCompose:
18891914
"""
18901915
LOGGER.info("Launching docker-compose topology...")
18911916
subprocess.run(
1892-
["docker-compose", "-f", self.output_file, "up", "-d"], check=True
1917+
self.docker_compose_cmd + ["-f", self.output_file, "up", "-d"],
1918+
check=True,
18931919
)
18941920

18951921

tests/test_host_check.py

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3338,13 +3338,25 @@ class TestDockerCompose(_CheckTestBase):
33383338

33393339
check_group = "xr-compose"
33403340
check_name = "docker-compose"
3341-
cmds = ["docker-compose --version"]
3341+
cmds = ["docker compose version", "docker-compose --version"]
33423342

3343-
def test_success(self, capsys):
3343+
def test_success_plugin(self, capsys):
33443344
"""Test the success case."""
3345-
result, output = self.perform_check(
3346-
capsys, cmd_effects="docker-compose version 1.18.0"
3345+
cmd_effects = ["Docker Compose version v2.19.0", None]
3346+
result, output = self.perform_check(capsys, cmd_effects=cmd_effects)
3347+
assert (
3348+
textwrap.dedent(output)
3349+
== "PASS -- docker-compose (version 2.19.0)\n"
33473350
)
3351+
assert result is CheckState.SUCCESS
3352+
3353+
def test_success_standalone(self, capsys):
3354+
"""Test the success case."""
3355+
cmd_effects = [
3356+
subprocess.SubprocessError,
3357+
"docker-compose version 1.18.0",
3358+
]
3359+
result, output = self.perform_check(capsys, cmd_effects=cmd_effects)
33483360
assert (
33493361
textwrap.dedent(output)
33503362
== "PASS -- docker-compose (version 1.18.0)\n"
@@ -3353,13 +3365,12 @@ def test_success(self, capsys):
33533365

33543366
def test_subproc_error(self, capsys):
33553367
"""Test a subprocess error being raised."""
3356-
result, output = self.perform_check(
3357-
capsys, cmd_effects=subprocess.SubprocessError
3358-
)
3368+
cmd_effects = [subprocess.SubprocessError, subprocess.SubprocessError]
3369+
result, output = self.perform_check(capsys, cmd_effects=cmd_effects)
33593370
assert textwrap.dedent(output) == textwrap.dedent(
33603371
"""\
33613372
FAIL -- docker-compose
3362-
Docker Compose not found (checked with 'docker-compose --version').
3373+
Docker Compose not found (checked with 'docker compose version' and 'docker-compose --version').
33633374
Launching XRd topologies with xr-compose requires docker-compose.
33643375
See installation instructions at https://docs.docker.com/compose/install/.
33653376
"""
@@ -3368,9 +3379,8 @@ def test_subproc_error(self, capsys):
33683379

33693380
def test_no_version_match(self, capsys):
33703381
"""Test failure to match the version in the output."""
3371-
result, output = self.perform_check(
3372-
capsys, cmd_effects="unexpected output"
3373-
)
3382+
cmd_effects = ["unexpected output", None]
3383+
result, output = self.perform_check(capsys, cmd_effects=cmd_effects)
33743384
assert textwrap.dedent(output) == textwrap.dedent(
33753385
"""\
33763386
ERROR -- docker-compose
@@ -3381,9 +3391,11 @@ def test_no_version_match(self, capsys):
33813391

33823392
def test_old_version(self, capsys):
33833393
"""Test the version being too old."""
3384-
result, output = self.perform_check(
3385-
capsys, cmd_effects="docker-compose version 1.17.10"
3386-
)
3394+
cmd_effects = [
3395+
subprocess.SubprocessError,
3396+
"docker-compose version 1.17.10",
3397+
]
3398+
result, output = self.perform_check(capsys, cmd_effects=cmd_effects)
33873399
assert textwrap.dedent(output) == textwrap.dedent(
33883400
"""\
33893401
FAIL -- docker-compose
@@ -3395,9 +3407,8 @@ def test_old_version(self, capsys):
33953407

33963408
def test_unexpected_error(self, capsys):
33973409
"""Test unexpected error being raised."""
3398-
result, output = self.perform_check(
3399-
capsys, cmd_effects=Exception("test exception")
3400-
)
3410+
cmd_effects = [Exception("test exception"), None]
3411+
result, output = self.perform_check(capsys, cmd_effects=cmd_effects)
34013412
assert textwrap.dedent(output) == textwrap.dedent(
34023413
"""\
34033414
ERROR -- docker-compose

0 commit comments

Comments
 (0)