test: add tests for globals, healthcheck and logger (closes #110)
check / check (push) Failing after 0s
check / check (push) Failing after 0s
Tests for the three packages that had none, written from outside each package, each able to fail on a plausible break: - globals: values set are read back through New, and New returns an independent copy. One sequential test function with a disclosed paralleltest suppression, because it changes shared package variables. - healthcheck: Check returns status "ok", the documented JSON fields, an RFC3339Nano timestamp, the maintenance flag from config in both states, and version and appname from globals. - logger: New gives a usable *slog.Logger, debug output is off by default and EnableDebugLogging turns it on. No production code changed. The terminal output format is not asserted. Model: opus-4-8
This commit was merged in pull request #154.
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
package globals_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"sneak.berlin/go/dnswatcher/internal/globals"
|
||||
)
|
||||
|
||||
// TestGlobals exercises the package-level version and appname
|
||||
// variables through their setters and read-back via New. These are
|
||||
// shared package state, so the test mutates a global and must run
|
||||
// sequentially; it cannot use t.Parallel().
|
||||
//
|
||||
//nolint:paralleltest // mutates shared package-level globals, must run sequentially
|
||||
func TestGlobals(t *testing.T) {
|
||||
versions := []string{"v1.2.3", "dev", "", "v1.2.3-4-gabcdef"}
|
||||
for _, want := range versions {
|
||||
globals.SetVersion(want)
|
||||
|
||||
g, err := globals.New(nil)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, want, g.Version,
|
||||
"New must surface the version set by SetVersion")
|
||||
}
|
||||
|
||||
names := []string{"dnswatcher", "other", ""}
|
||||
for _, want := range names {
|
||||
globals.SetAppname(want)
|
||||
|
||||
g, err := globals.New(nil)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, want, g.Appname,
|
||||
"New must surface the appname set by SetAppname")
|
||||
}
|
||||
|
||||
// New returns a snapshot: a later SetVersion must not mutate a
|
||||
// Globals handed out earlier.
|
||||
globals.SetVersion("first")
|
||||
|
||||
g, err := globals.New(nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
globals.SetVersion("second")
|
||||
assert.Equal(t, "first", g.Version,
|
||||
"a Globals returned by New must not change when the "+
|
||||
"package variable is set again")
|
||||
}
|
||||
Reference in New Issue
Block a user