Skip to content

Commit 7ebe7e6

Browse files
committed
task(RHOAIENG-30722):Update code coverage
1 parent b52e9bc commit 7ebe7e6

File tree

1 file changed

+127
-0
lines changed

1 file changed

+127
-0
lines changed

src/codeflare_sdk/ray/cluster/test_cluster.py

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
from unittest.mock import MagicMock
3838
from kubernetes import client
3939
import yaml
40+
import pytest
4041
import filecmp
4142
import os
4243

@@ -770,6 +771,132 @@ def custom_side_effect(group, version, namespace, plural, **kwargs):
770771
assert result.dashboard == rc_dashboard
771772

772773

774+
def test_throw_for_no_raycluster_crd_errors(mocker):
775+
"""Test RayCluster CRD error handling"""
776+
from kubernetes.client.rest import ApiException
777+
778+
mocker.patch("kubernetes.config.load_kube_config", return_value="ignore")
779+
780+
# Test 404 error - CRD not found
781+
mock_api_404 = MagicMock()
782+
mock_api_404.list_namespaced_custom_object.side_effect = ApiException(status=404)
783+
mocker.patch("kubernetes.client.CustomObjectsApi", return_value=mock_api_404)
784+
785+
cluster = create_cluster(mocker)
786+
with pytest.raises(RuntimeError, match="RayCluster CustomResourceDefinition unavailable"):
787+
cluster._throw_for_no_raycluster()
788+
789+
# Test other API error
790+
mock_api_500 = MagicMock()
791+
mock_api_500.list_namespaced_custom_object.side_effect = ApiException(status=500)
792+
mocker.patch("kubernetes.client.CustomObjectsApi", return_value=mock_api_500)
793+
794+
cluster2 = create_cluster(mocker)
795+
with pytest.raises(RuntimeError, match="Failed to get RayCluster CustomResourceDefinition"):
796+
cluster2._throw_for_no_raycluster()
797+
798+
799+
def test_cluster_apply_attribute_error_handling(mocker):
800+
"""Test AttributeError handling when DynamicClient fails"""
801+
mocker.patch("kubernetes.config.load_kube_config", return_value="ignore")
802+
mocker.patch("codeflare_sdk.ray.cluster.cluster.Cluster._throw_for_no_raycluster")
803+
mocker.patch(
804+
"kubernetes.client.CustomObjectsApi.list_namespaced_custom_object",
805+
return_value=get_local_queue("kueue.x-k8s.io", "v1beta1", "ns", "localqueues"),
806+
)
807+
808+
# Mock get_dynamic_client to raise AttributeError
809+
def raise_attribute_error():
810+
raise AttributeError("DynamicClient initialization failed")
811+
812+
mocker.patch(
813+
"codeflare_sdk.ray.cluster.cluster.Cluster.get_dynamic_client",
814+
side_effect=raise_attribute_error
815+
)
816+
817+
cluster = create_cluster(mocker)
818+
819+
with pytest.raises(RuntimeError, match="Failed to initialize DynamicClient"):
820+
cluster.apply()
821+
822+
823+
def test_cluster_namespace_handling(mocker, capsys):
824+
"""Test namespace validation in create_resource"""
825+
mocker.patch("kubernetes.config.load_kube_config", return_value="ignore")
826+
mocker.patch(
827+
"kubernetes.client.CustomObjectsApi.list_namespaced_custom_object",
828+
return_value=get_local_queue("kueue.x-k8s.io", "v1beta1", "ns", "localqueues"),
829+
)
830+
831+
# Test with None namespace that gets set
832+
mocker.patch(
833+
"codeflare_sdk.ray.cluster.cluster.get_current_namespace",
834+
return_value=None
835+
)
836+
837+
config = ClusterConfiguration(
838+
name="test-cluster-ns",
839+
namespace=None, # Will trigger namespace check
840+
num_workers=1,
841+
worker_cpu_requests=1,
842+
worker_cpu_limits=1,
843+
worker_memory_requests=2,
844+
worker_memory_limits=2,
845+
)
846+
847+
cluster = Cluster(config)
848+
captured = capsys.readouterr()
849+
# Verify the warning message was printed
850+
assert "Please specify with namespace=<your_current_namespace>" in captured.out
851+
assert cluster.config.namespace is None
852+
853+
854+
def test_cluster_up_with_write_to_file(mocker):
855+
"""Test cluster.up() method with write_to_file enabled"""
856+
mocker.patch("kubernetes.config.load_kube_config", return_value="ignore")
857+
mocker.patch("codeflare_sdk.ray.cluster.cluster.Cluster._throw_for_no_raycluster")
858+
mocker.patch("codeflare_sdk.ray.cluster.cluster.config_check")
859+
mocker.patch(
860+
"kubernetes.client.CustomObjectsApi.list_namespaced_custom_object",
861+
return_value=get_local_queue("kueue.x-k8s.io", "v1beta1", "ns", "localqueues"),
862+
)
863+
864+
# Mock the create_namespaced_custom_object
865+
mock_create = MagicMock()
866+
mocker.patch(
867+
"kubernetes.client.CustomObjectsApi.create_namespaced_custom_object",
868+
return_value=mock_create
869+
)
870+
871+
# Create cluster with write_to_file=True
872+
config = ClusterConfiguration(
873+
name="test-cluster-up",
874+
namespace="ns",
875+
num_workers=1,
876+
worker_cpu_requests=1,
877+
worker_cpu_limits=1,
878+
worker_memory_requests=2,
879+
worker_memory_limits=2,
880+
write_to_file=True,
881+
appwrapper=True,
882+
)
883+
884+
cluster = Cluster(config)
885+
886+
# Mock file reading
887+
import tempfile
888+
import os
889+
with tempfile.NamedTemporaryFile(mode='w', suffix='.yaml', delete=False) as f:
890+
f.write('apiVersion: v1\nkind: ConfigMap\nmetadata:\n name: test')
891+
temp_file = f.name
892+
893+
try:
894+
cluster.resource_yaml = temp_file
895+
cluster.up()
896+
finally:
897+
os.unlink(temp_file)
898+
899+
773900
# Make sure to always keep this function last
774901
def test_cleanup():
775902
os.remove(f"{aw_dir}test-all-params.yaml")

0 commit comments

Comments
 (0)