Adds test.

This commit is contained in:
Hyatt 2023-04-09 17:19:11 -05:00
parent dbe56ba613
commit 128fc21efb
Signed by: nhyatt
GPG Key ID: C50D0BBB5BC40BEA
2 changed files with 32 additions and 7 deletions

View File

@ -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)

View File

@ -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
}