-
Notifications
You must be signed in to change notification settings - Fork 41
feat: Implement Training Options pattern for flexible TrainJob customization #91
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
abhijeet-dhumal
wants to merge
7
commits into
kubeflow:main
Choose a base branch
from
abhijeet-dhumal:add-labels-annotations-support#87
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,308
−87
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f26d914
Add simplified Training Options for TrainJob labels, annotations and …
abhijeet-dhumal 4aff23f
Implement mixin-based options architecture (KubernetesCompatible, Loc…
abhijeet-dhumal b31bd37
Replace compatibility_mixin property with abstract validate_options a…
abhijeet-dhumal 15a7644
fix: Forward podSpecOverrides to TrainJob API in KubernetesBackend.tr…
abhijeet-dhumal df59e2c
fix: Replace PodSpecOverride with PodTemplateOverride API and add con…
abhijeet-dhumal fd89e68
refactor: consolidate training options into centralized package
abhijeet-dhumal 5a7c563
feat: add spec-level labels and annotations options for derivative Jo…
abhijeet-dhumal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,282 @@ | ||
| # Copyright 2025 The Kubeflow Authors. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """ | ||
| Unit tests for TrainerClient option handling and error messages. | ||
| """ | ||
|
|
||
| from unittest.mock import Mock, patch | ||
|
|
||
| import pytest | ||
|
|
||
| from kubeflow.trainer.api.trainer_client import TrainerClient | ||
| from kubeflow.trainer.backends.kubernetes.types import KubernetesBackendConfig | ||
| from kubeflow.trainer.backends.localprocess.types import LocalProcessBackendConfig | ||
| from kubeflow.trainer.options import Annotations, Labels | ||
| from kubeflow.trainer.types import types | ||
|
|
||
|
|
||
| class TestTrainerClientOptionValidation: | ||
abhijeet-dhumal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """Test TrainerClient option validation integration.""" | ||
|
|
||
| def test_trainer_client_passes_options_to_backend(self): | ||
| """Test that TrainerClient passes options to backend correctly.""" | ||
| config = LocalProcessBackendConfig() | ||
| client = TrainerClient(backend_config=config) | ||
|
|
||
| def simple_func(): | ||
| return "test" | ||
|
|
||
| trainer = types.CustomTrainer(func=simple_func) | ||
| options = [Labels({"app": "test"})] | ||
|
|
||
| with pytest.raises(ValueError) as exc_info: | ||
| client.train(trainer=trainer, options=options) | ||
|
|
||
| error_msg = str(exc_info.value) | ||
| assert "The following options are not compatible with this backend" in error_msg | ||
| assert "Labels" in error_msg | ||
|
|
||
| @patch("kubernetes.config.load_kube_config") | ||
| @patch("kubernetes.client.CustomObjectsApi") | ||
| @patch("kubernetes.client.CoreV1Api") | ||
| def test_trainer_client_with_kubernetes_backend( | ||
| self, mock_core_api, mock_custom_api, mock_load_config | ||
| ): | ||
| """Test TrainerClient with KubernetesBackend and compatible options.""" | ||
| mock_custom_api.return_value = Mock() | ||
| mock_core_api.return_value = Mock() | ||
|
|
||
| config = KubernetesBackendConfig() | ||
| client = TrainerClient(backend_config=config) | ||
|
|
||
| def simple_func(): | ||
| return "test" | ||
|
|
||
| trainer = types.CustomTrainer(func=simple_func) | ||
| options = [Labels({"app": "test"}), Annotations({"desc": "test"})] | ||
|
|
||
| with pytest.raises((ValueError, RuntimeError)) as exc_info: | ||
| client.train(trainer=trainer, options=options) | ||
|
|
||
| error_msg = str(exc_info.value) | ||
| # Should either fail with runtime requirement or K8s connection error | ||
| assert ( | ||
| "Runtime is required" in error_msg | ||
| or "Failed to get clustertrainingruntimes" in error_msg | ||
| ) | ||
|
|
||
| def test_trainer_client_empty_options(self): | ||
| """Test TrainerClient with empty options.""" | ||
| config = LocalProcessBackendConfig() | ||
| client = TrainerClient(backend_config=config) | ||
|
|
||
| def simple_func(): | ||
| return "test" | ||
|
|
||
| trainer = types.CustomTrainer(func=simple_func) | ||
|
|
||
| with pytest.raises(ValueError) as exc_info: | ||
| client.train(trainer=trainer, options=[]) | ||
|
|
||
| error_msg = str(exc_info.value) | ||
| assert "Runtime must be provided for LocalProcessBackend" in error_msg | ||
|
|
||
|
|
||
| class TestTrainerClientErrorHandling: | ||
| """Test TrainerClient error handling improvements.""" | ||
|
|
||
| def test_missing_runtime_error_message(self): | ||
| """Test improved error message for missing runtime.""" | ||
| config = LocalProcessBackendConfig() | ||
| client = TrainerClient(backend_config=config) | ||
|
|
||
| def simple_func(): | ||
| return "test" | ||
|
|
||
| trainer = types.CustomTrainer(func=simple_func) | ||
|
|
||
| with pytest.raises(ValueError) as exc_info: | ||
| client.train(trainer=trainer) | ||
|
|
||
| error_msg = str(exc_info.value) | ||
| # The error message should contain the runtime requirement | ||
| assert "Runtime must be provided for LocalProcessBackend" in error_msg | ||
|
|
||
| def test_option_validation_error_propagation(self): | ||
| """Test that option validation errors are properly propagated.""" | ||
| config = LocalProcessBackendConfig() | ||
| client = TrainerClient(backend_config=config) | ||
|
|
||
| def simple_func(): | ||
| return "test" | ||
|
|
||
| trainer = types.CustomTrainer(func=simple_func) | ||
| options = [Labels({"app": "test"}), Annotations({"desc": "test"})] | ||
|
|
||
| with pytest.raises(ValueError) as exc_info: | ||
| client.train(trainer=trainer, options=options) | ||
|
|
||
| error_msg = str(exc_info.value) | ||
| assert "The following options are not compatible with this backend" in error_msg | ||
| assert "Labels" in error_msg | ||
| assert "Annotations" in error_msg | ||
| assert "The following options are not compatible with this backend" in error_msg | ||
|
|
||
| def test_error_message_does_not_contain_runtime_help_for_option_errors(self): | ||
| """Test that option validation errors don't get runtime help text.""" | ||
| config = LocalProcessBackendConfig() | ||
| client = TrainerClient(backend_config=config) | ||
|
|
||
| def simple_func(): | ||
| return "test" | ||
|
|
||
| trainer = types.CustomTrainer(func=simple_func) | ||
| options = [Labels({"app": "test"})] | ||
|
|
||
| with pytest.raises(ValueError) as exc_info: | ||
| client.train(trainer=trainer, options=options) | ||
|
|
||
| error_msg = str(exc_info.value) | ||
| assert "The following options are not compatible with this backend" in error_msg | ||
| assert "Example usage:" not in error_msg | ||
|
|
||
| @patch("kubernetes.config.load_kube_config") | ||
| @patch("kubernetes.client.CustomObjectsApi") | ||
| @patch("kubernetes.client.CoreV1Api") | ||
| def test_kubernetes_backend_error_handling( | ||
| self, mock_core_api, mock_custom_api, mock_load_config | ||
| ): | ||
| """Test error handling with KubernetesBackend.""" | ||
| mock_custom_api.return_value = Mock() | ||
| mock_core_api.return_value = Mock() | ||
|
|
||
| config = KubernetesBackendConfig() | ||
| client = TrainerClient(backend_config=config) | ||
|
|
||
| def simple_func(): | ||
| return "test" | ||
|
|
||
| trainer = types.CustomTrainer(func=simple_func) | ||
|
|
||
| with pytest.raises((ValueError, RuntimeError)) as exc_info: | ||
| client.train(trainer=trainer) | ||
|
|
||
| error_msg = str(exc_info.value) | ||
| # Should either fail with runtime requirement or K8s connection error | ||
| assert ( | ||
| "Runtime is required" in error_msg | ||
| or "Failed to get clustertrainingruntimes" in error_msg | ||
| ) | ||
|
|
||
|
|
||
| class TestTrainerClientBackendSelection: | ||
| """Test TrainerClient backend selection and configuration.""" | ||
|
|
||
| @patch("kubernetes.config.load_kube_config") | ||
| @patch("kubernetes.client.CustomObjectsApi") | ||
| @patch("kubernetes.client.CoreV1Api") | ||
| def test_default_backend_is_kubernetes(self, mock_core_api, mock_custom_api, mock_load_config): | ||
| """Test that default backend is Kubernetes.""" | ||
| mock_custom_api.return_value = Mock() | ||
| mock_core_api.return_value = Mock() | ||
|
|
||
| client = TrainerClient() | ||
|
|
||
| from kubeflow.trainer.backends.kubernetes.backend import KubernetesBackend | ||
|
|
||
| assert isinstance(client.backend, KubernetesBackend) | ||
|
|
||
| def test_local_process_backend_selection(self): | ||
| """Test LocalProcess backend selection.""" | ||
| config = LocalProcessBackendConfig() | ||
| client = TrainerClient(backend_config=config) | ||
|
|
||
| from kubeflow.trainer.backends.localprocess.backend import LocalProcessBackend | ||
|
|
||
| assert isinstance(client.backend, LocalProcessBackend) | ||
|
|
||
| @patch("kubernetes.config.load_kube_config") | ||
| @patch("kubernetes.client.CustomObjectsApi") | ||
| @patch("kubernetes.client.CoreV1Api") | ||
| def test_kubernetes_backend_selection(self, mock_core_api, mock_custom_api, mock_load_config): | ||
| """Test Kubernetes backend selection.""" | ||
| mock_custom_api.return_value = Mock() | ||
| mock_core_api.return_value = Mock() | ||
|
|
||
| config = KubernetesBackendConfig() | ||
| client = TrainerClient(backend_config=config) | ||
|
|
||
| from kubeflow.trainer.backends.kubernetes.backend import KubernetesBackend | ||
|
|
||
| assert isinstance(client.backend, KubernetesBackend) | ||
|
|
||
|
|
||
| class TestTrainerClientOptionFlow: | ||
| """Test the complete option flow through TrainerClient.""" | ||
|
|
||
| def test_option_validation_happens_early(self): | ||
| """Test that option validation happens before other validations.""" | ||
| config = LocalProcessBackendConfig() | ||
| client = TrainerClient(backend_config=config) | ||
|
|
||
| def simple_func(): | ||
| return "test" | ||
|
|
||
| trainer = types.CustomTrainer(func=simple_func) | ||
| options = [Labels({"app": "test"})] | ||
|
|
||
| with pytest.raises(ValueError) as exc_info: | ||
| client.train(trainer=trainer, options=options) | ||
|
|
||
| error_msg = str(exc_info.value) | ||
| assert "The following options are not compatible with this backend" in error_msg | ||
|
|
||
| def test_multiple_option_types_validation(self): | ||
| """Test validation with multiple different option types.""" | ||
| config = LocalProcessBackendConfig() | ||
| client = TrainerClient(backend_config=config) | ||
|
|
||
| def simple_func(): | ||
| return "test" | ||
|
|
||
| trainer = types.CustomTrainer(func=simple_func) | ||
| options = [ | ||
| Labels({"app": "test"}), | ||
| Annotations({"desc": "test"}), | ||
| ] | ||
|
|
||
| with pytest.raises(ValueError) as exc_info: | ||
| client.train(trainer=trainer, options=options) | ||
|
|
||
| error_msg = str(exc_info.value) | ||
| assert "The following options are not compatible with this backend" in error_msg | ||
| assert "Labels" in error_msg | ||
| assert "Annotations" in error_msg | ||
|
|
||
| def test_none_options_handling(self): | ||
| """Test that None options are handled correctly.""" | ||
| config = LocalProcessBackendConfig() | ||
| client = TrainerClient(backend_config=config) | ||
|
|
||
| def simple_func(): | ||
| return "test" | ||
|
|
||
| trainer = types.CustomTrainer(func=simple_func) | ||
|
|
||
| with pytest.raises(ValueError) as exc_info: | ||
| client.train(trainer=trainer, options=None) | ||
|
|
||
| error_msg = str(exc_info.value) | ||
| assert "Runtime must be provided for LocalProcessBackend" in error_msg | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.