Read every setting through viper (closes #211)
check / check (push) Successful in 3m47s

servewaf's and rproxy's settings are read through viper, bound to the same environment variable names as before, in place of the os.LookupEnv function Main passed down. NewViper turns AutomaticEnv and AllowEmptyEnv on, so a setting that is set but empty reads as set, as it did. A setting may also be given as a file, the _FILE form, which rproxy's settings now have too; rproxy still writes nothing to disk. The settings tests pass set into a viper instead of a lookup function.

Model: deepseek-v4-pro
This commit is contained in:
sneak committed 2026-11-07 05:29:14 +00:00
1 parent 2e2f6f7175
commit 3fa1f4b732
19 files changed
+274 -185

No files matched your search

+31 -18
View File
@@ -9,6 +9,8 @@ import (
"testing"
"time"
"github.com/spf13/viper"
"sneak.berlin/go/smallwebwaf/internal/config"
)
@@ -32,11 +34,12 @@ const (
// rproxyConfig reads rproxy's settings from env.
func rproxyConfig(env map[string]string) (*config.RProxy, error) {
return config.RProxyFromEnvironment(func(name string) (string, bool) {
value, ok := env[name]
v := viper.New()
for name, value := range env {
v.Set(name, value)
}
return value, ok
})
return config.RProxyFromEnvironment(v)
}
func TestRProxyDefaults(t *testing.T) {
@@ -177,31 +180,41 @@ func TestRProxyNeedsItsBackend(t *testing.T) {
}
}
func TestRProxyReadsNoFile(t *testing.T) {
func TestRProxySettingReadFromFile(t *testing.T) {
t.Parallel()
path := filepath.Join(t.TempDir(), "backend")
err := os.WriteFile(path, []byte("http://from-a-file:8080\n"), 0o600)
err := os.WriteFile(path, []byte(backendURL+"\n"), 0o600)
if err != nil {
t.Fatalf("write: %v", err)
}
_, err = rproxyConfig(map[string]string{wafBackend + "_FILE": path})
if err == nil || !strings.HasPrefix(err.Error(), wafBackend+": is required") {
t.Errorf("error %v, want %s still required", err, wafBackend)
}
cfg, err := rproxyConfig(map[string]string{
wafBackend: backendURL,
rproxyTimeout + "_FILE": "/nonexistent/timeout",
})
cfg, err := rproxyConfig(map[string]string{wafBackend + "_FILE": path})
if err != nil {
t.Fatalf("settings: %v, with the file left unread", err)
t.Fatalf("settings: %v", err)
}
if cfg.Timeout != 2*time.Second {
t.Errorf("timeout %v, want the default", cfg.Timeout)
if cfg.WAFBackend.String() != backendURL {
t.Errorf("backend %v, want %s", cfg.WAFBackend, backendURL)
}
}
func TestRProxyUnreadableSettingFileStopsTheStart(t *testing.T) {
t.Parallel()
dir := t.TempDir()
for _, path := range []string{filepath.Join(dir, "missing"), dir} {
_, err := rproxyConfig(map[string]string{
wafBackend: backendURL,
rproxyTimeout + "_FILE": path,
})
want := rproxyTimeout + "_FILE: cannot be read: "
if err == nil || !strings.HasPrefix(err.Error(), want) {
t.Errorf("%s: error %v, want one starting %s", path, err, want)
}
}
}