-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathe2e_config_test.go
More file actions
402 lines (340 loc) · 9.86 KB
/
e2e_config_test.go
File metadata and controls
402 lines (340 loc) · 9.86 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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
// Copyright 2026 Aeneas Rekkas
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//go:build e2e
package main
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"path/filepath"
"testing"
"time"
"github.com/modelcontextprotocol/go-sdk/mcp"
)
// startServerWithEnv launches the MCP server subprocess with a custom
// environment. The caller controls all env vars (XDG_CONFIG_HOME,
// XDG_DATA_HOME, OLLAMA_HOST, etc.).
func startServerWithEnv(t *testing.T, env []string) *mcp.ClientSession {
t.Helper()
cmd := exec.Command(serverBinary, "stdio")
cmd.Env = env
transport := &mcp.CommandTransport{Command: cmd}
client := mcp.NewClient(&mcp.Implementation{
Name: "e2e-config-test-client",
Version: "0.1.0",
}, nil)
ctx := context.Background()
session, err := client.Connect(ctx, transport, nil)
if err != nil {
t.Fatalf("failed to connect to server: %v", err)
}
t.Cleanup(func() { session.Close() })
return session
}
// ollamaHostForTest returns the Ollama host from the environment or the default.
func ollamaHostForTest() string {
if h := os.Getenv("OLLAMA_HOST"); h != "" {
return h
}
return "http://localhost:11434"
}
// baseEnv returns the minimal env vars needed by the subprocess (HOME, PATH).
func baseEnv(dataHome string) []string {
return []string{
"HOME=" + os.Getenv("HOME"),
"PATH=" + os.Getenv("PATH"),
"XDG_DATA_HOME=" + dataHome,
}
}
// writeConfigYAML writes a config.yaml into <configHome>/lumen/config.yaml and
// returns the configHome path.
func writeConfigYAML(t *testing.T, content string) string {
t.Helper()
configHome := t.TempDir()
dir := filepath.Join(configHome, "lumen")
if err := os.MkdirAll(dir, 0755); err != nil {
t.Fatalf("MkdirAll: %v", err)
}
if err := os.WriteFile(filepath.Join(dir, "config.yaml"), []byte(content), 0644); err != nil {
t.Fatalf("WriteFile: %v", err)
}
return configHome
}
// newDownServer creates an httptest server that returns 503 on health checks
// and 500 on embed requests (simulating an unhealthy backend).
func newDownServer(t *testing.T) *httptest.Server {
t.Helper()
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusServiceUnavailable)
}))
t.Cleanup(srv.Close)
return srv
}
// --- Happy Path Tests ---
func TestE2E_Config_YAMLDrivesServerSelection(t *testing.T) {
configHome := writeConfigYAML(t, fmt.Sprintf(`
servers:
- backend: ollama
host: %s
model: all-minilm
dims: 384
max_chunk_tokens: 100
freshness_ttl: 1s
`, ollamaHostForTest()))
dataHome := t.TempDir()
env := append(baseEnv(dataHome),
"XDG_CONFIG_HOME="+configHome,
)
session := startServerWithEnv(t, env)
out := callSearch(t, session, map[string]any{
"query": "authentication",
"path": sampleProjectPath(t),
})
if len(out.Results) == 0 {
t.Fatal("expected search results from config.yaml-driven server, got none")
}
if !out.Reindexed {
t.Error("expected reindexing on first search")
}
}
func TestE2E_Config_MultiServerFailover(t *testing.T) {
down := newDownServer(t)
configHome := writeConfigYAML(t, fmt.Sprintf(`
servers:
- backend: ollama
host: %s
model: all-minilm
dims: 384
- backend: ollama
host: %s
model: all-minilm
dims: 384
max_chunk_tokens: 100
freshness_ttl: 1s
`, down.URL, ollamaHostForTest()))
dataHome := t.TempDir()
env := append(baseEnv(dataHome),
"XDG_CONFIG_HOME="+configHome,
)
session := startServerWithEnv(t, env)
out := callSearch(t, session, map[string]any{
"query": "database connection",
"path": sampleProjectPath(t),
})
if len(out.Results) == 0 {
t.Fatal("expected search results after failover to healthy server, got none")
}
}
func TestE2E_Config_HotReloadServerFailover(t *testing.T) {
// Start with only an unhealthy server — search should fail.
down := newDownServer(t)
configHome := writeConfigYAML(t, fmt.Sprintf(`
servers:
- backend: ollama
host: %s
model: all-minilm
dims: 384
max_chunk_tokens: 100
freshness_ttl: 1s
`, down.URL))
dataHome := t.TempDir()
env := append(baseEnv(dataHome),
"XDG_CONFIG_HOME="+configHome,
)
session := startServerWithEnv(t, env)
// Search should fail — only server is unhealthy
result1 := callSearchRaw(t, session, map[string]any{
"query": "authentication",
"path": sampleProjectPath(t),
})
if !result1.IsError {
t.Fatal("expected error when only server is unhealthy")
}
// Hot reload: add a healthy server
cfgFile := filepath.Join(configHome, "lumen", "config.yaml")
if err := os.WriteFile(cfgFile, []byte(fmt.Sprintf(`
servers:
- backend: ollama
host: %s
model: all-minilm
dims: 384
- backend: ollama
host: %s
model: all-minilm
dims: 384
max_chunk_tokens: 100
freshness_ttl: 1s
`, down.URL, ollamaHostForTest())), 0644); err != nil {
t.Fatalf("rewrite config: %v", err)
}
// Wait for fsnotify to pick up the change
time.Sleep(1 * time.Second)
// Search should now succeed after failover to the newly added healthy server
out := callSearch(t, session, map[string]any{
"query": "authentication",
"path": sampleProjectPath(t),
})
if len(out.Results) == 0 {
t.Fatal("expected search results after hot-reloading a healthy server, got none")
}
}
// --- Edge Case Tests ---
func TestE2E_Config_EnvVarsOverrideYAML(t *testing.T) {
// config.yaml references a model that doesn't exist
configHome := writeConfigYAML(t, fmt.Sprintf(`
servers:
- backend: ollama
host: %s
model: nonexistent-model-name
dims: 384
max_chunk_tokens: 100
freshness_ttl: 1s
`, ollamaHostForTest()))
dataHome := t.TempDir()
env := append(baseEnv(dataHome),
"XDG_CONFIG_HOME="+configHome,
// Env var overrides the bad model
"LUMEN_EMBED_MODEL=all-minilm",
)
session := startServerWithEnv(t, env)
out := callSearch(t, session, map[string]any{
"query": "authentication",
"path": sampleProjectPath(t),
})
if len(out.Results) == 0 {
t.Fatal("expected search results with env var overriding config.yaml model, got none")
}
}
func TestE2E_Config_InvalidReloadPreservesPrevious(t *testing.T) {
configHome := writeConfigYAML(t, fmt.Sprintf(`
servers:
- backend: ollama
host: %s
model: all-minilm
dims: 384
max_chunk_tokens: 100
freshness_ttl: 1s
`, ollamaHostForTest()))
dataHome := t.TempDir()
env := append(baseEnv(dataHome),
"XDG_CONFIG_HOME="+configHome,
)
session := startServerWithEnv(t, env)
// First search succeeds
out1 := callSearch(t, session, map[string]any{
"query": "authentication",
"path": sampleProjectPath(t),
})
if len(out1.Results) == 0 {
t.Fatal("expected search results before invalid reload")
}
// Reload to invalid config (empty servers list)
cfgFile := filepath.Join(configHome, "lumen", "config.yaml")
if err := os.WriteFile(cfgFile, []byte("servers: []\n"), 0644); err != nil {
t.Fatalf("rewrite config: %v", err)
}
time.Sleep(1 * time.Second)
// Search should still work with the previous valid config
out2 := callSearch(t, session, map[string]any{
"query": "authentication",
"path": sampleProjectPath(t),
})
if len(out2.Results) == 0 {
t.Fatal("expected search results after invalid config reload (should retain previous)")
}
}
func TestE2E_Config_NoConfigFileEnvVarsOnly(t *testing.T) {
// Don't create a config.yaml — set XDG_CONFIG_HOME to an empty temp dir
configHome := t.TempDir()
dataHome := t.TempDir()
env := append(baseEnv(dataHome),
"XDG_CONFIG_HOME="+configHome,
"OLLAMA_HOST="+ollamaHostForTest(),
"LUMEN_EMBED_MODEL=all-minilm",
"LUMEN_MAX_CHUNK_TOKENS=100",
"LUMEN_FRESHNESS_TTL=1s",
)
session := startServerWithEnv(t, env)
out := callSearch(t, session, map[string]any{
"query": "authentication",
"path": sampleProjectPath(t),
})
if len(out.Results) == 0 {
t.Fatal("expected search results with env-vars-only config (no config.yaml)")
}
}
// --- Unhappy Path Tests ---
func TestE2E_Config_AllServersUnhealthy(t *testing.T) {
down1 := newDownServer(t)
down2 := newDownServer(t)
configHome := writeConfigYAML(t, fmt.Sprintf(`
servers:
- backend: ollama
host: %s
model: all-minilm
dims: 384
- backend: ollama
host: %s
model: all-minilm
dims: 384
max_chunk_tokens: 100
freshness_ttl: 1s
`, down1.URL, down2.URL))
dataHome := t.TempDir()
env := append(baseEnv(dataHome),
"XDG_CONFIG_HOME="+configHome,
)
session := startServerWithEnv(t, env)
result := callSearchRaw(t, session, map[string]any{
"query": "authentication",
"path": sampleProjectPath(t),
})
if !result.IsError {
t.Fatal("expected error when all servers are unhealthy")
}
// Verify we get an error message, not a crash
text := getTextContent(t, result)
if text == "" {
t.Fatal("expected non-empty error message")
}
}
func TestE2E_Config_UnknownBackendRejectsStartup(t *testing.T) {
configHome := writeConfigYAML(t, `
servers:
- backend: foobar
host: http://localhost:9999
model: test-model
dims: 384
`)
dataHome := t.TempDir()
env := append(baseEnv(dataHome),
"XDG_CONFIG_HOME="+configHome,
)
cmd := exec.Command(serverBinary, "stdio")
cmd.Env = env
transport := &mcp.CommandTransport{Command: cmd}
client := mcp.NewClient(&mcp.Implementation{
Name: "e2e-config-test-client",
Version: "0.1.0",
}, nil)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
_, err := client.Connect(ctx, transport, nil)
if err == nil {
t.Fatal("expected server to reject startup with unknown backend, but connection succeeded")
}
}