-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathunit_tests.py
More file actions
executable file
·318 lines (271 loc) · 9.88 KB
/
Copy pathunit_tests.py
File metadata and controls
executable file
·318 lines (271 loc) · 9.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#!/usr/bin/env python3
"""
Unit tests for MCP Ansible Automation Platform server components
Test individual functions and classes without full MCP protocol
"""
import asyncio
import json
import os
import sys
from typing import Dict, Any
from unittest.mock import AsyncMock, MagicMock, patch
import logging
# Setup logging
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
# Import our modules
from aap_client import AAPClient, AAPConfig, JobTemplate, JobLaunch
class UnitTestRunner:
"""Run unit tests for MCP server components"""
def __init__(self):
self.passed = 0
self.failed = 0
self.tests = []
def test(self, description: str):
"""Decorator for test functions"""
def decorator(func):
self.tests.append((description, func))
return func
return decorator
async def run_all_tests(self):
"""Run all registered tests"""
print("🧪 Running Unit Tests for MCP AAP Server")
print("=" * 50)
for description, test_func in self.tests:
print(f"\n🔍 Testing: {description}")
try:
if asyncio.iscoroutinefunction(test_func):
await test_func()
else:
test_func()
print(f"✅ PASSED: {description}")
self.passed += 1
except Exception as e:
print(f"❌ FAILED: {description}")
print(f" Error: {str(e)}")
import traceback
traceback.print_exc()
self.failed += 1
print(f"\n" + "=" * 50)
print(f"📊 Test Results: {self.passed} passed, {self.failed} failed")
return self.failed == 0
# Create test runner instance
runner = UnitTestRunner()
@runner.test("AAPConfig Creation")
def test_aap_config():
"""Test AAPConfig model creation"""
config = AAPConfig(
url="https://test.example.com",
token="test-token",
project_id="123"
)
assert config.url == "https://test.example.com"
assert config.token == "test-token"
assert config.project_id == "123"
assert config.verify_ssl is True
assert config.timeout == 30
@runner.test("JobTemplate Model")
def test_job_template():
"""Test JobTemplate model creation"""
template = JobTemplate(
id=1,
name="Test Template",
description="Test Description",
project=5,
playbook="test.yml",
survey_enabled=True
)
assert template.id == 1
assert template.name == "Test Template"
assert template.playbook == "test.yml"
assert template.survey_enabled is True
@runner.test("JobLaunch Model")
def test_job_launch():
"""Test JobLaunch model creation"""
launch = JobLaunch(
job=123,
id=456,
type="job",
url="/api/v2/jobs/123/"
)
assert launch.job == 123
assert launch.id == 456
assert launch.type == "job"
@runner.test("AAPClient Configuration from Environment")
def test_aap_client_config():
"""Test AAPClient configuration loading"""
# Mock environment variables
with patch.dict(os.environ, {
'AAP_URL': 'https://test.example.com',
'AAP_TOKEN': 'test-token',
'AAP_PROJECT_ID': '123',
'AAP_VERIFY_SSL': 'False',
'AAP_TIMEOUT': '60'
}):
client = AAPClient()
assert client.config.url == 'https://test.example.com'
assert client.config.token == 'test-token'
assert client.config.project_id == '123'
assert client.config.verify_ssl is False
assert client.config.timeout == 60
@runner.test("AAPClient Invalid Configuration")
def test_aap_client_invalid_config():
"""Test AAPClient with invalid configuration"""
with patch.dict(os.environ, {}, clear=True):
try:
client = AAPClient()
assert False, "Should have raised ValueError"
except ValueError as e:
assert "AAP_URL and AAP_TOKEN must be configured" in str(e)
@runner.test("AAPClient HTTP Request Formatting")
async def test_aap_client_request_formatting():
"""Test AAPClient request URL formatting"""
config = AAPConfig(
url="https://test.example.com",
token="test-token",
project_id="123"
)
# Mock the httpx client
with patch('aap_client.httpx.AsyncClient') as mock_client:
mock_response = MagicMock()
mock_response.json.return_value = {"results": []}
mock_response.raise_for_status.return_value = None
mock_client.return_value.__aenter__.return_value.request = AsyncMock(return_value=mock_response)
client = AAPClient(config)
# Test URL formatting
async with client:
await client.get_job_templates()
# Check that the request was made with correct URL
call_args = mock_client.return_value.__aenter__.return_value.request.call_args
assert call_args[0][0] == "GET" # Method
assert call_args[0][1] == "/api/v2/job_templates/" # URL
@runner.test("Server Tool Registration")
def test_server_tool_registration():
"""Test that server tools are properly registered"""
# Import server to test tool registration
import server
# Check that the server has the expected tools
expected_tools = [
"get_job_templates",
"launch_job_template",
"get_job_status",
"get_job_output",
"test_aap_connection"
]
# This is a basic check - in a real scenario, we'd need to mock the server startup
# For now, just verify the server module can be imported
assert hasattr(server, 'server')
assert hasattr(server, 'list_tools')
assert hasattr(server, 'call_tool')
@runner.test("Mock AAP API Response Parsing")
async def test_mock_aap_response():
"""Test parsing of mock AAP API responses"""
# Mock AAP API response
mock_response = {
"results": [
{
"id": 1,
"name": "Deploy Web App",
"description": "Deploy web application",
"project": 5,
"playbook": "deploy.yml",
"survey_enabled": False
},
{
"id": 2,
"name": "Restart Services",
"description": "Restart system services",
"project": 5,
"playbook": "restart.yml",
"survey_enabled": True
}
]
}
# Test parsing
templates = []
for template_data in mock_response["results"]:
template = JobTemplate(**template_data)
templates.append(template)
assert len(templates) == 2
assert templates[0].name == "Deploy Web App"
assert templates[1].survey_enabled is True
@runner.test("Error Handling in AAP Client")
async def test_aap_client_error_handling():
"""Test error handling in AAPClient"""
config = AAPConfig(
url="https://test.example.com",
token="test-token",
project_id="123",
max_retries=1 # Limit retries for testing
)
with patch('aap_client.httpx.AsyncClient') as mock_client:
# Mock a failing HTTP request
mock_client.return_value.__aenter__.return_value.request.side_effect = Exception("Network error")
client = AAPClient(config)
try:
async with client:
await client.get_job_templates()
assert False, "Should have raised exception"
except Exception as e:
assert "AAP API request failed" in str(e)
def test_environment_loading():
"""Test environment variable loading"""
# Test with .env file
env_content = """
AAP_URL=https://env.example.com
AAP_TOKEN=env-token
AAP_PROJECT_ID=456
AAP_VERIFY_SSL=True
"""
# Create temporary .env file
with open('.env.test', 'w') as f:
f.write(env_content)
try:
# Load environment from test file
from dotenv import load_dotenv
load_dotenv('.env.test')
# Check if variables are loaded
assert os.getenv('AAP_URL') == 'https://env.example.com'
assert os.getenv('AAP_TOKEN') == 'env-token'
assert os.getenv('AAP_PROJECT_ID') == '456'
finally:
# Clean up
if os.path.exists('.env.test'):
os.remove('.env.test')
async def run_integration_test():
"""Run integration test with real AAP (if configured)"""
print("\n🔗 Integration Test (requires real AAP configuration)")
try:
# Try to create a real AAPClient
client = AAPClient()
print(f"Testing connection to: {client.config.url}")
async with client:
# Test connection
connection_ok = await client.test_connection()
if connection_ok:
print("✅ Real AAP connection successful!")
# Test template listing
templates = await client.get_job_templates()
print(f"✅ Found {len(templates)} templates")
if templates:
print("Sample templates:")
for i, template in enumerate(templates[:3]):
print(f" {i+1}. {template.name} (ID: {template.id})")
else:
print("❌ Real AAP connection failed")
except Exception as e:
print(f"⚠️ Integration test skipped: {e}")
print(" (This is normal if AAP is not configured)")
async def main():
"""Run all tests"""
# Run unit tests
success = await runner.run_all_tests()
# Run integration test if possible
await run_integration_test()
if success:
print("\n🎉 All unit tests passed!")
else:
print("\n❌ Some tests failed")
sys.exit(1)
if __name__ == "__main__":
asyncio.run(main())