215 lines
4.8 KiB
Go
215 lines
4.8 KiB
Go
package ezconf
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
// mockConfig is a test config struct with ezconf tags
|
|
type mockConfig struct {
|
|
Host string `ezconf:"MOCK_HOST,description:Host to connect to,default:localhost"`
|
|
Port int `ezconf:"MOCK_PORT,description:Port to connect to,default:8080"`
|
|
}
|
|
|
|
// mockConfig2 is a second test config struct
|
|
type mockConfig2 struct {
|
|
Token string `ezconf:"MOCK_TOKEN,description:API token,required"`
|
|
}
|
|
|
|
// mockIntegration implements the Integration interface for testing
|
|
type mockIntegration struct {
|
|
name string
|
|
configPtr any
|
|
configFunc func() (interface{}, error)
|
|
groupName string
|
|
}
|
|
|
|
func (m mockIntegration) Name() string {
|
|
return m.name
|
|
}
|
|
|
|
func (m mockIntegration) ConfigPointer() any {
|
|
return m.configPtr
|
|
}
|
|
|
|
func (m mockIntegration) ConfigFunc() func() (interface{}, error) {
|
|
return m.configFunc
|
|
}
|
|
|
|
func (m mockIntegration) GroupName() string {
|
|
if m.groupName == "" {
|
|
return "Test Group"
|
|
}
|
|
return m.groupName
|
|
}
|
|
|
|
func TestRegisterIntegration(t *testing.T) {
|
|
loader := New()
|
|
|
|
integration := mockIntegration{
|
|
name: "test",
|
|
configPtr: &mockConfig{},
|
|
configFunc: func() (interface{}, error) {
|
|
return "test config", nil
|
|
},
|
|
}
|
|
|
|
err := loader.RegisterIntegration(integration)
|
|
if err != nil {
|
|
t.Fatalf("RegisterIntegration failed: %v", err)
|
|
}
|
|
|
|
// Verify config struct was added
|
|
if len(loader.configStructs) != 1 {
|
|
t.Errorf("expected 1 config struct, got %d", len(loader.configStructs))
|
|
}
|
|
|
|
// Verify config func was added
|
|
if len(loader.configFuncs) != 1 {
|
|
t.Errorf("expected 1 config func, got %d", len(loader.configFuncs))
|
|
}
|
|
|
|
// Load and verify config
|
|
if err := loader.Load(); err != nil {
|
|
t.Fatalf("Load failed: %v", err)
|
|
}
|
|
|
|
cfg, ok := loader.GetConfig("test")
|
|
if !ok {
|
|
t.Error("test config not found")
|
|
}
|
|
|
|
if cfg != "test config" {
|
|
t.Errorf("expected 'test config', got %v", cfg)
|
|
}
|
|
|
|
// Verify env vars were parsed from struct tags
|
|
envVars := loader.GetEnvVars()
|
|
if len(envVars) != 2 {
|
|
t.Errorf("expected 2 env vars, got %d", len(envVars))
|
|
}
|
|
|
|
foundHost := false
|
|
foundPort := false
|
|
for _, ev := range envVars {
|
|
if ev.Name == "MOCK_HOST" {
|
|
foundHost = true
|
|
if ev.Default != "localhost" {
|
|
t.Errorf("expected default 'localhost', got '%s'", ev.Default)
|
|
}
|
|
if ev.Group != "Test Group" {
|
|
t.Errorf("expected group 'Test Group', got '%s'", ev.Group)
|
|
}
|
|
}
|
|
if ev.Name == "MOCK_PORT" {
|
|
foundPort = true
|
|
if ev.Default != "8080" {
|
|
t.Errorf("expected default '8080', got '%s'", ev.Default)
|
|
}
|
|
}
|
|
}
|
|
if !foundHost {
|
|
t.Error("MOCK_HOST not found in env vars")
|
|
}
|
|
if !foundPort {
|
|
t.Error("MOCK_PORT not found in env vars")
|
|
}
|
|
}
|
|
|
|
func TestRegisterIntegration_NilConfigPointer(t *testing.T) {
|
|
loader := New()
|
|
|
|
integration := mockIntegration{
|
|
name: "test",
|
|
configPtr: nil,
|
|
configFunc: func() (interface{}, error) {
|
|
return "test config", nil
|
|
},
|
|
}
|
|
|
|
err := loader.RegisterIntegration(integration)
|
|
if err == nil {
|
|
t.Error("expected error for nil config pointer")
|
|
}
|
|
}
|
|
|
|
func TestRegisterIntegrations(t *testing.T) {
|
|
loader := New()
|
|
|
|
integration1 := mockIntegration{
|
|
name: "test1",
|
|
configPtr: &mockConfig{},
|
|
configFunc: func() (interface{}, error) {
|
|
return "config1", nil
|
|
},
|
|
}
|
|
|
|
integration2 := mockIntegration{
|
|
name: "test2",
|
|
configPtr: &mockConfig2{},
|
|
configFunc: func() (interface{}, error) {
|
|
return "config2", nil
|
|
},
|
|
}
|
|
|
|
err := loader.RegisterIntegrations(integration1, integration2)
|
|
if err != nil {
|
|
t.Fatalf("RegisterIntegrations failed: %v", err)
|
|
}
|
|
|
|
if len(loader.configFuncs) != 2 {
|
|
t.Errorf("expected 2 config funcs, got %d", len(loader.configFuncs))
|
|
}
|
|
|
|
// Load and verify configs
|
|
if err := loader.Load(); err != nil {
|
|
t.Fatalf("Load failed: %v", err)
|
|
}
|
|
|
|
cfg1, ok1 := loader.GetConfig("test1")
|
|
cfg2, ok2 := loader.GetConfig("test2")
|
|
|
|
if !ok1 || !ok2 {
|
|
t.Error("configs not found")
|
|
}
|
|
|
|
if cfg1 != "config1" || cfg2 != "config2" {
|
|
t.Error("config values mismatch")
|
|
}
|
|
|
|
// Should have env vars from both structs
|
|
envVars := loader.GetEnvVars()
|
|
if len(envVars) != 3 {
|
|
t.Errorf("expected 3 env vars (2 from mockConfig + 1 from mockConfig2), got %d", len(envVars))
|
|
}
|
|
}
|
|
|
|
func TestRegisterIntegrations_PartialFailure(t *testing.T) {
|
|
loader := New()
|
|
|
|
integration1 := mockIntegration{
|
|
name: "test1",
|
|
configPtr: &mockConfig{},
|
|
configFunc: func() (interface{}, error) {
|
|
return "config1", nil
|
|
},
|
|
}
|
|
|
|
integration2 := mockIntegration{
|
|
name: "test2",
|
|
configPtr: nil, // This should cause failure
|
|
configFunc: func() (interface{}, error) {
|
|
return "config2", nil
|
|
},
|
|
}
|
|
|
|
err := loader.RegisterIntegrations(integration1, integration2)
|
|
if err == nil {
|
|
t.Error("expected error when one integration fails")
|
|
}
|
|
}
|
|
|
|
func TestIntegration_Interface(t *testing.T) {
|
|
// Verify that mockIntegration implements Integration interface
|
|
var _ Integration = (*mockIntegration)(nil)
|
|
}
|