|
10 | 10 | # distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
11 | 11 | # ANY KIND, either express or implied. See the License for the specific |
12 | 12 | # language governing permissions and limitations under the License. |
13 | | -from unittest.mock import patch, call |
| 13 | +from unittest.mock import patch, call, Mock |
14 | 14 | import pytest |
| 15 | +import subprocess |
15 | 16 |
|
16 | | -from sagemaker.train.local.local_container import _rmtree |
| 17 | +from sagemaker.train.local.local_container import _rmtree, _LocalContainer |
| 18 | +from sagemaker.core.shapes import DataSource, S3DataSource |
| 19 | +from sagemaker.core.shapes import Channel |
17 | 20 |
|
18 | 21 | IMAGE = "763104351884.dkr.ecr.us-east-1.amazonaws.com/pytorch-training:2.1-cpu-py310" |
19 | 22 |
|
@@ -78,3 +81,120 @@ def test_rmtree_no_image_raises(self, mock_rmtree): |
78 | 81 |
|
79 | 82 | with pytest.raises(PermissionError): |
80 | 83 | _rmtree("/tmp/test") |
| 84 | + |
| 85 | + |
| 86 | +@pytest.fixture |
| 87 | +def _basic_channel(): |
| 88 | + """Create a basic channel for testing.""" |
| 89 | + data_source = DataSource( |
| 90 | + s3_data_source=S3DataSource( |
| 91 | + s3_uri="s3://bucket/data", |
| 92 | + s3_data_type="S3Prefix", |
| 93 | + s3_data_distribution_type="FullyReplicated", |
| 94 | + ) |
| 95 | + ) |
| 96 | + return Channel(channel_name="training", data_source=data_source) |
| 97 | + |
| 98 | + |
| 99 | +@pytest.fixture |
| 100 | +def _mock_session(): |
| 101 | + """Create a mock SageMaker session.""" |
| 102 | + session = Mock() |
| 103 | + session.boto_region_name = "us-west-2" |
| 104 | + session.boto_session = Mock() |
| 105 | + session.s3_resource = Mock() |
| 106 | + session.s3_resource.meta.client._endpoint.host = "https://s3.us-west-2.amazonaws.com" |
| 107 | + return session |
| 108 | + |
| 109 | + |
| 110 | +def _make_container(_mock_session, _basic_channel): |
| 111 | + """Helper to create a _LocalContainer for compose prefix tests.""" |
| 112 | + return _LocalContainer( |
| 113 | + training_job_name="test-job", |
| 114 | + instance_type="local", |
| 115 | + instance_count=1, |
| 116 | + image="test-image:latest", |
| 117 | + container_root="/tmp/test", |
| 118 | + input_data_config=[_basic_channel], |
| 119 | + environment={}, |
| 120 | + hyper_parameters={}, |
| 121 | + container_entrypoint=[], |
| 122 | + container_arguments=[], |
| 123 | + sagemaker_session=_mock_session, |
| 124 | + ) |
| 125 | + |
| 126 | + |
| 127 | +class TestGetComposeCmdPrefix: |
| 128 | + """Test cases for _get_compose_cmd_prefix version detection.""" |
| 129 | + |
| 130 | + @patch("sagemaker.train.local.local_container.subprocess.check_output") |
| 131 | + def test_get_compose_cmd_prefix_with_docker_compose_v2(self, mock_check_output, _mock_session, _basic_channel): |
| 132 | + """Docker Compose v2 should be accepted.""" |
| 133 | + container = _make_container(_mock_session, _basic_channel) |
| 134 | + mock_check_output.return_value = "Docker Compose version v2.20.0" |
| 135 | + result = container._get_compose_cmd_prefix() |
| 136 | + assert result == ["docker", "compose"] |
| 137 | + |
| 138 | + @patch("sagemaker.train.local.local_container.subprocess.check_output") |
| 139 | + def test_get_compose_cmd_prefix_with_docker_compose_v5(self, mock_check_output, _mock_session, _basic_channel): |
| 140 | + """Docker Compose v5 should be accepted.""" |
| 141 | + container = _make_container(_mock_session, _basic_channel) |
| 142 | + mock_check_output.return_value = "Docker Compose version v5.1.1" |
| 143 | + result = container._get_compose_cmd_prefix() |
| 144 | + assert result == ["docker", "compose"] |
| 145 | + |
| 146 | + @patch("sagemaker.train.local.local_container.subprocess.check_output") |
| 147 | + def test_get_compose_cmd_prefix_with_docker_compose_v3(self, mock_check_output, _mock_session, _basic_channel): |
| 148 | + """Docker Compose v3 should be accepted.""" |
| 149 | + container = _make_container(_mock_session, _basic_channel) |
| 150 | + mock_check_output.return_value = "Docker Compose version v3.0.0" |
| 151 | + result = container._get_compose_cmd_prefix() |
| 152 | + assert result == ["docker", "compose"] |
| 153 | + |
| 154 | + @patch("sagemaker.train.local.local_container.shutil.which") |
| 155 | + @patch("sagemaker.train.local.local_container.subprocess.check_output") |
| 156 | + def test_get_compose_cmd_prefix_with_docker_compose_v1_falls_through( |
| 157 | + self, mock_check_output, mock_which, _mock_session, _basic_channel |
| 158 | + ): |
| 159 | + """Docker Compose v1 should not be accepted; falls through to docker-compose standalone.""" |
| 160 | + container = _make_container(_mock_session, _basic_channel) |
| 161 | + mock_check_output.return_value = "docker-compose version 1.29.2" |
| 162 | + mock_which.return_value = "/usr/bin/docker-compose" |
| 163 | + result = container._get_compose_cmd_prefix() |
| 164 | + assert result == ["docker-compose"] |
| 165 | + |
| 166 | + @patch("sagemaker.train.local.local_container.shutil.which") |
| 167 | + @patch("sagemaker.train.local.local_container.subprocess.check_output") |
| 168 | + def test_get_compose_cmd_prefix_with_docker_compose_v1_no_standalone_raises( |
| 169 | + self, mock_check_output, mock_which, _mock_session, _basic_channel |
| 170 | + ): |
| 171 | + """Docker Compose v1 with no standalone fallback should raise ImportError.""" |
| 172 | + container = _make_container(_mock_session, _basic_channel) |
| 173 | + mock_check_output.return_value = "docker-compose version v1.29.2" |
| 174 | + mock_which.return_value = None |
| 175 | + with pytest.raises(ImportError, match="Docker Compose is not installed"): |
| 176 | + container._get_compose_cmd_prefix() |
| 177 | + |
| 178 | + @patch("sagemaker.train.local.local_container.shutil.which") |
| 179 | + @patch("sagemaker.train.local.local_container.subprocess.check_output") |
| 180 | + def test_get_compose_cmd_prefix_not_installed_raises( |
| 181 | + self, mock_check_output, mock_which, _mock_session, _basic_channel |
| 182 | + ): |
| 183 | + """When docker compose is not installed at all, should raise ImportError.""" |
| 184 | + container = _make_container(_mock_session, _basic_channel) |
| 185 | + mock_check_output.side_effect = subprocess.CalledProcessError(1, "cmd") |
| 186 | + mock_which.return_value = None |
| 187 | + with pytest.raises(ImportError, match="Docker Compose is not installed"): |
| 188 | + container._get_compose_cmd_prefix() |
| 189 | + |
| 190 | + @patch("sagemaker.train.local.local_container.shutil.which") |
| 191 | + @patch("sagemaker.train.local.local_container.subprocess.check_output") |
| 192 | + def test_get_compose_cmd_prefix_standalone_fallback( |
| 193 | + self, mock_check_output, mock_which, _mock_session, _basic_channel |
| 194 | + ): |
| 195 | + """When docker compose plugin fails, falls back to docker-compose standalone.""" |
| 196 | + container = _make_container(_mock_session, _basic_channel) |
| 197 | + mock_check_output.side_effect = subprocess.CalledProcessError(1, "cmd") |
| 198 | + mock_which.return_value = "/usr/local/bin/docker-compose" |
| 199 | + result = container._get_compose_cmd_prefix() |
| 200 | + assert result == ["docker-compose"] |
0 commit comments