2021-08-23 00:45:44 +00:00
|
|
|
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
|
2021-09-02 00:49:11 +00:00
|
|
|
if c.RTMPListenAddr != ":5566" {
|
|
|
|
t.Errorf("Unexpected RTMPListenAddr: %s", c.RTMPListenAddr)
|
2021-08-23 00:45:44 +00:00
|
|
|
}
|
|
|
|
if c.LogLevel != "INFO" {
|
|
|
|
t.Errorf("Unexpected LogLevel: %s", c.LogLevel)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("TestWithValidValues", func(t *testing.T) {
|
|
|
|
configString := `
|
|
|
|
# Random comment
|
|
|
|
LogLevel = "DEBUG"
|
|
|
|
|
2021-09-02 00:49:11 +00:00
|
|
|
RTMPListenAddr = ":5555"
|
2021-08-23 00:45:44 +00:00
|
|
|
`
|
|
|
|
sr := strings.NewReader(configString)
|
|
|
|
|
|
|
|
c, err := config.FromReader(sr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error reading config: %s", err)
|
|
|
|
}
|
|
|
|
|
2021-09-02 00:49:11 +00:00
|
|
|
if c.RTMPListenAddr != ":5555" {
|
|
|
|
t.Errorf("Unexpected RTMPListenAddr: %s", c.RTMPListenAddr)
|
2021-08-23 00:45:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.LogLevel != "DEBUG" {
|
|
|
|
t.Errorf("Unexpected LogLevel: %s", c.LogLevel)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("TestWithInValidValues", func(t *testing.T) {
|
|
|
|
configString := `
|
|
|
|
# Random comment
|
|
|
|
LogLevel = "INVALID"
|
|
|
|
|
2021-09-02 00:49:11 +00:00
|
|
|
RTMPListenAddr = ":5555"
|
2021-08-23 00:45:44 +00:00
|
|
|
`
|
|
|
|
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{
|
2021-09-02 00:49:11 +00:00
|
|
|
"DOGTAMER_LOGLEVEL": "DEBUG",
|
|
|
|
"DOGTAMER_RTMPLISTENADDR": ":3333",
|
2021-08-23 00:45:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2021-09-02 00:49:11 +00:00
|
|
|
if c.RTMPListenAddr != envValues["DOGTAMER_RTMPLISTENADDR"] {
|
|
|
|
t.Errorf("RTMPListenAddr has wrong value: %s", c.RTMPListenAddr)
|
2021-08-23 00:45:44 +00:00
|
|
|
}
|
|
|
|
if c.LogLevel != envValues["DOGTAMER_LOGLEVEL"] {
|
|
|
|
t.Errorf("LogLevel has wrong value: %s", c.LogLevel)
|
|
|
|
}
|
2021-09-14 21:18:58 +00:00
|
|
|
|
|
|
|
t.Cleanup(func() {
|
|
|
|
for value := range envValues {
|
|
|
|
os.Unsetenv(value)
|
|
|
|
}
|
|
|
|
})
|
2021-08-23 00:45:44 +00:00
|
|
|
})
|
|
|
|
t.Run("TestFromEnvInvalid", func(t *testing.T) {
|
|
|
|
os.Clearenv()
|
|
|
|
|
|
|
|
envValues := map[string]string{
|
2021-09-02 00:49:11 +00:00
|
|
|
"DOGTAMER_LOGLEVEL": "TEST",
|
|
|
|
"DOGTAMER_RTMPLISTENADDR": ":3333",
|
2021-08-23 00:45:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2021-09-19 03:49:23 +00:00
|
|
|
if err == nil {
|
|
|
|
// TODO: Check that it actually returns the correct error
|
2021-08-23 00:45:44 +00:00
|
|
|
t.Fatalf("Error parsing config string: %s", err)
|
|
|
|
}
|
|
|
|
if err := c.UpdateFromEnv(); err == nil {
|
|
|
|
t.Errorf("No error when parsing invalid env values")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|