-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathvalidation_env_test.go
More file actions
489 lines (428 loc) · 13.1 KB
/
validation_env_test.go
File metadata and controls
489 lines (428 loc) · 13.1 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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
package config
import (
"os"
"strings"
"testing"
"github.com/github/gh-aw-mcpg/internal/sys"
"github.com/stretchr/testify/assert"
)
func TestDetectContainerID(t *testing.T) {
// This test verifies the function doesn't panic and returns consistent results
isContainerized, containerID := sys.DetectContainerID()
// In a test environment, we're typically not containerized
// but we just verify the function works
t.Logf("DetectContainerID: isContainerized=%v, containerID=%s", isContainerized, containerID)
// If we detect a container, the ID should have some content
if isContainerized && containerID != "" {
assert.GreaterOrEqual(t, len(containerID), 12, "Container ID should be at least 12 characters")
}
}
func TestCheckRequiredEnvVars(t *testing.T) {
// Clear any existing env vars for the test
for _, v := range RequiredEnvVars {
os.Unsetenv(v)
}
defer func() {
for _, v := range RequiredEnvVars {
os.Unsetenv(v)
}
}()
tests := []struct {
name string
envVars map[string]string
expected []string
}{
{
name: "all missing",
envVars: map[string]string{},
expected: RequiredEnvVars,
},
{
name: "all set",
envVars: map[string]string{
"MCP_GATEWAY_PORT": "8080",
"MCP_GATEWAY_DOMAIN": "localhost",
"MCP_GATEWAY_API_KEY": "test-key",
},
expected: nil,
},
{
name: "partial set - missing port",
envVars: map[string]string{
"MCP_GATEWAY_DOMAIN": "localhost",
"MCP_GATEWAY_API_KEY": "test-key",
},
expected: []string{"MCP_GATEWAY_PORT"},
},
{
name: "partial set - missing domain",
envVars: map[string]string{
"MCP_GATEWAY_PORT": "8080",
"MCP_GATEWAY_API_KEY": "test-key",
},
expected: []string{"MCP_GATEWAY_DOMAIN"},
},
{
name: "partial set - missing api key",
envVars: map[string]string{
"MCP_GATEWAY_PORT": "8080",
"MCP_GATEWAY_DOMAIN": "localhost",
},
expected: []string{"MCP_GATEWAY_API_KEY"},
},
{
name: "empty string values are missing",
envVars: map[string]string{
"MCP_GATEWAY_PORT": "",
"MCP_GATEWAY_DOMAIN": "localhost",
"MCP_GATEWAY_API_KEY": "test-key",
},
expected: []string{"MCP_GATEWAY_PORT"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Clear all env vars first
for _, v := range RequiredEnvVars {
os.Unsetenv(v)
}
// Set up test environment
for k, v := range tt.envVars {
if v != "" {
os.Setenv(k, v)
}
}
missing := checkRequiredEnvVars()
assert.ElementsMatch(t, tt.expected, missing, "Unexpected missing vars")
})
}
}
func TestGetGatewayDomainFromEnv(t *testing.T) {
tests := []struct {
name string
envValue string
setEnv bool
}{
{
name: "valid domain",
envValue: "localhost",
setEnv: true,
},
{
name: "domain with subdomain",
envValue: "mcp.example.com",
setEnv: true,
},
{
name: "not set",
setEnv: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
os.Unsetenv("MCP_GATEWAY_DOMAIN")
if tt.setEnv {
os.Setenv("MCP_GATEWAY_DOMAIN", tt.envValue)
}
defer os.Unsetenv("MCP_GATEWAY_DOMAIN")
domain := GetGatewayDomainFromEnv()
if tt.setEnv {
assert.Equal(t, tt.envValue, domain)
} else {
assert.Empty(t, domain, "Expected empty domain when not set")
}
})
}
}
func TestGetGatewayAPIKeyFromEnv(t *testing.T) {
tests := []struct {
name string
envValue string
setEnv bool
}{
{
name: "valid key",
envValue: "my-secret-key",
setEnv: true,
},
{
name: "complex key",
envValue: "abc123!@#$%^&*()",
setEnv: true,
},
{
name: "not set",
setEnv: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
os.Unsetenv("MCP_GATEWAY_API_KEY")
if tt.setEnv {
os.Setenv("MCP_GATEWAY_API_KEY", tt.envValue)
}
defer os.Unsetenv("MCP_GATEWAY_API_KEY")
key := GetGatewayAPIKeyFromEnv()
if tt.setEnv {
assert.Equal(t, tt.envValue, key)
} else {
assert.Empty(t, key, "Expected empty key when not set")
}
})
}
}
func TestEnvValidationResultIsValid(t *testing.T) {
tests := []struct {
name string
result *EnvValidationResult
valid bool
}{
{
name: "valid - no errors",
result: &EnvValidationResult{},
valid: true,
},
{
name: "valid - with warnings",
result: &EnvValidationResult{
ValidationWarnings: []string{"some warning"},
},
valid: true,
},
{
name: "invalid - with errors",
result: &EnvValidationResult{
ValidationErrors: []string{"some error"},
},
valid: false,
},
{
name: "invalid - multiple errors",
result: &EnvValidationResult{
ValidationErrors: []string{"error 1", "error 2"},
},
valid: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.valid, tt.result.IsValid())
})
}
}
func TestEnvValidationResultError(t *testing.T) {
tests := []struct {
name string
result *EnvValidationResult
expected string
}{
{
name: "no errors",
result: &EnvValidationResult{},
expected: "",
},
{
name: "single error",
result: &EnvValidationResult{
ValidationErrors: []string{"Docker not accessible"},
},
expected: "Environment validation failed:\n - Docker not accessible",
},
{
name: "multiple errors",
result: &EnvValidationResult{
ValidationErrors: []string{"Error 1", "Error 2"},
},
expected: "Environment validation failed:\n - Error 1\n - Error 2",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.expected, tt.result.Error())
})
}
}
func TestValidateExecutionEnvironment(t *testing.T) {
// This test verifies the function runs without panicking
// The actual Docker check will fail in most test environments
// Save original env vars
origPort := os.Getenv("MCP_GATEWAY_PORT")
origDomain := os.Getenv("MCP_GATEWAY_DOMAIN")
origAPIKey := os.Getenv("MCP_GATEWAY_API_KEY")
defer func() {
if origPort != "" {
os.Setenv("MCP_GATEWAY_PORT", origPort)
}
if origDomain != "" {
os.Setenv("MCP_GATEWAY_DOMAIN", origDomain)
}
if origAPIKey != "" {
os.Setenv("MCP_GATEWAY_API_KEY", origAPIKey)
}
}()
t.Run("with all env vars set", func(t *testing.T) {
os.Setenv("MCP_GATEWAY_PORT", "8080")
os.Setenv("MCP_GATEWAY_DOMAIN", "localhost")
os.Setenv("MCP_GATEWAY_API_KEY", "test-key")
result := ValidateExecutionEnvironment()
// Should not have missing env vars
assert.Empty(t, result.MissingEnvVars, "Expected no missing env vars")
})
t.Run("with missing env vars", func(t *testing.T) {
os.Unsetenv("MCP_GATEWAY_PORT")
os.Unsetenv("MCP_GATEWAY_DOMAIN")
os.Unsetenv("MCP_GATEWAY_API_KEY")
result := ValidateExecutionEnvironment()
// Should have missing env vars
assert.Len(t, result.MissingEnvVars, 3, "Expected 3 missing env vars")
// Should have validation errors
assert.NotEmpty(t, result.ValidationErrors, "Expected validation errors for missing env vars")
})
}
func TestValidateContainerizedEnvironment(t *testing.T) {
// Save original env vars
origPort := os.Getenv("MCP_GATEWAY_PORT")
origDomain := os.Getenv("MCP_GATEWAY_DOMAIN")
origAPIKey := os.Getenv("MCP_GATEWAY_API_KEY")
origLogDir := os.Getenv("MCP_GATEWAY_LOG_DIR")
defer func() {
if origPort != "" {
os.Setenv("MCP_GATEWAY_PORT", origPort)
} else {
os.Unsetenv("MCP_GATEWAY_PORT")
}
if origDomain != "" {
os.Setenv("MCP_GATEWAY_DOMAIN", origDomain)
} else {
os.Unsetenv("MCP_GATEWAY_DOMAIN")
}
if origAPIKey != "" {
os.Setenv("MCP_GATEWAY_API_KEY", origAPIKey)
} else {
os.Unsetenv("MCP_GATEWAY_API_KEY")
}
if origLogDir != "" {
os.Setenv("MCP_GATEWAY_LOG_DIR", origLogDir)
} else {
os.Unsetenv("MCP_GATEWAY_LOG_DIR")
}
}()
t.Run("empty container ID", func(t *testing.T) {
os.Setenv("MCP_GATEWAY_PORT", "8080")
os.Setenv("MCP_GATEWAY_DOMAIN", "localhost")
os.Setenv("MCP_GATEWAY_API_KEY", "test-key")
result := ValidateContainerizedEnvironment("")
assert.True(t, result.IsContainerized, "Should be marked as containerized")
assert.Equal(t, "", result.ContainerID, "Container ID should be empty")
assert.False(t, result.IsValid(), "Should be invalid with empty container ID")
assert.Contains(t, result.Error(), "Container ID could not be determined")
})
t.Run("valid container ID with all env vars", func(t *testing.T) {
os.Setenv("MCP_GATEWAY_PORT", "8080")
os.Setenv("MCP_GATEWAY_DOMAIN", "localhost")
os.Setenv("MCP_GATEWAY_API_KEY", "test-key")
result := ValidateContainerizedEnvironment("abcdef123456")
assert.True(t, result.IsContainerized, "Should be marked as containerized")
assert.Equal(t, "abcdef123456", result.ContainerID)
// Will fail validation because Docker checks will fail in test environment
// but we verify the container ID was set correctly
})
t.Run("missing required env vars", func(t *testing.T) {
os.Unsetenv("MCP_GATEWAY_PORT")
os.Unsetenv("MCP_GATEWAY_DOMAIN")
os.Unsetenv("MCP_GATEWAY_API_KEY")
result := ValidateContainerizedEnvironment("abcdef123456")
assert.True(t, result.IsContainerized)
assert.Equal(t, "abcdef123456", result.ContainerID)
assert.False(t, result.IsValid(), "Should be invalid with missing env vars")
assert.Len(t, result.MissingEnvVars, 3, "Should have 3 missing env vars")
})
t.Run("port validation failure", func(t *testing.T) {
os.Setenv("MCP_GATEWAY_PORT", "8080")
os.Setenv("MCP_GATEWAY_DOMAIN", "localhost")
os.Setenv("MCP_GATEWAY_API_KEY", "test-key")
result := ValidateContainerizedEnvironment("abcdef123456")
assert.True(t, result.IsContainerized)
// Port mapping check will fail (container doesn't exist)
assert.False(t, result.PortMapped, "Port should not be mapped for nonexistent container")
})
t.Run("stdin interactive check", func(t *testing.T) {
os.Setenv("MCP_GATEWAY_PORT", "8080")
os.Setenv("MCP_GATEWAY_DOMAIN", "localhost")
os.Setenv("MCP_GATEWAY_API_KEY", "test-key")
result := ValidateContainerizedEnvironment("abcdef123456")
assert.True(t, result.IsContainerized)
// Stdin check will fail (container doesn't exist)
assert.False(t, result.StdinInteractive, "Stdin should not be interactive for nonexistent container")
})
t.Run("log directory mount check with default", func(t *testing.T) {
os.Setenv("MCP_GATEWAY_PORT", "8080")
os.Setenv("MCP_GATEWAY_DOMAIN", "localhost")
os.Setenv("MCP_GATEWAY_API_KEY", "test-key")
os.Unsetenv("MCP_GATEWAY_LOG_DIR")
result := ValidateContainerizedEnvironment("abcdef123456")
assert.True(t, result.IsContainerized)
// Log dir check will fail (container doesn't exist)
assert.False(t, result.LogDirMounted, "Log dir should not be mounted for nonexistent container")
// Should have a warning about log dir not being mounted
assert.NotEmpty(t, result.ValidationWarnings, "Should have warnings")
})
t.Run("log directory mount check with custom dir", func(t *testing.T) {
os.Setenv("MCP_GATEWAY_PORT", "8080")
os.Setenv("MCP_GATEWAY_DOMAIN", "localhost")
os.Setenv("MCP_GATEWAY_API_KEY", "test-key")
os.Setenv("MCP_GATEWAY_LOG_DIR", "/custom/log/path")
result := ValidateContainerizedEnvironment("abcdef123456")
assert.True(t, result.IsContainerized)
assert.False(t, result.LogDirMounted)
// Verify the warning mentions the custom path
hasCustomPathWarning := false
for _, warning := range result.ValidationWarnings {
if strings.Contains(warning, "/custom/log/path") {
hasCustomPathWarning = true
break
}
}
if len(result.ValidationWarnings) > 0 {
assert.True(t, hasCustomPathWarning, "Should have warning with custom log path")
}
})
t.Run("docker not accessible", func(t *testing.T) {
// Set a DOCKER_HOST that doesn't exist
originalHost := os.Getenv("DOCKER_HOST")
defer func() {
if originalHost != "" {
os.Setenv("DOCKER_HOST", originalHost)
} else {
os.Unsetenv("DOCKER_HOST")
}
}()
os.Setenv("DOCKER_HOST", "unix:///nonexistent/docker.sock")
os.Setenv("MCP_GATEWAY_PORT", "8080")
os.Setenv("MCP_GATEWAY_DOMAIN", "localhost")
os.Setenv("MCP_GATEWAY_API_KEY", "test-key")
result := ValidateContainerizedEnvironment("abcdef123456")
assert.False(t, result.DockerAccessible, "Docker should not be accessible")
assert.False(t, result.IsValid(), "Should be invalid when Docker is not accessible")
// Should have error about Docker not being accessible
hasDockerError := false
for _, errMsg := range result.ValidationErrors {
if strings.Contains(errMsg, "Docker daemon") {
hasDockerError = true
break
}
}
assert.True(t, hasDockerError, "Should have Docker accessibility error")
})
t.Run("validation result error message format", func(t *testing.T) {
os.Unsetenv("MCP_GATEWAY_PORT")
os.Unsetenv("MCP_GATEWAY_DOMAIN")
os.Unsetenv("MCP_GATEWAY_API_KEY")
result := ValidateContainerizedEnvironment("abcdef123456")
errorMsg := result.Error()
assert.NotEmpty(t, errorMsg, "Error message should not be empty")
assert.Contains(t, errorMsg, "Environment validation failed", "Error should have header")
// Each error should be on its own line with bullet point
assert.Contains(t, errorMsg, "\n - ", "Errors should be formatted with bullets")
})
}