|
| 1 | +import os |
| 2 | +import tempfile |
| 3 | + |
| 4 | +from unittest.mock import MagicMock, mock_open, patch |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from flagscale.runner.elastic.diagnostic import error_types, generate_diagnostic_report |
| 9 | + |
| 10 | + |
| 11 | +class TestDiagnostic: |
| 12 | + """Test cases for diagnostic module""" |
| 13 | + |
| 14 | + @pytest.fixture |
| 15 | + def mock_config(self): |
| 16 | + """Mock config object""" |
| 17 | + return MagicMock() |
| 18 | + |
| 19 | + @pytest.fixture |
| 20 | + def sample_log_content(self): |
| 21 | + """Sample log content with various errors""" |
| 22 | + return """ |
| 23 | + [INFO] Starting training... |
| 24 | + [ERROR] CUDA out of memory |
| 25 | + Traceback (most recent call last): |
| 26 | + File "train.py", line 100, in <module> |
| 27 | + model.forward() |
| 28 | + torch.distributed.elastic.rendezvous.api.RendezvousConnectionError: Connection failed |
| 29 | + OutOfMemoryError: GPU memory exhausted |
| 30 | + [ERROR] Training failed |
| 31 | + """ |
| 32 | + |
| 33 | + def test_error_types_dict_exists(self): |
| 34 | + """Test that error_types dictionary is properly defined""" |
| 35 | + assert isinstance(error_types, dict) |
| 36 | + assert len(error_types) > 0 |
| 37 | + |
| 38 | + expected_keys = ['out of memory', 'rendezvous', 'traceback', 'error', 'cuda'] |
| 39 | + for key in expected_keys: |
| 40 | + assert key in error_types |
| 41 | + |
| 42 | + def test_generate_diagnostic_report_empty_file(self, mock_config): |
| 43 | + with tempfile.NamedTemporaryFile(mode='w', delete=False) as f: |
| 44 | + f.write("") |
| 45 | + temp_path = f.name |
| 46 | + |
| 47 | + try: |
| 48 | + report = generate_diagnostic_report( |
| 49 | + mock_config, "localhost", 0, temp_path, return_content=True |
| 50 | + ) |
| 51 | + |
| 52 | + assert "Diagnostic Report for localhost (node 0)" in report |
| 53 | + assert "Log file is empty" in report |
| 54 | + finally: |
| 55 | + os.unlink(temp_path) |
| 56 | + |
| 57 | + def test_generate_diagnostic_report_with_errors(self, mock_config, sample_log_content): |
| 58 | + with tempfile.NamedTemporaryFile(mode='w', delete=False) as f: |
| 59 | + f.write(sample_log_content) |
| 60 | + temp_path = f.name |
| 61 | + |
| 62 | + try: |
| 63 | + report = generate_diagnostic_report( |
| 64 | + mock_config, "localhost", 0, temp_path, return_content=True |
| 65 | + ) |
| 66 | + |
| 67 | + assert "Diagnostic Report for localhost (node 0)" in report |
| 68 | + assert "OutOfMemoryError" in report |
| 69 | + assert "RendezvousConnectionError" in report |
| 70 | + assert "CodeError" in report |
| 71 | + assert "GeneralError" in report |
| 72 | + finally: |
| 73 | + os.unlink(temp_path) |
| 74 | + |
| 75 | + def test_generate_diagnostic_report_nonexistent_file(self, mock_config): |
| 76 | + report = generate_diagnostic_report( |
| 77 | + mock_config, "localhost", 0, "/nonexistent/file.log", return_content=True |
| 78 | + ) |
| 79 | + |
| 80 | + assert "Diagnostic Report for localhost (node 0)" in report |
| 81 | + assert "Log file is empty or does not exist" in report |
| 82 | + |
| 83 | + def test_generate_diagnostic_report_no_errors(self, mock_config): |
| 84 | + content = """ |
| 85 | + [INFO] Training started successfully |
| 86 | + [INFO] Epoch 1/100 completed |
| 87 | + [INFO] Training finished successfully |
| 88 | + """ |
| 89 | + |
| 90 | + with tempfile.NamedTemporaryFile(mode='w', delete=False) as f: |
| 91 | + f.write(content) |
| 92 | + temp_path = f.name |
| 93 | + |
| 94 | + try: |
| 95 | + report = generate_diagnostic_report( |
| 96 | + mock_config, "localhost", 0, temp_path, return_content=True |
| 97 | + ) |
| 98 | + |
| 99 | + assert "Diagnostic Report for localhost (node 0)" in report |
| 100 | + assert "No errors or unknown error detected" in report |
| 101 | + finally: |
| 102 | + os.unlink(temp_path) |
| 103 | + |
| 104 | + def test_generate_diagnostic_report_file_output(self, mock_config, sample_log_content): |
| 105 | + with tempfile.NamedTemporaryFile(mode='w', delete=False) as f: |
| 106 | + f.write(sample_log_content) |
| 107 | + temp_path = f.name |
| 108 | + |
| 109 | + try: |
| 110 | + # Test file output mode |
| 111 | + result_path = generate_diagnostic_report( |
| 112 | + mock_config, "localhost", 0, temp_path, return_content=False |
| 113 | + ) |
| 114 | + |
| 115 | + assert result_path is not None |
| 116 | + assert "diagnostic" in result_path or result_path.endswith('.txt') |
| 117 | + finally: |
| 118 | + os.unlink(temp_path) |
| 119 | + |
| 120 | + @patch('flagscale.runner.elastic.diagnostic.logger') |
| 121 | + def test_generate_diagnostic_report_read_error(self, mock_logger, mock_config): |
| 122 | + """Test diagnostic report generation with file read error""" |
| 123 | + with patch('builtins.open', side_effect=PermissionError("Access denied")): |
| 124 | + report = generate_diagnostic_report( |
| 125 | + mock_config, "localhost", 0, "/some/file.log", return_content=True |
| 126 | + ) |
| 127 | + |
| 128 | + assert "Error reading log file" in report |
| 129 | + mock_logger.error.assert_called() |
| 130 | + |
| 131 | + def test_error_detection_case_insensitive(self, mock_config): |
| 132 | + content = """ |
| 133 | + CUDA OUT OF MEMORY ERROR occurred |
| 134 | + TRACEBACK: Failed to execute |
| 135 | + RENDEZVOUS connection failed |
| 136 | + """ |
| 137 | + |
| 138 | + with tempfile.NamedTemporaryFile(mode='w', delete=False) as f: |
| 139 | + f.write(content) |
| 140 | + temp_path = f.name |
| 141 | + |
| 142 | + try: |
| 143 | + report = generate_diagnostic_report( |
| 144 | + mock_config, "localhost", 0, temp_path, return_content=True |
| 145 | + ) |
| 146 | + |
| 147 | + # Should detect errors regardless of case |
| 148 | + assert "OutOfMemoryError" in report |
| 149 | + assert "CodeError" in report |
| 150 | + assert "RendezvousError" in report |
| 151 | + finally: |
| 152 | + os.unlink(temp_path) |
| 153 | + |
| 154 | + def test_multiple_error_types_detection(self, mock_config): |
| 155 | + content = """ |
| 156 | + CUDA error: out of memory |
| 157 | + ImportError: module not found |
| 158 | + Permission denied: cannot access file |
| 159 | + Connection timeout occurred |
| 160 | + Process was killed |
| 161 | + """ |
| 162 | + |
| 163 | + with tempfile.NamedTemporaryFile(mode='w', delete=False) as f: |
| 164 | + f.write(content) |
| 165 | + temp_path = f.name |
| 166 | + |
| 167 | + try: |
| 168 | + report = generate_diagnostic_report( |
| 169 | + mock_config, "localhost", 0, temp_path, return_content=True |
| 170 | + ) |
| 171 | + |
| 172 | + assert "CUDAError" in report |
| 173 | + assert "ImportError" in report |
| 174 | + assert "PermissionError" in report |
| 175 | + assert "TimeoutError" in report |
| 176 | + assert "ProcessKilled" in report |
| 177 | + finally: |
| 178 | + os.unlink(temp_path) |
0 commit comments