Files
steward/cmd/steward/main_test.go
T
abrown38andClaude Opus 5.5 3c0808591f Steward 0.1.0 (in development): a DHCPv4 server with a web UI, API and CLI
Subnets served per interface, pools, reservations, options at three
levels, leases in SQLite saved before the ACK, ping and decline conflict
checks, validated changes with history and undo, users and tokens, an
audit log, a Kea importer, a sandboxed systemd unit and Debian packages.
Tested with real udhcpc and dhcpcd clients in Docker (scripts/e2e.sh).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-14 12:14:07 +01:00

45 lines
1.7 KiB
Go

package main
import (
"testing"
"steward/internal/api"
"steward/internal/model"
)
func TestDurations(t *testing.T) {
for in, want := range map[string]int{"3600": 3600, "12h": 43200, "30m": 1800, "1d": 86400, "1d12h": 129600, "1h30m": 5400} {
if got, err := parseSeconds(in); err != nil || got != want {
t.Errorf("parseSeconds(%q) = %d, %v; want %d", in, got, err, want)
}
}
for _, bad := range []string{"12x", "d", "1d2x"} {
if _, err := parseSeconds(bad); err == nil {
t.Errorf("parseSeconds(%q) accepted", bad)
}
}
for n, want := range map[int]string{43200: "12h", 5400: "1h30m", 90061: "1d1h1m1s", 0: "-"} {
if got := fmtSeconds(n); got != want {
t.Errorf("fmtSeconds(%d) = %q, want %q", n, got, want)
}
}
}
func TestOptions(t *testing.T) {
in, err := optionsArg([]string{"routers=10.0.10.1", "224:string=hi", " ntp-servers = 10.0.10.2 "})
if err != nil || len(in) != 3 || in[1].Option != "224" || in[1].Type != "string" || in[2].Option != "ntp-servers" || in[2].Value != "10.0.10.2" {
t.Fatalf("optionsArg: %+v %v", in, err)
}
if _, err := optionsArg([]string{"routers"}); err == nil {
t.Error("an option without a value accepted")
}
cur := []model.OptionValue{{Code: 3, Name: "routers", Value: "10.0.10.1"}, {Code: 6, Name: "domain-name-servers", Value: "10.0.10.2"}}
got, err := mergeOptions(cur, []api.OptionInput{{Option: "routers", Value: "10.0.10.254"}}, []string{"domain-name-servers"})
if err != nil || len(got) != 1 || got[0].Option != "routers" || got[0].Value != "10.0.10.254" {
t.Errorf("merge: %+v %v", got, err)
}
if _, err := mergeOptions(cur, nil, []string{"ntp-servers"}); err == nil {
t.Error("unsetting an option that isn't set accepted")
}
}