2022-01-18 23:38:25 +00:00
|
|
|
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"
|
2022-01-19 21:28:08 +00:00
|
|
|
SigningSecret = "abc999"
|
2022-01-18 23:38:25 +00:00
|
|
|
|
|
|
|
[Store]
|
2022-01-19 00:03:24 +00:00
|
|
|
Type = "fs"
|
|
|
|
[Store.FS]
|
|
|
|
Dir = "/tmp"
|
2022-01-18 23:38:25 +00:00
|
|
|
`
|
|
|
|
expected := &gpaste.ServerConfig{
|
2022-01-19 21:28:08 +00:00
|
|
|
LogLevel: "INFO",
|
|
|
|
URL: "http://paste.example.org",
|
|
|
|
ListenAddr: ":8080",
|
|
|
|
SigningSecret: "abc999",
|
2022-01-18 23:38:25 +00:00
|
|
|
Store: &gpaste.ServerStoreConfig{
|
2022-01-19 00:03:24 +00:00
|
|
|
Type: "fs",
|
|
|
|
FS: &gpaste.ServerStoreFSStoreConfig{
|
|
|
|
Dir: "/tmp",
|
|
|
|
},
|
2022-01-18 23:38:25 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
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{
|
2022-01-19 21:28:08 +00:00
|
|
|
"GPASTE_LOGLEVEL": "DEBUG",
|
|
|
|
"GPASTE_URL": "http://gpaste.example.org",
|
|
|
|
"GPASTE_STORE_TYPE": "fs",
|
|
|
|
"GPASTE_LISTENADDR": ":8000",
|
|
|
|
"GPASTE_SIGNINGSECRET": "test1345",
|
|
|
|
"GPASTE_STORE_FS_DIR": "/tmp",
|
2022-01-18 23:38:25 +00:00
|
|
|
}
|
|
|
|
expected := &gpaste.ServerConfig{
|
2022-01-19 21:28:08 +00:00
|
|
|
LogLevel: "DEBUG",
|
|
|
|
URL: "http://gpaste.example.org",
|
|
|
|
ListenAddr: ":8000",
|
|
|
|
SigningSecret: "test1345",
|
2022-01-18 23:38:25 +00:00
|
|
|
Store: &gpaste.ServerStoreConfig{
|
2022-01-19 00:03:24 +00:00
|
|
|
Type: "fs",
|
|
|
|
FS: &gpaste.ServerStoreFSStoreConfig{
|
|
|
|
Dir: "/tmp",
|
|
|
|
},
|
2022-01-18 23:38:25 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|