|
| 1 | +# Copyright 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 | +"""Unit tests for log monitoring and failure pattern extraction.""" |
| 16 | + |
| 17 | +from kubeflow_mcp.trainer.api.monitoring import _extract_failure_hint |
| 18 | + |
| 19 | + |
| 20 | +def test_extract_failure_hint_openshift_pip_error(): |
| 21 | + # Test exact PermissionError trace |
| 22 | + logs = ( |
| 23 | + "Installing collected packages: torch\n" |
| 24 | + "PermissionError: [Errno 13] Permission denied: '/.local'\n" |
| 25 | + "ERROR: Job failed" |
| 26 | + ) |
| 27 | + hint = _extract_failure_hint(logs) |
| 28 | + assert hint is not None |
| 29 | + assert hint["category"] == "OPENSHIFT_PIP_ERROR" |
| 30 | + assert "On OpenShift under a restricted SCC" in hint["suggestion"] |
| 31 | + assert "Do NOT use the 'packages' parameter" in hint["suggestion"] |
| 32 | + |
| 33 | + # Test generic permission denied on /.local |
| 34 | + logs_generic = "Permission denied: '/.local/bin/pip'" |
| 35 | + hint_generic = _extract_failure_hint(logs_generic) |
| 36 | + assert hint_generic is not None |
| 37 | + assert hint_generic["category"] == "OPENSHIFT_PIP_ERROR" |
| 38 | + |
| 39 | + |
| 40 | +def test_extract_failure_hint_generic_permission_error(): |
| 41 | + # Test a generic permission error does not trigger OpenShift pip error |
| 42 | + logs = "PermissionError: [Errno 13] Permission denied: '/workspace/data.csv'" |
| 43 | + hint = _extract_failure_hint(logs) |
| 44 | + assert hint is not None |
| 45 | + assert hint["category"] == "PERMISSION_ERROR" |
| 46 | + assert "Check service account permissions" in hint["suggestion"] |
| 47 | + |
| 48 | + |
| 49 | +def test_extract_failure_hint_other_patterns(): |
| 50 | + # Test CUDA OOM |
| 51 | + oom_logs = "RuntimeError: CUDA out of memory. Tried to allocate 2.00 GiB" |
| 52 | + oom_hint = _extract_failure_hint(oom_logs) |
| 53 | + assert oom_hint is not None |
| 54 | + assert oom_hint["category"] == "OOM" |
| 55 | + |
| 56 | + # Test Missing Module |
| 57 | + module_logs = "ModuleNotFoundError: No module named 'peft'" |
| 58 | + module_hint = _extract_failure_hint(module_logs) |
| 59 | + assert module_hint is not None |
| 60 | + assert module_hint["category"] == "MISSING_MODULE" |
| 61 | + |
| 62 | + # Test no match |
| 63 | + clean_logs = "Training completed successfully. Epoch 5/5 finished." |
| 64 | + assert _extract_failure_hint(clean_logs) is None |
0 commit comments