config: fail fast when DNSWATCHER_TARGETS is empty
All checks were successful
check / check (push) Successful in 45s
All checks were successful
check / check (push) Successful in 45s
When DNSWATCHER_TARGETS is empty or unset (the default), dnswatcher now exits with a clear error message instead of silently starting with nothing to monitor. Added ErrNoTargets sentinel error returned from config.New when both domains and hostnames lists are empty after target classification. This causes the fx application to fail to start, preventing silent misconfiguration. Also extracted classifyAndValidateTargets and parseDurationOrDefault helper functions to keep buildConfig within the funlen limit. Closes #69
This commit is contained in:
87
internal/config/config_test.go
Normal file
87
internal/config/config_test.go
Normal file
@@ -0,0 +1,87 @@
|
||||
package config_test
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"go.uber.org/fx"
|
||||
|
||||
"sneak.berlin/go/dnswatcher/internal/config"
|
||||
"sneak.berlin/go/dnswatcher/internal/globals"
|
||||
"sneak.berlin/go/dnswatcher/internal/logger"
|
||||
)
|
||||
|
||||
func TestNewReturnsErrNoTargetsWhenEmpty(t *testing.T) {
|
||||
// Cannot use t.Parallel() because t.Setenv modifies the process
|
||||
// environment.
|
||||
t.Setenv("DNSWATCHER_TARGETS", "")
|
||||
t.Setenv("DNSWATCHER_DATA_DIR", t.TempDir())
|
||||
|
||||
var cfg *config.Config
|
||||
|
||||
app := fx.New(
|
||||
fx.Provide(
|
||||
func() *globals.Globals {
|
||||
return &globals.Globals{
|
||||
Appname: "dnswatcher-test-empty",
|
||||
}
|
||||
},
|
||||
logger.New,
|
||||
config.New,
|
||||
),
|
||||
fx.Populate(&cfg),
|
||||
fx.NopLogger,
|
||||
)
|
||||
|
||||
err := app.Err()
|
||||
if err == nil {
|
||||
t.Fatal(
|
||||
"expected error when DNSWATCHER_TARGETS is empty, got nil",
|
||||
)
|
||||
}
|
||||
|
||||
if !errors.Is(err, config.ErrNoTargets) {
|
||||
t.Errorf("expected ErrNoTargets, got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewSucceedsWithTargets(t *testing.T) {
|
||||
// Cannot use t.Parallel() because t.Setenv modifies the process
|
||||
// environment.
|
||||
t.Setenv("DNSWATCHER_TARGETS", "example.com")
|
||||
t.Setenv("DNSWATCHER_DATA_DIR", t.TempDir())
|
||||
|
||||
// Prevent loading a local config file by changing to a temp dir.
|
||||
t.Chdir(t.TempDir())
|
||||
|
||||
var cfg *config.Config
|
||||
|
||||
app := fx.New(
|
||||
fx.Provide(
|
||||
func() *globals.Globals {
|
||||
return &globals.Globals{
|
||||
Appname: "dnswatcher-test-ok",
|
||||
}
|
||||
},
|
||||
logger.New,
|
||||
config.New,
|
||||
),
|
||||
fx.Populate(&cfg),
|
||||
fx.NopLogger,
|
||||
)
|
||||
|
||||
err := app.Err()
|
||||
if err != nil {
|
||||
t.Fatalf(
|
||||
"expected no error with valid targets, got: %v",
|
||||
err,
|
||||
)
|
||||
}
|
||||
|
||||
if len(cfg.Domains) != 1 || cfg.Domains[0] != "example.com" {
|
||||
t.Errorf(
|
||||
"expected [example.com], got domains=%v",
|
||||
cfg.Domains,
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user