|
| 1 | +# Copyright 2025 The Kubeflow Authors. |
| 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 | +import pytest |
| 16 | + |
| 17 | +from kubeflow.trainer.test.common import FAILED, SUCCESS, TestCase |
| 18 | +from kubeflow.trainer.types import types |
| 19 | + |
| 20 | + |
| 21 | +@pytest.mark.parametrize( |
| 22 | + "test_case", |
| 23 | + [ |
| 24 | + TestCase( |
| 25 | + name="valid datacacheinitializer creation", |
| 26 | + expected_status=SUCCESS, |
| 27 | + config={ |
| 28 | + "storage_uri": "cache://test_schema/test_table", |
| 29 | + "num_data_nodes": 3, |
| 30 | + "metadata_loc": "gs://my-bucket/metadata", |
| 31 | + }, |
| 32 | + expected_output=None, |
| 33 | + ), |
| 34 | + TestCase( |
| 35 | + name="invalid num_data_nodes raises ValueError", |
| 36 | + expected_status=FAILED, |
| 37 | + config={ |
| 38 | + "storage_uri": "cache://test_schema/test_table", |
| 39 | + "num_data_nodes": 1, |
| 40 | + "metadata_loc": "gs://my-bucket/metadata", |
| 41 | + }, |
| 42 | + expected_error=ValueError, |
| 43 | + ), |
| 44 | + TestCase( |
| 45 | + name="zero num_data_nodes raises ValueError", |
| 46 | + expected_status=FAILED, |
| 47 | + config={ |
| 48 | + "storage_uri": "cache://test_schema/test_table", |
| 49 | + "num_data_nodes": 0, |
| 50 | + "metadata_loc": "gs://my-bucket/metadata", |
| 51 | + }, |
| 52 | + expected_error=ValueError, |
| 53 | + ), |
| 54 | + TestCase( |
| 55 | + name="negative num_data_nodes raises ValueError", |
| 56 | + expected_status=FAILED, |
| 57 | + config={ |
| 58 | + "storage_uri": "cache://test_schema/test_table", |
| 59 | + "num_data_nodes": -1, |
| 60 | + "metadata_loc": "gs://my-bucket/metadata", |
| 61 | + }, |
| 62 | + expected_error=ValueError, |
| 63 | + ), |
| 64 | + TestCase( |
| 65 | + name="invalid storage_uri without cache:// prefix raises ValueError", |
| 66 | + expected_status=FAILED, |
| 67 | + config={ |
| 68 | + "storage_uri": "invalid://test_schema/test_table", |
| 69 | + "num_data_nodes": 3, |
| 70 | + "metadata_loc": "gs://my-bucket/metadata", |
| 71 | + }, |
| 72 | + expected_error=ValueError, |
| 73 | + ), |
| 74 | + TestCase( |
| 75 | + name="invalid storage_uri format raises ValueError", |
| 76 | + expected_status=FAILED, |
| 77 | + config={ |
| 78 | + "storage_uri": "cache://test_schema", |
| 79 | + "num_data_nodes": 3, |
| 80 | + "metadata_loc": "gs://my-bucket/metadata", |
| 81 | + }, |
| 82 | + expected_error=ValueError, |
| 83 | + ), |
| 84 | + TestCase( |
| 85 | + name="invalid storage_uri with too many parts raises ValueError", |
| 86 | + expected_status=FAILED, |
| 87 | + config={ |
| 88 | + "storage_uri": "cache://test_schema/test_table/extra", |
| 89 | + "num_data_nodes": 3, |
| 90 | + "metadata_loc": "gs://my-bucket/metadata", |
| 91 | + }, |
| 92 | + expected_error=ValueError, |
| 93 | + ), |
| 94 | + ], |
| 95 | +) |
| 96 | +def test_data_cache_initializer(test_case: TestCase): |
| 97 | + """Test DataCacheInitializer creation and validation.""" |
| 98 | + print("Executing test:", test_case.name) |
| 99 | + |
| 100 | + try: |
| 101 | + initializer = types.DataCacheInitializer( |
| 102 | + storage_uri=test_case.config["storage_uri"], |
| 103 | + num_data_nodes=test_case.config["num_data_nodes"], |
| 104 | + metadata_loc=test_case.config["metadata_loc"], |
| 105 | + ) |
| 106 | + |
| 107 | + assert test_case.expected_status == SUCCESS |
| 108 | + # Only check the fields that were passed in config, not auto-generated ones |
| 109 | + for key in test_case.config: |
| 110 | + assert getattr(initializer, key) == test_case.config[key] |
| 111 | + |
| 112 | + except Exception as e: |
| 113 | + assert test_case.expected_status == FAILED |
| 114 | + assert type(e) is test_case.expected_error |
| 115 | + print("test execution complete") |
0 commit comments