|
| 1 | +# Copyright 2025 IBM, Red Hat |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +""" |
| 16 | +Tests for common/utils/utils.py |
| 17 | +""" |
| 18 | + |
| 19 | +import pytest |
| 20 | +from collections import namedtuple |
| 21 | +from codeflare_sdk.common.utils.utils import ( |
| 22 | + update_image, |
| 23 | + get_ray_image_for_python_version, |
| 24 | +) |
| 25 | +from codeflare_sdk.common.utils.constants import ( |
| 26 | + SUPPORTED_PYTHON_VERSIONS, |
| 27 | + CUDA_PY311_RUNTIME_IMAGE, |
| 28 | + CUDA_PY312_RUNTIME_IMAGE, |
| 29 | +) |
| 30 | + |
| 31 | + |
| 32 | +def test_update_image_with_empty_string_python_311(mocker): |
| 33 | + """Test that update_image() with empty string returns default image for Python 3.11.""" |
| 34 | + # Mock sys.version_info to simulate Python 3.11 |
| 35 | + VersionInfo = namedtuple( |
| 36 | + "version_info", ["major", "minor", "micro", "releaselevel", "serial"] |
| 37 | + ) |
| 38 | + mocker.patch("sys.version_info", VersionInfo(3, 11, 0, "final", 0)) |
| 39 | + |
| 40 | + # Test with empty image (should use default for Python 3.11) |
| 41 | + image = update_image("") |
| 42 | + assert image == CUDA_PY311_RUNTIME_IMAGE |
| 43 | + assert image == SUPPORTED_PYTHON_VERSIONS["3.11"] |
| 44 | + |
| 45 | + |
| 46 | +def test_update_image_with_empty_string_python_312(mocker): |
| 47 | + """Test that update_image() with empty string returns default image for Python 3.12.""" |
| 48 | + # Mock sys.version_info to simulate Python 3.12 |
| 49 | + VersionInfo = namedtuple( |
| 50 | + "version_info", ["major", "minor", "micro", "releaselevel", "serial"] |
| 51 | + ) |
| 52 | + mocker.patch("sys.version_info", VersionInfo(3, 12, 0, "final", 0)) |
| 53 | + |
| 54 | + # Test with empty image (should use default for Python 3.12) |
| 55 | + image = update_image("") |
| 56 | + assert image == CUDA_PY312_RUNTIME_IMAGE |
| 57 | + assert image == SUPPORTED_PYTHON_VERSIONS["3.12"] |
| 58 | + |
| 59 | + |
| 60 | +def test_update_image_with_none_python_311(mocker): |
| 61 | + """Test that update_image() with None returns default image for Python 3.11.""" |
| 62 | + # Mock sys.version_info to simulate Python 3.11 |
| 63 | + VersionInfo = namedtuple( |
| 64 | + "version_info", ["major", "minor", "micro", "releaselevel", "serial"] |
| 65 | + ) |
| 66 | + mocker.patch("sys.version_info", VersionInfo(3, 11, 0, "final", 0)) |
| 67 | + |
| 68 | + # Test with None image (should use default for Python 3.11) |
| 69 | + image = update_image(None) |
| 70 | + assert image == CUDA_PY311_RUNTIME_IMAGE |
| 71 | + |
| 72 | + |
| 73 | +def test_update_image_with_none_python_312(mocker): |
| 74 | + """Test that update_image() with None returns default image for Python 3.12.""" |
| 75 | + # Mock sys.version_info to simulate Python 3.12 |
| 76 | + VersionInfo = namedtuple( |
| 77 | + "version_info", ["major", "minor", "micro", "releaselevel", "serial"] |
| 78 | + ) |
| 79 | + mocker.patch("sys.version_info", VersionInfo(3, 12, 0, "final", 0)) |
| 80 | + |
| 81 | + # Test with None image (should use default for Python 3.12) |
| 82 | + image = update_image(None) |
| 83 | + assert image == CUDA_PY312_RUNTIME_IMAGE |
| 84 | + |
| 85 | + |
| 86 | +def test_update_image_with_unsupported_python_version(mocker): |
| 87 | + """Test update_image() warning for unsupported Python versions.""" |
| 88 | + # Mock sys.version_info to simulate Python 3.8 (unsupported) |
| 89 | + VersionInfo = namedtuple( |
| 90 | + "version_info", ["major", "minor", "micro", "releaselevel", "serial"] |
| 91 | + ) |
| 92 | + mocker.patch("sys.version_info", VersionInfo(3, 8, 0, "final", 0)) |
| 93 | + |
| 94 | + # Mock warnings.warn to check if it gets called |
| 95 | + warn_mock = mocker.patch("warnings.warn") |
| 96 | + |
| 97 | + # Call update_image with empty image |
| 98 | + image = update_image("") |
| 99 | + |
| 100 | + # Assert that the warning was called with the expected message |
| 101 | + warn_mock.assert_called_once() |
| 102 | + assert "No default Ray image defined for 3.8" in warn_mock.call_args[0][0] |
| 103 | + assert "3.11, 3.12" in warn_mock.call_args[0][0] |
| 104 | + |
| 105 | + # Assert that no image was set since the Python version is not supported |
| 106 | + assert image is None |
| 107 | + |
| 108 | + |
| 109 | +def test_update_image_with_provided_custom_image(): |
| 110 | + """Test that providing a custom image bypasses auto-detection.""" |
| 111 | + custom_image = "my-custom-ray:latest" |
| 112 | + image = update_image(custom_image) |
| 113 | + |
| 114 | + # Should return the provided image unchanged |
| 115 | + assert image == custom_image |
| 116 | + |
| 117 | + |
| 118 | +def test_update_image_with_provided_image_empty_string(): |
| 119 | + """Test update_image() with provided custom image as a non-empty string.""" |
| 120 | + custom_image = "docker.io/rayproject/ray:2.40.0" |
| 121 | + image = update_image(custom_image) |
| 122 | + |
| 123 | + # Should return the provided image unchanged |
| 124 | + assert image == custom_image |
| 125 | + |
| 126 | + |
| 127 | +def test_get_ray_image_for_python_version_explicit_311(): |
| 128 | + """Test get_ray_image_for_python_version() with explicit Python 3.11.""" |
| 129 | + image = get_ray_image_for_python_version("3.11") |
| 130 | + assert image == CUDA_PY311_RUNTIME_IMAGE |
| 131 | + |
| 132 | + |
| 133 | +def test_get_ray_image_for_python_version_explicit_312(): |
| 134 | + """Test get_ray_image_for_python_version() with explicit Python 3.12.""" |
| 135 | + image = get_ray_image_for_python_version("3.12") |
| 136 | + assert image == CUDA_PY312_RUNTIME_IMAGE |
| 137 | + |
| 138 | + |
| 139 | +def test_get_ray_image_for_python_version_auto_detect_311(mocker): |
| 140 | + """Test get_ray_image_for_python_version() auto-detects Python 3.11.""" |
| 141 | + # Mock sys.version_info to simulate Python 3.11 |
| 142 | + VersionInfo = namedtuple( |
| 143 | + "version_info", ["major", "minor", "micro", "releaselevel", "serial"] |
| 144 | + ) |
| 145 | + mocker.patch("sys.version_info", VersionInfo(3, 11, 0, "final", 0)) |
| 146 | + |
| 147 | + # Test with None (should auto-detect) |
| 148 | + image = get_ray_image_for_python_version() |
| 149 | + assert image == CUDA_PY311_RUNTIME_IMAGE |
| 150 | + |
| 151 | + |
| 152 | +def test_get_ray_image_for_python_version_auto_detect_312(mocker): |
| 153 | + """Test get_ray_image_for_python_version() auto-detects Python 3.12.""" |
| 154 | + # Mock sys.version_info to simulate Python 3.12 |
| 155 | + VersionInfo = namedtuple( |
| 156 | + "version_info", ["major", "minor", "micro", "releaselevel", "serial"] |
| 157 | + ) |
| 158 | + mocker.patch("sys.version_info", VersionInfo(3, 12, 0, "final", 0)) |
| 159 | + |
| 160 | + # Test with None (should auto-detect) |
| 161 | + image = get_ray_image_for_python_version() |
| 162 | + assert image == CUDA_PY312_RUNTIME_IMAGE |
| 163 | + |
| 164 | + |
| 165 | +def test_get_ray_image_for_python_version_unsupported_with_warning(mocker): |
| 166 | + """Test get_ray_image_for_python_version() warns for unsupported versions.""" |
| 167 | + warn_mock = mocker.patch("warnings.warn") |
| 168 | + |
| 169 | + # Test with unsupported version and warn_on_unsupported=True (default) |
| 170 | + image = get_ray_image_for_python_version("3.9", warn_on_unsupported=True) |
| 171 | + |
| 172 | + # Should have warned |
| 173 | + warn_mock.assert_called_once() |
| 174 | + assert "No default Ray image defined for 3.9" in warn_mock.call_args[0][0] |
| 175 | + |
| 176 | + # Should return None |
| 177 | + assert image is None |
| 178 | + |
| 179 | + |
| 180 | +def test_get_ray_image_for_python_version_unsupported_without_warning(): |
| 181 | + """Test get_ray_image_for_python_version() falls back to 3.12 without warning.""" |
| 182 | + # Test with unsupported version and warn_on_unsupported=False |
| 183 | + image = get_ray_image_for_python_version("3.10", warn_on_unsupported=False) |
| 184 | + |
| 185 | + # Should fall back to Python 3.12 image |
| 186 | + assert image == CUDA_PY312_RUNTIME_IMAGE |
| 187 | + |
| 188 | + |
| 189 | +def test_get_ray_image_for_python_version_unsupported_silent_fallback(): |
| 190 | + """Test get_ray_image_for_python_version() silently falls back for old versions.""" |
| 191 | + # Test with Python 3.8 and warn_on_unsupported=False |
| 192 | + image = get_ray_image_for_python_version("3.8", warn_on_unsupported=False) |
| 193 | + |
| 194 | + # Should fall back to Python 3.12 image without warning |
| 195 | + assert image == CUDA_PY312_RUNTIME_IMAGE |
| 196 | + |
| 197 | + |
| 198 | +def test_get_ray_image_for_python_version_none_defaults_to_current(mocker): |
| 199 | + """Test that passing None to get_ray_image_for_python_version() uses current Python.""" |
| 200 | + # Mock sys.version_info to simulate Python 3.11 |
| 201 | + VersionInfo = namedtuple( |
| 202 | + "version_info", ["major", "minor", "micro", "releaselevel", "serial"] |
| 203 | + ) |
| 204 | + mocker.patch("sys.version_info", VersionInfo(3, 11, 5, "final", 0)) |
| 205 | + |
| 206 | + # Passing None should detect the mocked version |
| 207 | + image = get_ray_image_for_python_version(None, warn_on_unsupported=True) |
| 208 | + |
| 209 | + assert image == CUDA_PY311_RUNTIME_IMAGE |
0 commit comments