|
| 1 | +"""Tests for MCP backup system integration. |
| 2 | +
|
| 3 | +This module contains integration tests for the backup system with existing |
| 4 | +Hatch infrastructure and end-to-end workflows. |
| 5 | +""" |
| 6 | + |
| 7 | +import unittest |
| 8 | +import tempfile |
| 9 | +import shutil |
| 10 | +import json |
| 11 | +import time |
| 12 | +from pathlib import Path |
| 13 | +from unittest.mock import Mock, patch |
| 14 | + |
| 15 | +from wobble.decorators import integration_test, slow_test, regression_test |
| 16 | +from test_data_utils import MCPBackupTestDataLoader |
| 17 | + |
| 18 | +from hatch.mcp.backup import ( |
| 19 | + MCPHostConfigBackupManager, |
| 20 | + BackupAwareOperation, |
| 21 | + BackupInfo, |
| 22 | + BackupResult |
| 23 | +) |
| 24 | + |
| 25 | + |
| 26 | +class TestMCPBackupIntegration(unittest.TestCase): |
| 27 | + """Test backup system integration with existing Hatch infrastructure.""" |
| 28 | + |
| 29 | + def setUp(self): |
| 30 | + """Set up integration test environment.""" |
| 31 | + self.temp_dir = Path(tempfile.mkdtemp(prefix="test_integration_")) |
| 32 | + self.backup_manager = MCPHostConfigBackupManager(backup_root=self.temp_dir / "backups") |
| 33 | + self.test_data = MCPBackupTestDataLoader() |
| 34 | + |
| 35 | + # Create test configuration files |
| 36 | + self.config_dir = self.temp_dir / "configs" |
| 37 | + self.config_dir.mkdir(parents=True) |
| 38 | + |
| 39 | + self.test_configs = {} |
| 40 | + for hostname in ['claude-desktop', 'claude-code', 'vscode', 'cursor']: |
| 41 | + config_data = self.test_data.load_host_agnostic_config("simple_server") |
| 42 | + config_file = self.config_dir / f"{hostname}_config.json" |
| 43 | + with open(config_file, 'w') as f: |
| 44 | + json.dump(config_data, f, indent=2) |
| 45 | + self.test_configs[hostname] = config_file |
| 46 | + |
| 47 | + def tearDown(self): |
| 48 | + """Clean up integration test environment.""" |
| 49 | + shutil.rmtree(self.temp_dir, ignore_errors=True) |
| 50 | + |
| 51 | + @integration_test(scope="component") |
| 52 | + def test_complete_backup_restore_cycle(self): |
| 53 | + """Test complete backup creation and restoration cycle.""" |
| 54 | + hostname = 'claude-desktop' |
| 55 | + config_file = self.test_configs[hostname] |
| 56 | + |
| 57 | + # Create backup |
| 58 | + backup_result = self.backup_manager.create_backup(config_file, hostname) |
| 59 | + self.assertTrue(backup_result.success) |
| 60 | + |
| 61 | + # Modify original file |
| 62 | + modified_data = self.test_data.load_host_agnostic_config("complex_server") |
| 63 | + with open(config_file, 'w') as f: |
| 64 | + json.dump(modified_data, f) |
| 65 | + |
| 66 | + # Verify file was modified |
| 67 | + with open(config_file) as f: |
| 68 | + current_data = json.load(f) |
| 69 | + self.assertEqual(current_data, modified_data) |
| 70 | + |
| 71 | + # Restore from backup (placeholder - actual restore would need host config paths) |
| 72 | + restore_success = self.backup_manager.restore_backup(hostname) |
| 73 | + self.assertTrue(restore_success) # Currently returns True as placeholder |
| 74 | + |
| 75 | + @integration_test(scope="component") |
| 76 | + def test_multi_host_backup_management(self): |
| 77 | + """Test backup management across multiple hosts.""" |
| 78 | + # Create backups for multiple hosts |
| 79 | + results = {} |
| 80 | + for hostname, config_file in self.test_configs.items(): |
| 81 | + results[hostname] = self.backup_manager.create_backup(config_file, hostname) |
| 82 | + self.assertTrue(results[hostname].success) |
| 83 | + |
| 84 | + # Verify separate backup directories |
| 85 | + for hostname in self.test_configs.keys(): |
| 86 | + backups = self.backup_manager.list_backups(hostname) |
| 87 | + self.assertEqual(len(backups), 1) |
| 88 | + |
| 89 | + # Verify backup isolation |
| 90 | + backup_dir = backups[0].file_path.parent |
| 91 | + self.assertEqual(backup_dir.name, hostname) |
| 92 | + |
| 93 | + # Verify no cross-contamination |
| 94 | + for other_hostname in self.test_configs.keys(): |
| 95 | + if other_hostname != hostname: |
| 96 | + other_backups = self.backup_manager.list_backups(other_hostname) |
| 97 | + self.assertNotEqual( |
| 98 | + backups[0].file_path.parent, |
| 99 | + other_backups[0].file_path.parent |
| 100 | + ) |
| 101 | + |
| 102 | + @integration_test(scope="end_to_end") |
| 103 | + def test_backup_with_configuration_update_workflow(self): |
| 104 | + """Test backup integration with configuration update operations.""" |
| 105 | + hostname = 'vscode' |
| 106 | + config_file = self.test_configs[hostname] |
| 107 | + |
| 108 | + # Simulate configuration update with backup |
| 109 | + original_data = self.test_data.load_host_agnostic_config("simple_server") |
| 110 | + updated_data = self.test_data.load_host_agnostic_config("complex_server") |
| 111 | + |
| 112 | + # Ensure original data is in file |
| 113 | + with open(config_file, 'w') as f: |
| 114 | + json.dump(original_data, f) |
| 115 | + |
| 116 | + # Simulate update operation with backup |
| 117 | + backup_result = self.backup_manager.create_backup(config_file, hostname) |
| 118 | + self.assertTrue(backup_result.success) |
| 119 | + |
| 120 | + # Update configuration |
| 121 | + with open(config_file, 'w') as f: |
| 122 | + json.dump(updated_data, f) |
| 123 | + |
| 124 | + # Verify backup contains original data |
| 125 | + backups = self.backup_manager.list_backups(hostname) |
| 126 | + self.assertEqual(len(backups), 1) |
| 127 | + |
| 128 | + with open(backups[0].file_path) as f: |
| 129 | + backup_data = json.load(f) |
| 130 | + self.assertEqual(backup_data, original_data) |
| 131 | + |
| 132 | + # Verify current file has updated data |
| 133 | + with open(config_file) as f: |
| 134 | + current_data = json.load(f) |
| 135 | + self.assertEqual(current_data, updated_data) |
| 136 | + |
| 137 | + @integration_test(scope="service") |
| 138 | + def test_backup_system_with_existing_test_utilities(self): |
| 139 | + """Test backup system integration with existing test utilities.""" |
| 140 | + # Use existing TestDataLoader patterns |
| 141 | + test_config = self.test_data.load_host_agnostic_config("complex_server") |
| 142 | + |
| 143 | + # Test backup creation with complex configuration |
| 144 | + config_path = self.temp_dir / "complex_config.json" |
| 145 | + with open(config_path, 'w') as f: |
| 146 | + json.dump(test_config, f) |
| 147 | + |
| 148 | + result = self.backup_manager.create_backup(config_path, "lmstudio") |
| 149 | + self.assertTrue(result.success) |
| 150 | + |
| 151 | + # Verify integration with existing test data patterns |
| 152 | + self.assertIsInstance(test_config, dict) |
| 153 | + self.assertIn("servers", test_config) |
| 154 | + |
| 155 | + # Verify backup content matches test data |
| 156 | + with open(result.backup_path) as f: |
| 157 | + backup_content = json.load(f) |
| 158 | + self.assertEqual(backup_content, test_config) |
| 159 | + |
| 160 | + @integration_test(scope="component") |
| 161 | + def test_backup_aware_operation_workflow(self): |
| 162 | + """Test backup-aware operation following environment manager patterns.""" |
| 163 | + hostname = 'cursor' |
| 164 | + config_file = self.test_configs[hostname] |
| 165 | + |
| 166 | + # Test backup-aware operation following existing patterns |
| 167 | + operation = BackupAwareOperation(self.backup_manager) |
| 168 | + |
| 169 | + # Simulate environment manager update workflow |
| 170 | + backup_result = operation.prepare_backup(config_file, hostname, no_backup=False) |
| 171 | + self.assertTrue(backup_result.success) |
| 172 | + |
| 173 | + # Verify backup was created following existing patterns |
| 174 | + backups = self.backup_manager.list_backups(hostname) |
| 175 | + self.assertEqual(len(backups), 1) |
| 176 | + self.assertEqual(backups[0].hostname, hostname) |
| 177 | + |
| 178 | + # Test rollback capability |
| 179 | + rollback_success = operation.rollback_on_failure(backup_result, config_file, hostname) |
| 180 | + self.assertTrue(rollback_success) |
| 181 | + |
| 182 | + |
| 183 | +class TestMCPBackupPerformance(unittest.TestCase): |
| 184 | + """Test backup system performance characteristics.""" |
| 185 | + |
| 186 | + def setUp(self): |
| 187 | + """Set up performance test environment.""" |
| 188 | + self.temp_dir = Path(tempfile.mkdtemp(prefix="test_performance_")) |
| 189 | + self.backup_manager = MCPHostConfigBackupManager(backup_root=self.temp_dir / "backups") |
| 190 | + self.test_data = MCPBackupTestDataLoader() |
| 191 | + |
| 192 | + def tearDown(self): |
| 193 | + """Clean up performance test environment.""" |
| 194 | + shutil.rmtree(self.temp_dir, ignore_errors=True) |
| 195 | + |
| 196 | + @slow_test |
| 197 | + @regression_test |
| 198 | + def test_backup_performance_large_config(self): |
| 199 | + """Test backup performance with larger configuration files.""" |
| 200 | + # Create large host-agnostic configuration |
| 201 | + large_config = {"servers": {}} |
| 202 | + for i in range(1000): |
| 203 | + large_config["servers"][f"server_{i}"] = { |
| 204 | + "command": f"python_{i}", |
| 205 | + "args": [f"arg_{j}" for j in range(10)] |
| 206 | + } |
| 207 | + |
| 208 | + config_file = self.temp_dir / "large_config.json" |
| 209 | + with open(config_file, 'w') as f: |
| 210 | + json.dump(large_config, f) |
| 211 | + |
| 212 | + start_time = time.time() |
| 213 | + result = self.backup_manager.create_backup(config_file, "gemini") |
| 214 | + duration = time.time() - start_time |
| 215 | + |
| 216 | + self.assertTrue(result.success) |
| 217 | + self.assertLess(duration, 1.0) # Should complete within 1 second |
| 218 | + |
| 219 | + @regression_test |
| 220 | + def test_pydantic_validation_performance(self): |
| 221 | + """Test Pydantic model validation performance.""" |
| 222 | + hostname = "claude-desktop" |
| 223 | + config_data = self.test_data.load_host_agnostic_config("simple_server") |
| 224 | + config_file = self.temp_dir / "test_config.json" |
| 225 | + |
| 226 | + with open(config_file, 'w') as f: |
| 227 | + json.dump(config_data, f) |
| 228 | + |
| 229 | + start_time = time.time() |
| 230 | + |
| 231 | + # Create backup (includes Pydantic validation) |
| 232 | + result = self.backup_manager.create_backup(config_file, hostname) |
| 233 | + |
| 234 | + # List backups (includes Pydantic model creation) |
| 235 | + backups = self.backup_manager.list_backups(hostname) |
| 236 | + |
| 237 | + duration = time.time() - start_time |
| 238 | + |
| 239 | + self.assertTrue(result.success) |
| 240 | + self.assertEqual(len(backups), 1) |
| 241 | + self.assertLess(duration, 0.1) # Pydantic operations should be fast |
| 242 | + |
| 243 | + @regression_test |
| 244 | + def test_concurrent_backup_operations(self): |
| 245 | + """Test concurrent backup operations for different hosts.""" |
| 246 | + import threading |
| 247 | + |
| 248 | + results = {} |
| 249 | + config_files = {} |
| 250 | + |
| 251 | + # Create test configurations for different hosts |
| 252 | + for hostname in ['claude-desktop', 'vscode', 'cursor', 'lmstudio']: |
| 253 | + config_data = self.test_data.load_host_agnostic_config("simple_server") |
| 254 | + config_file = self.temp_dir / f"{hostname}_config.json" |
| 255 | + with open(config_file, 'w') as f: |
| 256 | + json.dump(config_data, f) |
| 257 | + config_files[hostname] = config_file |
| 258 | + |
| 259 | + def create_backup_thread(hostname, config_file): |
| 260 | + results[hostname] = self.backup_manager.create_backup(config_file, hostname) |
| 261 | + |
| 262 | + # Start concurrent backup operations |
| 263 | + threads = [] |
| 264 | + for hostname, config_file in config_files.items(): |
| 265 | + thread = threading.Thread(target=create_backup_thread, args=(hostname, config_file)) |
| 266 | + threads.append(thread) |
| 267 | + thread.start() |
| 268 | + |
| 269 | + # Wait for all threads to complete |
| 270 | + for thread in threads: |
| 271 | + thread.join(timeout=5.0) |
| 272 | + |
| 273 | + # Verify all operations succeeded |
| 274 | + for hostname in config_files.keys(): |
| 275 | + self.assertIn(hostname, results) |
| 276 | + self.assertTrue(results[hostname].success) |
| 277 | + |
| 278 | + @regression_test |
| 279 | + def test_backup_list_performance_many_backups(self): |
| 280 | + """Test backup listing performance with many backup files.""" |
| 281 | + hostname = "claude-code" |
| 282 | + config_data = self.test_data.load_host_agnostic_config("simple_server") |
| 283 | + config_file = self.temp_dir / "test_config.json" |
| 284 | + |
| 285 | + with open(config_file, 'w') as f: |
| 286 | + json.dump(config_data, f) |
| 287 | + |
| 288 | + # Create many backups |
| 289 | + for i in range(50): |
| 290 | + result = self.backup_manager.create_backup(config_file, hostname) |
| 291 | + self.assertTrue(result.success) |
| 292 | + |
| 293 | + # Test listing performance |
| 294 | + start_time = time.time() |
| 295 | + backups = self.backup_manager.list_backups(hostname) |
| 296 | + duration = time.time() - start_time |
| 297 | + |
| 298 | + self.assertEqual(len(backups), 50) |
| 299 | + self.assertLess(duration, 0.1) # Should be fast even with many backups |
| 300 | + |
| 301 | + # Verify all backups are valid Pydantic models |
| 302 | + for backup in backups: |
| 303 | + self.assertIsInstance(backup, BackupInfo) |
| 304 | + self.assertEqual(backup.hostname, hostname) |
| 305 | + |
| 306 | + |
| 307 | +if __name__ == '__main__': |
| 308 | + unittest.main() |
0 commit comments