test_server.py•4.94 kB
#!/usr/bin/env python3
"""
Basic test script for Linux MCP Server
Tests the core functionality without requiring actual SSH or browser connections.
"""
import asyncio
import os
import sys
from unittest.mock import Mock, patch
# Add current directory to path for imports
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from linux_mcp_server import (
get_ssh_client, init_browser, close_browser,
execute_command, puppeteer_navigate, puppeteer_screenshot
)
async def test_ssh_connection_mock():
"""Test SSH connection logic (without actual connection)."""
print("🧪 Testing SSH connection logic...")
# Test the environment variable handling
import os
old_user = os.environ.get("SSH_USER")
old_host = os.environ.get("SSH_HOST")
try:
# Set test values
os.environ["SSH_USER"] = "testuser"
os.environ["SSH_HOST"] = "testhost"
# Reload the module to get new env values
import importlib
import linux_mcp_server
importlib.reload(linux_mcp_server)
# Check that the values are loaded
if linux_mcp_server.SSH_USER == "testuser" and linux_mcp_server.SSH_HOST == "testhost":
print("✅ SSH environment variables loaded correctly")
return True
else:
print("❌ SSH environment variables not loaded correctly")
return False
except Exception as e:
print(f"❌ SSH environment test failed: {e}")
return False
finally:
# Restore original values
if old_user:
os.environ["SSH_USER"] = old_user
else:
os.environ.pop("SSH_USER", None)
if old_host:
os.environ["SSH_HOST"] = old_host
else:
os.environ.pop("SSH_HOST", None)
async def test_browser_mock():
"""Test browser-related imports and setup."""
print("🧪 Testing browser setup...")
try:
# Test that pyppeteer can be imported
import pyppeteer
print("✅ Pyppeteer import successful")
# Test that browser globals are initialized
from linux_mcp_server import browser, page, console_logs, network_requests
if browser is None and page is None:
print("✅ Browser globals initialized correctly")
return True
else:
print("❌ Browser globals not initialized correctly")
return False
except ImportError as e:
print(f"❌ Pyppeteer import failed: {e}")
return False
except Exception as e:
print(f"❌ Browser setup test failed: {e}")
return False
async def test_tool_validation():
"""Test tool parameter validation."""
print("🧪 Testing tool parameter validation...")
# Test execute_command with empty parameter
result = await execute_command("")
if "Command parameter is required" in result:
print("✅ execute_command validation works")
else:
print("❌ execute_command validation failed")
return False
# Test puppeteer_navigate with empty parameter
result = await puppeteer_navigate("")
if "URL parameter is required" in result:
print("✅ puppeteer_navigate validation works")
else:
print("❌ puppeteer_navigate validation failed")
return False
# Test puppeteer_screenshot with empty parameter
result = await puppeteer_screenshot("", "", "800", "600")
if "Name parameter is required" in result:
print("✅ puppeteer_screenshot validation works")
else:
print("❌ puppeteer_screenshot validation failed")
return False
return True
async def test_mcp_structure():
"""Test that MCP server is properly initialized."""
print("🧪 Testing MCP structure...")
from linux_mcp_server import mcp
# Check that mcp is a FastMCP instance
if hasattr(mcp, 'name') and mcp.name == "linux-server":
print("✅ MCP server initialized correctly")
return True
else:
print("❌ MCP structure test failed - server not initialized properly")
return False
async def main():
"""Run all tests."""
print("🚀 Starting Linux MCP Server Tests\n")
tests = [
test_mcp_structure,
test_tool_validation,
test_ssh_connection_mock,
test_browser_mock,
]
passed = 0
total = len(tests)
for test in tests:
try:
if await test():
passed += 1
print()
except Exception as e:
print(f"❌ Test {test.__name__} crashed: {e}\n")
print(f"📊 Test Results: {passed}/{total} tests passed")
if passed == total:
print("🎉 All tests passed!")
return 0
else:
print("⚠️ Some tests failed. Please check the implementation.")
return 1
if __name__ == "__main__":
exit_code = asyncio.run(main())
sys.exit(exit_code)