a6991079de
Add postgres to Devbox * addcmd
75 lines
2.2 KiB
Go
75 lines
2.2 KiB
Go
package serviceconfig_test
|
|
|
|
import (
|
|
"bytes"
|
|
"log/slog"
|
|
"os"
|
|
"queryorchestration/internal/serviceconfig"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// TestPrintConfigRecursive tests the PrintConfigRecursive function as a blackbox test.
|
|
func TestPrintConfigRecursive(t *testing.T) {
|
|
serviceconfig.ResetConfigInitOnceTestOnly()
|
|
os.Clearenv()
|
|
envVars := map[string]string{
|
|
"PGUSER": "postgres",
|
|
"PGPASSWORD": "password",
|
|
"PGHOST": "localhost",
|
|
"PGPORT": "5432",
|
|
"PGDATABASE": "query_orchestration",
|
|
"DB_NOSSL": "true",
|
|
"AWS_ACCESS_KEY_ID": "key",
|
|
"AWS_SECRET_ACCESS_KEY": "password",
|
|
"AWS_REGION": "region",
|
|
"PWD": "/foo", // Only include vars needed by BaseConfig
|
|
}
|
|
|
|
// Set environment variables
|
|
for k, v := range envVars {
|
|
t.Setenv(k, v)
|
|
}
|
|
|
|
// Create a buffer to capture log output
|
|
var logBuffer bytes.Buffer
|
|
|
|
// Create a test logger that writes to our buffer
|
|
testLogger := slog.New(slog.NewTextHandler(&logBuffer, &slog.HandlerOptions{
|
|
Level: slog.LevelDebug,
|
|
}))
|
|
|
|
// Use BaseConfig instead of initializeConfigTestStruct
|
|
cfg := &serviceconfig.BaseConfig{}
|
|
err := serviceconfig.InitializeConfig(cfg)
|
|
if err != nil {
|
|
t.Fatalf("Error initializing config: %v", err)
|
|
}
|
|
|
|
// Set logger after initialization
|
|
cfg.Logger = testLogger
|
|
|
|
// Call the function we're testing
|
|
cfg.PrintConfig("secret")
|
|
|
|
// Get the log output as a string
|
|
logOutput := logBuffer.String()
|
|
|
|
// Check that each environment variable value appears in the logs
|
|
// except for sensitive values which should be masked
|
|
for envKey, envValue := range envVars {
|
|
if envKey == "PGPASSWORD" || envKey == "AWS_SECRET_ACCESS_KEY" {
|
|
// Password should be masked
|
|
if strings.Contains(logOutput, envValue) {
|
|
t.Errorf("Full password should not appear in logs: %s", logOutput)
|
|
}
|
|
// Should see at least part of the masked value
|
|
if !strings.Contains(logOutput, "...") {
|
|
t.Errorf("Expected masked password in logs, got: %s", logOutput)
|
|
}
|
|
} else if !strings.Contains(logOutput, envValue) {
|
|
t.Errorf("Expected %s in logs, but it was not found. Log output: %s", envValue, logOutput)
|
|
}
|
|
}
|
|
}
|