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.RTMPListenAddr != ":5566" {
|
|
t.Errorf("Unexpected RTMPListenAddr: %s", c.RTMPListenAddr)
|
|
}
|
|
if c.LogLevel != "INFO" {
|
|
t.Errorf("Unexpected LogLevel: %s", c.LogLevel)
|
|
}
|
|
})
|
|
|
|
t.Run("TestWithValidValues", func(t *testing.T) {
|
|
configString := `
|
|
# Random comment
|
|
LogLevel = "DEBUG"
|
|
|
|
RTMPListenAddr = ":5555"
|
|
`
|
|
sr := strings.NewReader(configString)
|
|
|
|
c, err := config.FromReader(sr)
|
|
if err != nil {
|
|
t.Fatalf("Error reading config: %s", err)
|
|
}
|
|
|
|
if c.RTMPListenAddr != ":5555" {
|
|
t.Errorf("Unexpected RTMPListenAddr: %s", c.RTMPListenAddr)
|
|
}
|
|
|
|
if c.LogLevel != "DEBUG" {
|
|
t.Errorf("Unexpected LogLevel: %s", c.LogLevel)
|
|
}
|
|
})
|
|
t.Run("TestWithInValidValues", func(t *testing.T) {
|
|
configString := `
|
|
# Random comment
|
|
LogLevel = "INVALID"
|
|
|
|
RTMPListenAddr = ":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_RTMPLISTENADDR": ":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.RTMPListenAddr != envValues["DOGTAMER_RTMPLISTENADDR"] {
|
|
t.Errorf("RTMPListenAddr has wrong value: %s", c.RTMPListenAddr)
|
|
}
|
|
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_RTMPLISTENADDR": ":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")
|
|
}
|
|
})
|
|
}
|