115 lines
3.1 KiB
Go
115 lines
3.1 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
)
|
|
|
|
func TestInit(t *testing.T) {
|
|
// Save original env vars
|
|
origEndpoint := os.Getenv("ES_ENDPOINT")
|
|
origRegion := os.Getenv("AWS_REGION")
|
|
origAccessKey := os.Getenv("AWS_ACCESS_KEY_ID")
|
|
origSecretKey := os.Getenv("AWS_SECRET_ACCESS_KEY")
|
|
|
|
// Restore original env vars after test
|
|
defer func() {
|
|
os.Setenv("ES_ENDPOINT", origEndpoint)
|
|
os.Setenv("AWS_REGION", origRegion)
|
|
os.Setenv("AWS_ACCESS_KEY_ID", origAccessKey)
|
|
os.Setenv("AWS_SECRET_ACCESS_KEY", origSecretKey)
|
|
}()
|
|
|
|
// Set test env vars
|
|
os.Setenv("ES_ENDPOINT", "https://test.example.com")
|
|
os.Setenv("AWS_REGION", "us-west-2")
|
|
os.Setenv("AWS_ACCESS_KEY_ID", "test-access-key")
|
|
os.Setenv("AWS_SECRET_ACCESS_KEY", "test-secret-key")
|
|
|
|
// Test Init
|
|
Init()
|
|
|
|
if Endpoint != "https://test.example.com" {
|
|
t.Errorf("Endpoint = %v, want %v", Endpoint, "https://test.example.com")
|
|
}
|
|
if Region != "us-west-2" {
|
|
t.Errorf("Region = %v, want %v", Region, "us-west-2")
|
|
}
|
|
if AccessKey != "test-access-key" {
|
|
t.Errorf("AccessKey = %v, want %v", AccessKey, "test-access-key")
|
|
}
|
|
if SecretKey != "test-secret-key" {
|
|
t.Errorf("SecretKey = %v, want %v", SecretKey, "test-secret-key")
|
|
}
|
|
}
|
|
|
|
func TestLoad(t *testing.T) {
|
|
// Save original env vars
|
|
origEndpoint := os.Getenv("ES_ENDPOINT")
|
|
origRegion := os.Getenv("AWS_REGION")
|
|
origAccessKey := os.Getenv("AWS_ACCESS_KEY_ID")
|
|
origSecretKey := os.Getenv("AWS_SECRET_ACCESS_KEY")
|
|
|
|
// Clear environment variables before test
|
|
os.Unsetenv("ES_ENDPOINT")
|
|
os.Unsetenv("AWS_REGION")
|
|
os.Unsetenv("AWS_ACCESS_KEY_ID")
|
|
os.Unsetenv("AWS_SECRET_ACCESS_KEY")
|
|
|
|
// Restore original env vars after test
|
|
defer func() {
|
|
if origEndpoint != "" {
|
|
os.Setenv("ES_ENDPOINT", origEndpoint)
|
|
}
|
|
if origRegion != "" {
|
|
os.Setenv("AWS_REGION", origRegion)
|
|
}
|
|
if origAccessKey != "" {
|
|
os.Setenv("AWS_ACCESS_KEY_ID", origAccessKey)
|
|
}
|
|
if origSecretKey != "" {
|
|
os.Setenv("AWS_SECRET_ACCESS_KEY", origSecretKey)
|
|
}
|
|
// Reload original config
|
|
Init()
|
|
}()
|
|
|
|
// Create a temporary .env file
|
|
tmpFile := ".env.test"
|
|
content := `ES_ENDPOINT=https://test-load.example.com
|
|
AWS_REGION=eu-west-1
|
|
AWS_ACCESS_KEY_ID=load-access-key
|
|
AWS_SECRET_ACCESS_KEY=load-secret-key
|
|
`
|
|
if err := os.WriteFile(tmpFile, []byte(content), 0644); err != nil {
|
|
t.Fatalf("failed to create test .env file: %v", err)
|
|
}
|
|
defer os.Remove(tmpFile)
|
|
|
|
// Test Load
|
|
if err := Load(tmpFile); err != nil {
|
|
t.Fatalf("Load() error = %v", err)
|
|
}
|
|
|
|
if Endpoint != "https://test-load.example.com" {
|
|
t.Errorf("Endpoint = %v, want %v", Endpoint, "https://test-load.example.com")
|
|
}
|
|
if Region != "eu-west-1" {
|
|
t.Errorf("Region = %v, want %v", Region, "eu-west-1")
|
|
}
|
|
if AccessKey != "load-access-key" {
|
|
t.Errorf("AccessKey = %v, want %v", AccessKey, "load-access-key")
|
|
}
|
|
if SecretKey != "load-secret-key" {
|
|
t.Errorf("SecretKey = %v, want %v", SecretKey, "load-secret-key")
|
|
}
|
|
}
|
|
|
|
func TestLoad_FileNotFound(t *testing.T) {
|
|
// Test with non-existent file - should not return error
|
|
err := Load("non-existent-file.env")
|
|
if err != nil {
|
|
t.Errorf("Load() with non-existent file should not error, got %v", err)
|
|
}
|
|
}
|