gpaste/config_test.go
Torjus Håkestad 41e82fb21e
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
Add config test and update from env
2022-01-19 00:38:25 +01:00

85 lines
1.7 KiB
Go

package gpaste_test
import (
"os"
"strings"
"testing"
"git.t-juice.club/torjus/gpaste"
"github.com/google/go-cmp/cmp"
)
func TestServerConfig(t *testing.T) {
t.Run("FromReader", func(t *testing.T) {
clearEnv()
simpleConfig := `
LogLevel = "INFO"
URL = "http://paste.example.org"
ListenAddr = ":8080"
[Store]
Type = "memory"
`
expected := &gpaste.ServerConfig{
LogLevel: "INFO",
URL: "http://paste.example.org",
ListenAddr: ":8080",
Store: &gpaste.ServerStoreConfig{
Type: "memory",
},
}
sr := strings.NewReader(simpleConfig)
c, err := gpaste.ServerConfigFromReader(sr)
if err != nil {
t.Fatalf("Error parsing config: %s", err)
}
if !cmp.Equal(c, expected) {
t.Errorf("Result does not match: %s", cmp.Diff(c, expected))
}
})
t.Run("FromEnv", func(t *testing.T) {
clearEnv()
var envMap map[string]string = map[string]string{
"GPASTE_LOGLEVEL": "DEBUG",
"GPASTE_URL": "http://gpaste.example.org",
"GPASTE_STORE_TYPE": "memory",
"GPASTE_LISTENADDR": ":8000",
}
expected := &gpaste.ServerConfig{
LogLevel: "DEBUG",
URL: "http://gpaste.example.org",
ListenAddr: ":8000",
Store: &gpaste.ServerStoreConfig{
Type: "memory",
},
}
for k, v := range envMap {
os.Setenv(k, v)
}
sr := strings.NewReader("")
c, err := gpaste.ServerConfigFromReader(sr)
if err != nil {
t.Fatalf("Error parsing empty config")
}
if !cmp.Equal(c, expected) {
t.Errorf("Result does not match: %s", cmp.Diff(c, expected))
}
})
}
func clearEnv() {
for _, env := range os.Environ() {
result := strings.Split(env, "=")
value := result[0]
if strings.Contains(value, "GPASTE_") {
os.Unsetenv(value)
}
}
}