127 lines
2.8 KiB
Go
127 lines
2.8 KiB
Go
package config_test
|
|
|
|
import (
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.uio.no/torjus/dogtamer/config"
|
|
)
|
|
|
|
func TestConfig(t *testing.T) {
|
|
t.Run("TestEmpty", func(t *testing.T) {
|
|
emptyConfig := ""
|
|
sr := strings.NewReader(emptyConfig)
|
|
|
|
c, err := config.FromReader(sr)
|
|
if err != nil {
|
|
t.Fatalf("Error reading empty config: %s", err)
|
|
}
|
|
|
|
// Ensure proper defaults are set
|
|
if c.ListenAddr != ":5566" {
|
|
t.Errorf("Unexpected ListenAddr: %s", c.ListenAddr)
|
|
}
|
|
if c.LogLevel != "INFO" {
|
|
t.Errorf("Unexpected LogLevel: %s", c.LogLevel)
|
|
}
|
|
})
|
|
|
|
t.Run("TestWithValidValues", func(t *testing.T) {
|
|
configString := `
|
|
# Random comment
|
|
LogLevel = "DEBUG"
|
|
|
|
ListenAddr = ":5555"
|
|
`
|
|
sr := strings.NewReader(configString)
|
|
|
|
c, err := config.FromReader(sr)
|
|
if err != nil {
|
|
t.Fatalf("Error reading config: %s", err)
|
|
}
|
|
|
|
if c.ListenAddr != ":5555" {
|
|
t.Errorf("Unexpected ListenAddr: %s", c.ListenAddr)
|
|
}
|
|
|
|
if c.LogLevel != "DEBUG" {
|
|
t.Errorf("Unexpected LogLevel: %s", c.LogLevel)
|
|
}
|
|
})
|
|
t.Run("TestWithInValidValues", func(t *testing.T) {
|
|
configString := `
|
|
# Random comment
|
|
LogLevel = "INVALID"
|
|
|
|
ListenAddr = ":5555"
|
|
`
|
|
sr := strings.NewReader(configString)
|
|
|
|
_, err := config.FromReader(sr)
|
|
ive, ok := err.(*config.InvalidValueError)
|
|
if !ok {
|
|
t.Fatalf("Error is of wrong type: %T", ive)
|
|
}
|
|
if ive.Key != "LogLevel" {
|
|
t.Errorf("Error has wrong key: %s", ive.Key)
|
|
}
|
|
})
|
|
t.Run("TestFromEnv", func(t *testing.T) {
|
|
os.Clearenv()
|
|
|
|
envValues := map[string]string{
|
|
"DOGTAMER_LOGLEVEL": "DEBUG",
|
|
"DOGTAMER_LISTENADDR": ":3333",
|
|
}
|
|
|
|
for k, v := range envValues {
|
|
if err := os.Setenv(k, v); err != nil {
|
|
t.Fatalf("Unable to set env values: %s", err)
|
|
}
|
|
}
|
|
|
|
emptyCfg := ""
|
|
sr := strings.NewReader(emptyCfg)
|
|
|
|
c, err := config.FromReader(sr)
|
|
if err != nil {
|
|
t.Fatalf("Error parsing config string: %s", err)
|
|
}
|
|
if err := c.UpdateFromEnv(); err != nil {
|
|
t.Errorf("Error updating config with environment")
|
|
}
|
|
|
|
if c.ListenAddr != envValues["DOGTAMER_LISTENADDR"] {
|
|
t.Errorf("ListenAddr has wrong value: %s", c.ListenAddr)
|
|
}
|
|
if c.LogLevel != envValues["DOGTAMER_LOGLEVEL"] {
|
|
t.Errorf("LogLevel has wrong value: %s", c.LogLevel)
|
|
}
|
|
})
|
|
t.Run("TestFromEnvInvalid", func(t *testing.T) {
|
|
os.Clearenv()
|
|
|
|
envValues := map[string]string{
|
|
"DOGTAMER_LOGLEVEL": "TEST",
|
|
"DOGTAMER_LISTENADDR": ":3333",
|
|
}
|
|
|
|
for k, v := range envValues {
|
|
if err := os.Setenv(k, v); err != nil {
|
|
t.Fatalf("Unable to set env values: %s", err)
|
|
}
|
|
}
|
|
emptyCfg := ""
|
|
sr := strings.NewReader(emptyCfg)
|
|
|
|
c, err := config.FromReader(sr)
|
|
if err != nil {
|
|
t.Fatalf("Error parsing config string: %s", err)
|
|
}
|
|
if err := c.UpdateFromEnv(); err == nil {
|
|
t.Errorf("No error when parsing invalid env values")
|
|
}
|
|
})
|
|
}
|