-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.js
More file actions
237 lines (221 loc) · 6.19 KB
/
test.js
File metadata and controls
237 lines (221 loc) · 6.19 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
#!/usr/bin/env node
import { spawn } from 'child_process';
import { createInterface } from 'readline';
import { writeFileSync, unlinkSync, existsSync } from 'fs';
import { join } from 'path';
import os from 'os';
// Create a temporary docker-compose.yml file for testing
const createTestComposeFile = () => {
const composeContent = `
version: '3'
services:
test-service:
image: hello-world
`;
const composePath = join(process.cwd(), 'test-docker-compose.yml');
writeFileSync(composePath, composeContent);
return composePath;
};
// Clean up temporary files
const cleanupTestFiles = (composePath) => {
if (existsSync(composePath)) {
unlinkSync(composePath);
}
};
// Helper function to send requests to server - 确保每个请求以换行符结束
const sendRequest = (server, request) => {
const requestStr = JSON.stringify(request) + '\n';
server.stdin.write(requestStr);
console.log('Sent request:', JSON.stringify(request, null, 2));
};
// Create test compose file
const testComposePath = createTestComposeFile();
// Start the MCP server
const server = spawn('node', ['dist/index.js'], {
stdio: ['pipe', 'pipe', 'inherit']
});
// Create a temporary docker-compose.yml file for testing
const tempFilePath = join(os.tmpdir(), 'test-docker-compose.yml');
const composeFileContent = `
version: '3'
services:
nginx:
image: nginx:latest
ports:
- "8080:80"
`;
// 创建文件
writeFileSync(tempFilePath, composeFileContent);
// Create readline interface for reading server output
const rl = createInterface({
input: server.stdout,
crlfDelay: Infinity
});
// 跟踪测试步骤
let testStep = 0;
// Wait for server to start
setTimeout(() => {
console.log('Testing Docker MCP server...');
// 初始化请求
testStep = 1;
const initializeRequest = {
jsonrpc: '2.0',
id: '1',
method: 'initialize',
params: {
protocolVersion: '2024-11-05',
clientInfo: {
name: 'test-client',
version: '0.3.0'
},
capabilities: {
tools: {}
}
}
};
sendRequest(server, initializeRequest);
}, 1000);
// Handle server responses
rl.on('line', (line) => {
try {
// 忽略非JSON输出,比如"Docker MCP server running on stdio"
if (line.trim().startsWith('{') && line.trim().endsWith('}')) {
const response = JSON.parse(line);
console.log('Server response:', JSON.stringify(response, null, 2));
// 根据测试步骤进行下一步测试
if (testStep === 1 && response.id === '1') {
// 初始化成功,发送initialized通知
testStep = 2;
const initializedNotification = {
jsonrpc: '2.0',
method: 'initialized',
params: {}
};
sendRequest(server, initializedNotification);
// 请求工具列表
setTimeout(() => {
testStep = 3;
const listToolsRequest = {
jsonrpc: '2.0',
id: '2',
method: 'tools/list',
params: {}
};
sendRequest(server, listToolsRequest);
}, 500);
}
else if (testStep === 3 && response.id === '2') {
// 获取工具列表成功,测试容器运行
testStep = 4;
const runContainerRequest = {
jsonrpc: '2.0',
id: '3',
method: 'tools/call',
params: {
name: 'run_container',
arguments: {
image: 'hello-world'
}
}
};
sendRequest(server, runContainerRequest);
}
else if (testStep === 4 && response.id === '3') {
// 运行容器成功,测试Docker Compose命令
testStep = 5;
console.log('Testing Docker Compose commands...');
const composeUpRequest = {
jsonrpc: '2.0',
id: '4',
method: 'tools/call',
params: {
name: 'compose_up',
arguments: {
file: testComposePath,
detach: true
}
}
};
sendRequest(server, composeUpRequest);
}
else if (testStep === 5 && response.id === '4') {
// compose up成功,测试compose ps
testStep = 6;
const composePsRequest = {
jsonrpc: '2.0',
id: '5',
method: 'tools/call',
params: {
name: 'compose_ps',
arguments: {
file: testComposePath
}
}
};
sendRequest(server, composePsRequest);
}
else if (testStep === 6 && response.id === '5') {
// compose ps成功,测试compose down
testStep = 7;
const composeDownRequest = {
jsonrpc: '2.0',
id: '6',
method: 'tools/call',
params: {
name: 'compose_down',
arguments: {
file: testComposePath
}
}
};
sendRequest(server, composeDownRequest);
}
else if (testStep === 7 && response.id === '6') {
// 所有测试完成,退出
testStep = 8;
console.log('Tests completed. Exiting...');
cleanupTestFiles(testComposePath);
// 清理临时文件
if (existsSync(tempFilePath)) {
unlinkSync(tempFilePath);
}
server.kill();
process.exit(0);
}
} else {
console.log('Server message (non-JSON):', line);
}
} catch (error) {
console.error('Error parsing response:', error.message);
console.error('Raw line:', line);
}
});
// 设置超时,避免测试卡住
setTimeout(() => {
console.log('Test timeout reached. Exiting...');
cleanupTestFiles(testComposePath);
if (existsSync(tempFilePath)) {
unlinkSync(tempFilePath);
}
server.kill();
process.exit(1);
}, 30000);
// Handle server exit
server.on('exit', (code) => {
console.log(`Server exited with code ${code}`);
cleanupTestFiles(testComposePath);
// 清理临时文件
if (existsSync(tempFilePath)) {
unlinkSync(tempFilePath);
}
});
// Handle process termination
process.on('SIGINT', () => {
cleanupTestFiles(testComposePath);
// 清理临时文件
if (existsSync(tempFilePath)) {
unlinkSync(tempFilePath);
}
server.kill();
process.exit(0);
});