|
| 1 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"). You |
| 4 | +# may not use this file except in compliance with the License. A copy of |
| 5 | +# the License is located at |
| 6 | +# |
| 7 | +# http://aws.amazon.com/apache2.0/ |
| 8 | +# |
| 9 | +# or in the "license" file accompanying this file. This file is |
| 10 | +# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
| 11 | +# ANY KIND, either express or implied. See the License for the specific |
| 12 | +# language governing permissions and limitations under the License. |
| 13 | +"""Integration tests for Docker Compose version detection fix (issue #5739). |
| 14 | +
|
| 15 | +These tests verify that _get_compose_cmd_prefix correctly accepts Docker Compose |
| 16 | +versions >= 2 (including v3, v4, v5, etc.) rather than only accepting v2. |
| 17 | +
|
| 18 | +The tests run against the real Docker Compose installation on the machine — no mocking. |
| 19 | +Requires: Docker with Compose plugin installed (any version >= 2). |
| 20 | +""" |
| 21 | +from __future__ import absolute_import |
| 22 | + |
| 23 | +import re |
| 24 | +import subprocess |
| 25 | +import tempfile |
| 26 | + |
| 27 | +import pytest |
| 28 | + |
| 29 | +from sagemaker.core.local.image import _SageMakerContainer |
| 30 | +from sagemaker.core.modules.local_core.local_container import ( |
| 31 | + _LocalContainer as CoreModulesLocalContainer, |
| 32 | +) |
| 33 | +from sagemaker.core.shapes import Channel, DataSource, S3DataSource |
| 34 | +from sagemaker.train.local.local_container import ( |
| 35 | + _LocalContainer as TrainLocalContainer, |
| 36 | +) |
| 37 | + |
| 38 | + |
| 39 | +def _get_installed_compose_major_version(): |
| 40 | + """Return the major version int of the installed Docker Compose, or None.""" |
| 41 | + try: |
| 42 | + output = subprocess.check_output( |
| 43 | + ["docker", "compose", "version"], |
| 44 | + stderr=subprocess.DEVNULL, |
| 45 | + encoding="UTF-8", |
| 46 | + ) |
| 47 | + match = re.search(r"v(\d+)", output.strip()) |
| 48 | + if match: |
| 49 | + return int(match.group(1)) |
| 50 | + except (subprocess.CalledProcessError, FileNotFoundError): |
| 51 | + pass |
| 52 | + return None |
| 53 | + |
| 54 | + |
| 55 | +# Skip the entire module if Docker Compose >= 2 is not available |
| 56 | +_compose_major = _get_installed_compose_major_version() |
| 57 | +pytestmark = pytest.mark.skipif( |
| 58 | + _compose_major is None or _compose_major < 2, |
| 59 | + reason=f"Docker Compose >= 2 required (found: v{_compose_major})", |
| 60 | +) |
| 61 | + |
| 62 | + |
| 63 | +def _make_basic_channel(): |
| 64 | + """Create a minimal Channel for constructing _LocalContainer instances.""" |
| 65 | + data_source = DataSource( |
| 66 | + s3_data_source=S3DataSource( |
| 67 | + s3_uri="s3://bucket/data", |
| 68 | + s3_data_type="S3Prefix", |
| 69 | + s3_data_distribution_type="FullyReplicated", |
| 70 | + ) |
| 71 | + ) |
| 72 | + return Channel(channel_name="training", data_source=data_source) |
| 73 | + |
| 74 | + |
| 75 | +def _make_local_container(container_cls): |
| 76 | + """Construct a _LocalContainer with minimal valid args. |
| 77 | +
|
| 78 | + sagemaker_session is None since _get_compose_cmd_prefix doesn't use it, |
| 79 | + and the Pydantic model rejects Mock objects. |
| 80 | + """ |
| 81 | + container_root = tempfile.mkdtemp(prefix="sagemaker-integ-compose-") |
| 82 | + return container_cls( |
| 83 | + training_job_name="integ-test-compose-detection", |
| 84 | + instance_type="local", |
| 85 | + instance_count=1, |
| 86 | + image="test-image:latest", |
| 87 | + container_root=container_root, |
| 88 | + input_data_config=[_make_basic_channel()], |
| 89 | + environment={}, |
| 90 | + hyper_parameters={}, |
| 91 | + container_entrypoint=[], |
| 92 | + container_arguments=[], |
| 93 | + sagemaker_session=None, |
| 94 | + ) |
| 95 | + |
| 96 | + |
| 97 | +@pytest.fixture |
| 98 | +def _core_modules_container(): |
| 99 | + return _make_local_container(CoreModulesLocalContainer) |
| 100 | + |
| 101 | + |
| 102 | +@pytest.fixture |
| 103 | +def _train_container(): |
| 104 | + return _make_local_container(TrainLocalContainer) |
| 105 | + |
| 106 | + |
| 107 | +class TestDockerComposeVersionDetection: |
| 108 | + """Integration tests for _get_compose_cmd_prefix across all three code locations. |
| 109 | +
|
| 110 | + Validates the fix for https://github.com/aws/sagemaker-python-sdk/issues/5739 |
| 111 | + where Docker Compose v3+ was incorrectly rejected. |
| 112 | + """ |
| 113 | + |
| 114 | + def test_sagemaker_core_image_accepts_installed_compose(self): |
| 115 | + """sagemaker-core local/image.py _SageMakerContainer._get_compose_cmd_prefix |
| 116 | + should accept the installed Docker Compose version.""" |
| 117 | + result = _SageMakerContainer._get_compose_cmd_prefix() |
| 118 | + |
| 119 | + assert result == ["docker", "compose"], ( |
| 120 | + f"Expected ['docker', 'compose'] but got {result}. " |
| 121 | + f"Installed Docker Compose is v{_compose_major}." |
| 122 | + ) |
| 123 | + |
| 124 | + def test_sagemaker_core_modules_local_container_accepts_installed_compose( |
| 125 | + self, _core_modules_container |
| 126 | + ): |
| 127 | + """sagemaker-core modules/local_core/local_container.py |
| 128 | + _LocalContainer._get_compose_cmd_prefix should accept the installed version.""" |
| 129 | + result = _core_modules_container._get_compose_cmd_prefix() |
| 130 | + |
| 131 | + assert result == ["docker", "compose"], ( |
| 132 | + f"Expected ['docker', 'compose'] but got {result}. " |
| 133 | + f"Installed Docker Compose is v{_compose_major}." |
| 134 | + ) |
| 135 | + |
| 136 | + def test_sagemaker_train_local_container_accepts_installed_compose( |
| 137 | + self, _train_container |
| 138 | + ): |
| 139 | + """sagemaker-train local/local_container.py |
| 140 | + _LocalContainer._get_compose_cmd_prefix should accept the installed version.""" |
| 141 | + result = _train_container._get_compose_cmd_prefix() |
| 142 | + |
| 143 | + assert result == ["docker", "compose"], ( |
| 144 | + f"Expected ['docker', 'compose'] but got {result}. " |
| 145 | + f"Installed Docker Compose is v{_compose_major}." |
| 146 | + ) |
| 147 | + |
| 148 | + def test_returned_command_is_functional(self): |
| 149 | + """The command returned by _get_compose_cmd_prefix should actually work.""" |
| 150 | + cmd = _SageMakerContainer._get_compose_cmd_prefix() |
| 151 | + |
| 152 | + # Run the returned command with "version" to prove it's functional |
| 153 | + result = subprocess.run( |
| 154 | + cmd + ["version"], |
| 155 | + capture_output=True, |
| 156 | + text=True, |
| 157 | + timeout=10, |
| 158 | + ) |
| 159 | + assert result.returncode == 0, ( |
| 160 | + f"Command {cmd + ['version']} failed: {result.stderr}" |
| 161 | + ) |
| 162 | + assert "version" in result.stdout.lower(), ( |
| 163 | + f"Unexpected output from {cmd + ['version']}: {result.stdout}" |
| 164 | + ) |
| 165 | + |
| 166 | + @pytest.mark.skipif( |
| 167 | + _compose_major is not None and _compose_major < 3, |
| 168 | + reason="This test specifically validates v3+ acceptance (installed is v2)", |
| 169 | + ) |
| 170 | + def test_v3_plus_specifically_accepted(self): |
| 171 | + """When Docker Compose v3+ is installed, it must be accepted — not rejected. |
| 172 | +
|
| 173 | + This is the core regression test for issue #5739. |
| 174 | + """ |
| 175 | + result = _SageMakerContainer._get_compose_cmd_prefix() |
| 176 | + assert result == ["docker", "compose"], ( |
| 177 | + f"Docker Compose v{_compose_major} was rejected. " |
| 178 | + "This is the exact bug described in issue #5739." |
| 179 | + ) |
0 commit comments