213 lines
4.6 KiB
Go
213 lines
4.6 KiB
Go
package ezconf
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
// Mock integration for testing
|
|
type mockIntegration struct {
|
|
name string
|
|
packagePath string
|
|
configFunc func() (interface{}, error)
|
|
}
|
|
|
|
func (m mockIntegration) Name() string {
|
|
return m.name
|
|
}
|
|
|
|
func (m mockIntegration) PackagePath() string {
|
|
return m.packagePath
|
|
}
|
|
|
|
func (m mockIntegration) ConfigFunc() func() (interface{}, error) {
|
|
return m.configFunc
|
|
}
|
|
|
|
func (m mockIntegration) GroupName() string {
|
|
return "Test Group"
|
|
}
|
|
|
|
func TestRegisterIntegration(t *testing.T) {
|
|
loader := New()
|
|
|
|
integration := mockIntegration{
|
|
name: "test",
|
|
packagePath: ".",
|
|
configFunc: func() (interface{}, error) {
|
|
return "test config", nil
|
|
},
|
|
}
|
|
|
|
err := loader.RegisterIntegration(integration)
|
|
if err != nil {
|
|
t.Fatalf("RegisterIntegration failed: %v", err)
|
|
}
|
|
|
|
// Verify package path was added
|
|
if len(loader.packagePaths) != 1 {
|
|
t.Errorf("expected 1 package path, got %d", len(loader.packagePaths))
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
}
|
|
|
|
func TestRegisterIntegration_InvalidPath(t *testing.T) {
|
|
loader := New()
|
|
|
|
integration := mockIntegration{
|
|
name: "test",
|
|
packagePath: "/nonexistent/path",
|
|
configFunc: func() (interface{}, error) {
|
|
return "test config", nil
|
|
},
|
|
}
|
|
|
|
err := loader.RegisterIntegration(integration)
|
|
if err == nil {
|
|
t.Error("expected error for invalid package path")
|
|
}
|
|
}
|
|
|
|
func TestRegisterIntegrations(t *testing.T) {
|
|
loader := New()
|
|
|
|
integration1 := mockIntegration{
|
|
name: "test1",
|
|
packagePath: ".",
|
|
configFunc: func() (interface{}, error) {
|
|
return "config1", nil
|
|
},
|
|
}
|
|
|
|
integration2 := mockIntegration{
|
|
name: "test2",
|
|
packagePath: ".",
|
|
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")
|
|
}
|
|
}
|
|
|
|
func TestRegisterIntegrations_PartialFailure(t *testing.T) {
|
|
loader := New()
|
|
|
|
integration1 := mockIntegration{
|
|
name: "test1",
|
|
packagePath: ".",
|
|
configFunc: func() (interface{}, error) {
|
|
return "config1", nil
|
|
},
|
|
}
|
|
|
|
integration2 := mockIntegration{
|
|
name: "test2",
|
|
packagePath: "/nonexistent",
|
|
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)
|
|
}
|
|
|
|
func TestRegisterIntegration_RealPackage(t *testing.T) {
|
|
// Integration test with real hlog package if available
|
|
hlogPath := filepath.Join("..", "hlog")
|
|
if _, err := os.Stat(hlogPath); os.IsNotExist(err) {
|
|
t.Skip("hlog package not found, skipping integration test")
|
|
}
|
|
|
|
loader := New()
|
|
|
|
// Create a simple integration for testing
|
|
integration := mockIntegration{
|
|
name: "hlog",
|
|
packagePath: hlogPath,
|
|
configFunc: func() (interface{}, error) {
|
|
// Return a mock config instead of calling real ConfigFromEnv
|
|
return struct{ LogLevel string }{LogLevel: "info"}, nil
|
|
},
|
|
}
|
|
|
|
err := loader.RegisterIntegration(integration)
|
|
if err != nil {
|
|
t.Fatalf("RegisterIntegration with real package failed: %v", err)
|
|
}
|
|
|
|
if err := loader.Load(); err != nil {
|
|
t.Fatalf("Load failed: %v", err)
|
|
}
|
|
|
|
// Should have parsed env vars from hlog
|
|
envVars := loader.GetEnvVars()
|
|
if len(envVars) == 0 {
|
|
t.Error("expected env vars from hlog package")
|
|
}
|
|
|
|
// Check for known hlog variables
|
|
foundLogLevel := false
|
|
for _, ev := range envVars {
|
|
if ev.Name == "LOG_LEVEL" {
|
|
foundLogLevel = true
|
|
t.Logf("Found LOG_LEVEL: %s", ev.Description)
|
|
break
|
|
}
|
|
}
|
|
|
|
if !foundLogLevel {
|
|
t.Error("expected to find LOG_LEVEL from hlog")
|
|
}
|
|
}
|