diff --git a/internal/config/initialize.go b/internal/config/initialize.go index 3ba2d8c..d10abd1 100644 --- a/internal/config/initialize.go +++ b/internal/config/initialize.go @@ -78,7 +78,7 @@ func Init() Config { // read config file configFileData, err := getConfigFileData(cfg.ConfigFile) if err != nil { - log.Fatalf("[FATAL] Unable to read configuration file") + log.Fatalf("[FATAL] Unable to read configuration file: %v", err) } updateValues(&cfg, configFileData) diff --git a/internal/config/initialize_test.go b/internal/config/initialize_test.go index ba2d91a..7e281c0 100644 --- a/internal/config/initialize_test.go +++ b/internal/config/initialize_test.go @@ -1,19 +1,47 @@ package config import ( + "os" "testing" ) +func TestInit(t *testing.T) { + cfg := Init() + if cfg.AllowAdminNoMutateToggle != "7b068a99-c02b-410a-bd59-3514bac85e7a" { + t.Errorf("Init() returned incorrect value for AllowAdminNoMutateToggle, got %v, wanted %v", cfg.AllowAdminNoMutateToggle, "7b068a99-c02b-410a-bd59-3514bac85e7a") + } +} + +func TestGetOSEnv(t *testing.T) { + expected := "foo" + os.Setenv("TEST_ENV", expected) + result := getOSEnv("TEST_ENV", "bar") + + if result != expected { + t.Errorf("getOSEnv() returned incorrect value, got %s, wanted %s", result, expected) + } + + os.Unsetenv("TEST_ENV") + result = getOSEnv("TEST_ENV", expected) + if result != expected { + t.Errorf("getOSEnv() returned incorrect value, got %s, wanted %s", result, expected) + } +} + func TestUpdateValues(t *testing.T) { cfg := Config{ AllowAdminNoMutate: false, AllowAdminNoMutateToggle: "7b068a99-c02b-410a-bd59-3514bac85e7a", DockerhubRegistry: "registry.hub.docker.com", + NameSpace: "ingress-nginx", } cfgFile := configFileStruct{ AllowAdminNoMutate: true, AllowAdminNoMutateToggle: "test-token", DockerhubRegistry: "registry.example.com", + Kubernetes: KubernetesStruct{ + Namespace: "example-namespace", + }, } updateValues(&cfg, cfgFile) @@ -26,13 +54,10 @@ func TestUpdateValues(t *testing.T) { if cfg.DockerhubRegistry != cfgFile.DockerhubRegistry { t.Errorf("updateValues() returned incorrect value for DockerhubRegistry, got %v, wanted %v", cfg.DockerhubRegistry, cfgFile.DockerhubRegistry) } + if cfg.NameSpace != cfgFile.Kubernetes.Namespace { + t.Errorf("updateValues() returned incorrect value for NameSpace, got %v, wanted %v", cfg.NameSpace, cfgFile.Kubernetes.Namespace) + } /* - if cfg.DockerhubRegistry == "registry.hub.docker.com" && configFileData.DockerhubRegistry != "registry.hub.docker.com" { - cfg.DockerhubRegistry = configFileData.DockerhubRegistry - } - if cfg.NameSpace == "ingress-nginx" && configFileData.Kubernetes.Namespace != "ingress-nginx" { - cfg.NameSpace = configFileData.Kubernetes.Namespace - } if cfg.ServiceName == "webhook" && configFileData.Kubernetes.ServiceName != "webhook" { cfg.ServiceName = configFileData.Kubernetes.ServiceName }