|
| 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 kube_api_helpers module. |
| 17 | +""" |
| 18 | + |
| 19 | +import pytest |
| 20 | +from unittest.mock import patch |
| 21 | +from kubernetes import client |
| 22 | + |
| 23 | +from codeflare_sdk.common.kubernetes_cluster.kube_api_helpers import ( |
| 24 | + _format_api_error_body, |
| 25 | + _kube_api_error_handling, |
| 26 | +) |
| 27 | + |
| 28 | + |
| 29 | +class TestFormatApiErrorBody: |
| 30 | + """Tests for _format_api_error_body.""" |
| 31 | + |
| 32 | + def test_empty_body_returns_empty_string(self): |
| 33 | + assert _format_api_error_body(None) == "" |
| 34 | + assert _format_api_error_body("") == "" |
| 35 | + |
| 36 | + def test_valid_json_with_message_returns_details_line(self): |
| 37 | + body = '{"message": "Something went wrong"}' |
| 38 | + assert _format_api_error_body(body) == "Details: Something went wrong" |
| 39 | + |
| 40 | + def test_bytes_body_decoded_and_parsed(self): |
| 41 | + body = b'{"message": "Bytes message"}' |
| 42 | + assert _format_api_error_body(body) == "Details: Bytes message" |
| 43 | + |
| 44 | + def test_cluster_name_validation_returns_friendly_message(self): |
| 45 | + body = '{"message": "Invalid value: metadata.name must be lowercase RFC 1123 subdomain"}' |
| 46 | + result = _format_api_error_body(body) |
| 47 | + assert "Cluster name is invalid" in result |
| 48 | + assert "lowercase" in result and "my-cluster" in result |
| 49 | + |
| 50 | + def test_json_without_message_returns_empty(self): |
| 51 | + assert _format_api_error_body('{"code": 404}') == "" |
| 52 | + |
| 53 | + def test_invalid_json_returns_response_line(self): |
| 54 | + result = _format_api_error_body("not json") |
| 55 | + assert result.startswith("Response:") |
| 56 | + assert "not json" in result |
| 57 | + |
| 58 | + |
| 59 | +class TestKubeApiErrorHandling: |
| 60 | + """Tests for _kube_api_error_handling.""" |
| 61 | + |
| 62 | + @patch("builtins.print") |
| 63 | + def test_422_cluster_name_error_prints_friendly_detail_only(self, mock_print): |
| 64 | + api_exc = client.ApiException(status=422, reason="Unprocessable Entity") |
| 65 | + api_exc.body = b'{"message": "Invalid value: metadata.name lowercase RFC 1123"}' |
| 66 | + _kube_api_error_handling(api_exc) |
| 67 | + # Should print the friendly cluster-name message, not the generic Unprocessable Entity line |
| 68 | + call_args = [str(c) for c in mock_print.call_args_list] |
| 69 | + assert any("Cluster name is invalid" in c for c in call_args) |
| 70 | + assert not any( |
| 71 | + "something in your cluster configuration is invalid" in c for c in call_args |
| 72 | + ) |
| 73 | + |
| 74 | + @patch("builtins.print") |
| 75 | + def test_404_with_body_prints_message_and_detail(self, mock_print): |
| 76 | + api_exc = client.ApiException(status=404, reason="Not Found") |
| 77 | + api_exc.body = '{"message": "RayCluster xyz not found"}' |
| 78 | + _kube_api_error_handling(api_exc) |
| 79 | + call_args = [str(c) for c in mock_print.call_args_list] |
| 80 | + assert any("requested resource could not be located" in c for c in call_args) |
| 81 | + assert any("Details: RayCluster xyz not found" in c for c in call_args) |
| 82 | + |
| 83 | + @patch("builtins.print") |
| 84 | + def test_403_prints_forbidden_message(self, mock_print): |
| 85 | + """403 uses status-code lookup so it gets the right message (not fallback).""" |
| 86 | + api_exc = client.ApiException(status=403, reason="Forbidden") |
| 87 | + api_exc.body = None |
| 88 | + _kube_api_error_handling(api_exc) |
| 89 | + call_args = [str(c) for c in mock_print.call_args_list] |
| 90 | + assert any("Access denied:" in c for c in call_args) |
| 91 | + |
| 92 | + @patch("builtins.print") |
| 93 | + def test_api_exception_with_no_body_prints_reason_and_status(self, mock_print): |
| 94 | + api_exc = client.ApiException(status=500, reason="Internal Server Error") |
| 95 | + api_exc.body = None |
| 96 | + _kube_api_error_handling(api_exc) |
| 97 | + call_args = [str(c) for c in mock_print.call_args_list] |
| 98 | + assert any("Reason: Internal Server Error" in c for c in call_args) |
| 99 | + assert any("HTTP 500" in c for c in call_args) |
0 commit comments