From c27285f224d4eb32dde681852d4284e40ab60722 Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Tue, 10 Nov 2026 08:58:20 +0000 Subject: [PATCH] types: remove viper Updates #1926 --- cmd/headscale/cli/check_test.go | 4 - cmd/headscale/cli/root_test.go | 6 - flakehashes.json | 4 +- go.mod | 5 - go.sum | 10 -- hscontrol/db/db_test.go | 4 - hscontrol/types/config_keys.go | 2 +- hscontrol/types/config_source.go | 15 ++- hscontrol/types/config_source_test.go | 179 ++++---------------------- 9 files changed, 34 insertions(+), 195 deletions(-) diff --git a/cmd/headscale/cli/check_test.go b/cmd/headscale/cli/check_test.go index 491947599..ea33f20df 100644 --- a/cmd/headscale/cli/check_test.go +++ b/cmd/headscale/cli/check_test.go @@ -10,7 +10,6 @@ import ( policyv2 "github.com/juanfont/headscale/hscontrol/policy/v2" "github.com/spf13/cobra" - "github.com/spf13/viper" "github.com/stretchr/testify/require" ) @@ -149,9 +148,6 @@ func writeFile(t *testing.T, dir, name, content string) string { func runCheck(t *testing.T, c *cobra.Command, file string) (string, error) { t.Helper() - viper.Reset() - t.Cleanup(viper.Reset) - cmd := &cobra.Command{} cmd.Flags().String("file", file, "") diff --git a/cmd/headscale/cli/root_test.go b/cmd/headscale/cli/root_test.go index cc1eb600f..cb651eaea 100644 --- a/cmd/headscale/cli/root_test.go +++ b/cmd/headscale/cli/root_test.go @@ -10,7 +10,6 @@ import ( "github.com/juanfont/headscale/hscontrol/types" "github.com/rs/zerolog" "github.com/spf13/cobra" - "github.com/spf13/viper" "github.com/stretchr/testify/require" ) @@ -349,9 +348,6 @@ func TestOfflineSetPolicyRunsTests(t *testing.T) { }, } { t.Run(tt.name, func(t *testing.T) { - viper.Reset() - t.Cleanup(viper.Reset) - dir := t.TempDir() configPath := filepath.Join(dir, "config.yaml") config := fmt.Sprintf(`server_url: https://headscale.example.com @@ -456,8 +452,6 @@ func TestLoadConfig(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - viper.Reset() - t.Cleanup(viper.Reset) t.Setenv("HEADSCALE_CONFIG", tt.env) old := cfgFile diff --git a/flakehashes.json b/flakehashes.json index c78ac8cee..95db3a9f2 100644 --- a/flakehashes.json +++ b/flakehashes.json @@ -1,6 +1,6 @@ { "vendor": { - "goModSum": "sha256-1vBREh4bGi6Pwb3XB2YPm9aJVZmHxN5eCVrcmN44RKI=", - "sri": "sha256-PJNsVZs5twIo7PNAbZL9dfPHT6vAfe1iUZjUTR0ULc0=" + "goModSum": "sha256-jT09QcBZYC/45UW2H9CUiun/gYjVJqFcpTQ8Cf2Jamg=", + "sri": "sha256-FlwgM94RjLO2Ct7ljZDsYrNh23/Obb3RwxjoE1oAndQ=" } } diff --git a/go.mod b/go.mod index 57ff02381..73a04364f 100644 --- a/go.mod +++ b/go.mod @@ -52,7 +52,6 @@ require ( github.com/sasha-s/go-deadlock v0.3.9 github.com/spf13/cast v1.10.0 github.com/spf13/cobra v1.10.2 - github.com/spf13/viper v1.21.0 github.com/stretchr/testify v1.12.1 github.com/tailscale/hujson v0.0.0-20260727124030-b80ff77dac4f github.com/tailscale/squibble v0.0.0-20260831233316-9a4c8961e6f5 @@ -212,7 +211,6 @@ require ( github.com/opencontainers/image-spec v1.1.1 // indirect github.com/opencontainers/runc v1.3.2 // indirect github.com/pandatix/go-abnf v0.5.1 // indirect - github.com/pelletier/go-toml/v2 v2.4.3 // indirect github.com/peterbourgon/ff/v3 v3.4.0 // indirect github.com/petermattis/goid v0.0.0-20260330135022-df67b199bc81 // indirect github.com/pires/go-proxyproto v0.15.0 // indirect @@ -221,12 +219,9 @@ require ( github.com/prometheus/procfs v0.22.0 // indirect github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect github.com/safchain/ethtool v0.7.0 // indirect - github.com/sagikazarmark/locafero v0.12.0 // indirect github.com/sirupsen/logrus v1.10.2 // indirect github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e // indirect - github.com/spf13/afero v1.15.0 // indirect github.com/spf13/pflag v1.0.10 // indirect - github.com/subosito/gotenv v1.6.0 // indirect github.com/tailscale/certstore v0.1.1-0.20260409135935-3638fb84b77d // indirect github.com/tailscale/go-winio v0.0.0-20231025203758-c4f33415bf55 // indirect github.com/tailscale/peercred v0.0.0-20250107143737-35a0c7bd7edc // indirect diff --git a/go.sum b/go.sum index 00c0cfc61..863d797c2 100644 --- a/go.sum +++ b/go.sum @@ -431,8 +431,6 @@ github.com/ory/dockertest/v3 v3.12.0 h1:3oV9d0sDzlSQfHtIaB5k6ghUCVMVLpAY8hwrqoCy github.com/ory/dockertest/v3 v3.12.0/go.mod h1:aKNDTva3cp8dwOWwb9cWuX84aH5akkxXRvO7KCwWVjE= github.com/pandatix/go-abnf v0.5.1 h1:iyLvSu2Aq12iE0r4n948Qw21BHDuckLq/8WIXAAF+HI= github.com/pandatix/go-abnf v0.5.1/go.mod h1:VBmbtpFgQNEaQpyAvmi+Ecd9wAYJKzA3TmiB3L93sVg= -github.com/pelletier/go-toml/v2 v2.4.3 h1:GTRvJQutkOSftxIFD5xw9aepkYNuPWmVJpffdDPYVpY= -github.com/pelletier/go-toml/v2 v2.4.3/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY= github.com/peterbourgon/ff/v3 v3.4.0 h1:QBvM/rizZM1cB0p0lGMdmR7HxZeI/ZrBWB4DqLkMUBc= github.com/peterbourgon/ff/v3 v3.4.0/go.mod h1:zjJVUhx+twciwfDl0zBcFzl4dW8axCRyXE/eKY9RztQ= github.com/petermattis/goid v0.0.0-20250813065127-a731cc31b4fe/go.mod h1:pxMtw7cyUw6B2bRH0ZBANSPg+AoSud1I1iyJHI69jH4= @@ -474,8 +472,6 @@ github.com/rs/zerolog v1.35.1/go.mod h1:EjML9kdfa/RMA7h/6z6pYmq1ykOuA8/mjWaEvGI+ github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/safchain/ethtool v0.7.0 h1:rlJzfDetsVvT61uz8x1YIcFn12akMfuPulHtZjtb7Is= github.com/safchain/ethtool v0.7.0/go.mod h1:MenQKEjXdfkjD3mp2QdCk8B/hwvkrlOTm/FD4gTpFxQ= -github.com/sagikazarmark/locafero v0.12.0 h1:/NQhBAkUb4+fH1jivKHWusDYFjMOOKU88eegjfxfHb4= -github.com/sagikazarmark/locafero v0.12.0/go.mod h1:sZh36u/YSZ918v0Io+U9ogLYQJ9tLLBmM4eneO6WwsI= github.com/samber/lo v1.53.0 h1:t975lj2py4kJPQ6haz1QMgtId2gtmfktACxIXArw3HM= github.com/samber/lo v1.53.0/go.mod h1:4+MXEGsJzbKGaUEQFKBq2xtfuznW9oz/WrgyzMzRoM0= github.com/sasha-s/go-deadlock v0.3.9 h1:fiaT9rB7g5sr5ddNZvlwheclN9IP86eFW9WgqlEQV+w= @@ -486,8 +482,6 @@ github.com/sirupsen/logrus v1.10.2 h1:G2SED73/qrAu6YwbdxOD6peLkCBI3z7L+ykJFTXJBB github.com/sirupsen/logrus v1.10.2/go.mod h1:SLEg8TqYulVKKfIGHldVp2K2aYz2DKSVBq4g/H5bR7Q= github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e h1:MRM5ITcdelLK2j1vwZ3Je0FKVCfqOLp5zO6trqMLYs0= github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e/go.mod h1:XV66xRDqSt+GTGFMVlhk3ULuV0y9ZmzeVGR4mloJI3M= -github.com/spf13/afero v1.15.0 h1:b/YBCLWAJdFWJTN9cLhiXXcD7mzKn9Dm86dNnfyQw1I= -github.com/spf13/afero v1.15.0/go.mod h1:NC2ByUVxtQs4b3sIUphxK0NioZnmxgyCrfzeuq8lxMg= github.com/spf13/cast v1.10.0 h1:h2x0u2shc1QuLHfxi+cTJvs30+ZAHOGRic8uyGTDWxY= github.com/spf13/cast v1.10.0/go.mod h1:jNfB8QC9IA6ZuY2ZjDp0KtFO2LZZlg4S/7bzP6qqeHo= github.com/spf13/cobra v1.10.2 h1:DMTTonx5m65Ic0GOoRY2c16WCbHxOOw6xxezuLaBpcU= @@ -495,8 +489,6 @@ github.com/spf13/cobra v1.10.2/go.mod h1:7C1pvHqHw5A4vrJfjNwvOdzYu0Gml16OCs2GRiT github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk= github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= -github.com/spf13/viper v1.21.0 h1:x5S+0EU27Lbphp4UKm1C+1oQO+rKx36vfCoaVebLFSU= -github.com/spf13/viper v1.21.0/go.mod h1:P0lhsswPGWD/1lZJ9ny3fYnVqxiegrlNrEmgLjbTCAY= github.com/spkg/bom v0.0.0-20160624110644-59b7046e48ad/go.mod h1:qLr4V1qq6nMqFKkMo8ZTx3f+BZEkzsRUY10Xsm2mwU0= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= @@ -508,8 +500,6 @@ github.com/stretchr/testify v1.12.1 h1:EuwCh5fleGS7H32xRwO3wRGT7DxrDhLAT6FF8MpWD github.com/stretchr/testify v1.12.1/go.mod h1:MDEgiDPPsNp5cuIrHPPCyornHKgEVbtFUmoNlxoYthg= github.com/studio-b12/gowebdav v0.13.0 h1:OcwSg6IQHOFNdYHn3bPOHwSE8looG8N56Y5xTT1asqQ= github.com/studio-b12/gowebdav v0.13.0/go.mod h1:bHA7t77X/QFExdeAnDzK6vKM34kEZAcE1OX4MfiwjkE= -github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8= -github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU= github.com/tailscale/certstore v0.1.1-0.20260409135935-3638fb84b77d h1:JcGKBZAL7ePLwOhUdN8qGQZlP5GueEiIZwY7R62pejE= github.com/tailscale/certstore v0.1.1-0.20260409135935-3638fb84b77d/go.mod h1:XrBNfAFN+pwoWuksbFS9Ccxnopa15zJGgXRFN90l3K4= github.com/tailscale/gliderssh v0.3.4-0.20261026144310-31f7cf365d3a h1:LQfS++EBVN2Bk9/FbFjJmdP/CsebU0OupOX58tO4p04= diff --git a/hscontrol/db/db_test.go b/hscontrol/db/db_test.go index ed4211ae5..d2a43b7a3 100644 --- a/hscontrol/db/db_test.go +++ b/hscontrol/db/db_test.go @@ -11,7 +11,6 @@ import ( "time" "github.com/juanfont/headscale/hscontrol/types" - "github.com/spf13/viper" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "gorm.io/gorm" @@ -24,9 +23,6 @@ import ( const legacyPlaintextKey = "plaintextlegacykey0000000000000000000000000000" func TestSQLiteSameQueryInAndOutsideTransaction(t *testing.T) { - viper.Reset() - t.Cleanup(viper.Reset) - configPath := filepath.Join(t.TempDir(), "config.yaml") require.NoError(t, os.WriteFile(configPath, []byte(` server_url: https://example.com diff --git a/hscontrol/types/config_keys.go b/hscontrol/types/config_keys.go index bc080942b..cddb2d3a8 100644 --- a/hscontrol/types/config_keys.go +++ b/hscontrol/types/config_keys.go @@ -9,7 +9,7 @@ import ( // keyPosition returns the 1-based position of the key at path in src. // JSON and hujson.Standardize output are valid YAML, so one parser serves all. -// Keys match case-insensitively, as viper lowercased them. +// Keys match case-insensitively, as the loader lowercases them. func keyPosition(src []byte, path []string) (int, int, bool) { var doc yaml.Node if yaml.Unmarshal(src, &doc) != nil || len(doc.Content) == 0 { diff --git a/hscontrol/types/config_source.go b/hscontrol/types/config_source.go index 8524d3289..d58919ee1 100644 --- a/hscontrol/types/config_source.go +++ b/hscontrol/types/config_source.go @@ -42,7 +42,8 @@ var reservedEnvNames = []string{ } // configSource is the layered configuration: defaults, then file, then -// environment. Keys are lowercase; values coerce like viper's. +// environment. Keys are lowercase; accessors coerce values, so a string from +// the environment reads as a bool, number or duration. type configSource struct { k *koanf.Koanf file string @@ -134,8 +135,8 @@ func configParser(file string) (koanf.Parser, error) { return nil, fmt.Errorf("unsupported config file format %q: use .json, .yaml, .yml or .hujson", filepath.Ext(file)) //nolint:err113 } -// normaliseConfigMap lowercases keys at every depth and drops nulls, as -// viper does, so lookups are case-insensitive and null means unset. +// normaliseConfigMap lowercases keys at every depth and drops nulls, so +// lookups are case-insensitive and null means unset. func normaliseConfigMap(m map[string]any) map[string]any { out := make(map[string]any, len(m)) @@ -535,7 +536,7 @@ func (s *configSource) loadEnv(environ []string) error { for _, kv := range environ { name, val, _ := strings.Cut(kv, "=") - // Empty means unset, as with viper. + // Empty means unset, so a blank variable never overrides the file. if val != "" && strings.HasPrefix(name, configEnvPrefix) { env[name] = val } @@ -683,7 +684,8 @@ func (s *configSource) StringMapStringSlice(key string) map[string][]string { return cast.ToStringMapStringSlice(s.Get(key)) } -// UnmarshalKey decodes key into out with viper's decoder settings. +// UnmarshalKey decodes key into out, weakly typed: values from the +// environment are strings. func (s *configSource) UnmarshalKey(key string, out any) error { if !s.IsSet(key) { return nil @@ -716,8 +718,7 @@ type keyEntry struct { func (e keyEntry) spelling() string { return strings.Join(e.names, " > ") } // checkOverlaps rejects a path given both dotted and nested ("log.level" next -// to "log: {level: ...}"): viper resolved such pairs by lookup order, which -// nothing here reproduces. +// to "log: {level: ...}"): no rule says which of the two should win. func (s *configSource) checkOverlaps(v *configValidator, m map[string]any) { var entries []keyEntry diff --git a/hscontrol/types/config_source_test.go b/hscontrol/types/config_source_test.go index 5b102b035..1229fcb31 100644 --- a/hscontrol/types/config_source_test.go +++ b/hscontrol/types/config_source_test.go @@ -2,21 +2,17 @@ package types import ( "cmp" - "errors" "go/ast" "go/parser" "go/token" "os" "path/filepath" - "regexp" "slices" "strconv" "strings" "testing" "time" - "github.com/spf13/cast" - "github.com/spf13/viper" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -367,127 +363,27 @@ func TestConfigSourceSearch(t *testing.T) { } func TestConfigSourceUnmarshalKey(t *testing.T) { - p := writeConfig(t, "config.yaml", "dns:\n extra_records:\n - name: a.example.com\n type: A\n value: 1.2.3.4\n") - s, err := newConfigSource(p, true, nil) - require.NoError(t, err) - - var recs []struct { - Name, Type, Value string - } - - require.NoError(t, s.UnmarshalKey("dns.extra_records", &recs)) - require.Len(t, recs, 1) - assert.Equal(t, "a.example.com", recs[0].Name) -} - -// parityRejected lists the fixtures the source must reject, with every key it -// must name. All other fixtures must load. -var parityRejected = func() map[string][]string { - core := []string{ - "acme_email", "acme_url", "database.gorm.prepare_stmt", "grpc_allow_insecure", "grpc_listen_addr", - "tls_cert_path", "tls_key_path", "tls_letsencrypt_cache_dir", "tls_letsencrypt_challenge_type", - "tls_letsencrypt_hostname", "tls_letsencrypt_listen", - } - mid := append([]string{"ephemeral_node_inactivity_timeout", "randomize_client_port"}, core...) - - return map[string][]string{ - "example-v0.23.0": append([]string{"dns.use_username_in_magic_dns"}, mid...), - "example-v0.24.0": mid, - "example-v0.25.0": mid, - "example-v0.26.0": mid, - "example-v0.27.0": mid, - "example-v0.28.0": mid, - "example-v0.29.0": core, - "example-v0.29.7": core, - "hsic-default": {"private_key_path"}, - "hsic-postgres": {"private_key_path"}, - "testdata-policy-path-is-loaded": {"acl_policy_path", "policy.type"}, - "bad-values": {"dns.magic_dsn", "ephemeral_node_inactivity_timeout"}, - "nixos-nixpkgs": { - "tls_letsencrypt_cache_dir", "tls_letsencrypt_challenge_type", "tls_letsencrypt_hostname", "tls_letsencrypt_listen", - }, - "file-everything": {"oidc.client_secret_path"}, - } -}() - -var removedKeyInReason = regexp.MustCompile(`configuration key (\S+) was removed|unknown configuration key (\S+)`) - -// TestConfigSourceParity checks the source against viper on every Phase-0 -// fixture: same set-ness and same value for each coercion. -func TestConfigSourceParity(t *testing.T) { - // The shipped example is the only fixture with every current key and no - // removed ones. - rows := append(compatRows(t), compatRow{name: "config-example", file: "../../config-example.yaml"}) - - unsetHeadscaleEnv(t) - - for _, tt := range rows { - t.Run(tt.name, func(t *testing.T) { - dir := stageCompat(t, tt.file, tt.envFile) - loadViper(t, dir) - - s, err := newConfigSource(dir, false, os.Environ()) - - if want, rejected := parityRejected[tt.name]; rejected { - require.Error(t, err) - - var got []string - - for _, ce := range ConfigErrors(err) { - m := removedKeyInReason.FindStringSubmatch(ce.Reason) - require.NotNil(t, m, ce.Reason) - - got = append(got, m[1]+m[2]) - } - - assert.ElementsMatch(t, want, got) - - return - } - - require.NoError(t, err) - - for _, key := range configKeys { - v := viper.Get(key) - - assert.Equalf(t, viper.IsSet(key), s.IsSet(key), "IsSet %s", key) - assert.Equalf(t, cast.ToString(v), s.String(key), "String %s", key) - assert.Equalf(t, cast.ToBool(v), s.Bool(key), "Bool %s", key) - assert.Equalf(t, cast.ToInt(v), s.Int(key), "Int %s", key) - assert.Equalf(t, cast.ToDuration(v), s.Duration(key), "Duration %s", key) - assert.Equalf(t, cast.ToStringSlice(v), s.StringSlice(key), "StringSlice %s", key) - assert.Equalf(t, cast.ToStringMapString(v), s.StringMapString(key), "StringMapString %s", key) - assert.Equalf(t, cast.ToStringMapStringSlice(v), s.StringMapStringSlice(key), "StringMapStringSlice %s", key) - } - }) - } -} - -// TestConfigSourceUnmarshalParity checks UnmarshalKey against viper's decoder. -func TestConfigSourceUnmarshalParity(t *testing.T) { type rec struct{ Name, Type, Value string } - for _, content := range []string{ - "dns:\n extra_records: \"\"\n", - "dns:\n extra_records:\n - {name: a.example.com, type: A, value: 1.2.3.4}\n", - } { - dir := t.TempDir() - require.NoError(t, os.WriteFile(filepath.Join(dir, "config.yaml"), []byte(content), 0o600)) + tests := []struct { + name, file string + want []rec + }{ + // An empty string decodes to an empty, non-nil slice. + {"empty string", "dns:\n extra_records: \"\"\n", []rec{}}, + {"list", "dns:\n extra_records:\n - {name: a.example.com, type: A, value: 1.2.3.4}\n", []rec{{"a.example.com", "A", "1.2.3.4"}}}, + } - loadViper(t, dir) + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + s, err := newConfigSource(writeConfig(t, "config.yaml", tt.file), true, nil) + require.NoError(t, err) - var want []rec + var got []rec - wantErr := viper.UnmarshalKey("dns.extra_records", &want) - - s, err := newConfigSource(dir, false, nil) - require.NoError(t, err) - - var got []rec - - gotErr := s.UnmarshalKey("dns.extra_records", &got) - require.Equal(t, wantErr == nil, gotErr == nil, "viper: %v, source: %v", wantErr, gotErr) - assert.Equal(t, want, got) + require.NoError(t, s.UnmarshalKey("dns.extra_records", &got)) + assert.Equal(t, tt.want, got) + }) } } @@ -552,30 +448,6 @@ func TestConfigKeysComplete(t *testing.T) { } } -// loadViper loads the config file in dir as the viper loader did, for the -// parity checks. -func loadViper(t *testing.T, dir string) { - t.Helper() - viper.Reset() - t.Cleanup(viper.Reset) - - viper.SetConfigName("config") - viper.AddConfigPath(dir) - - viper.SetEnvPrefix("headscale") - viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) - viper.AutomaticEnv() - - for k, v := range configDefaults { - viper.SetDefault(k, v) - } - - err := viper.ReadInConfig() - if _, ok := errors.AsType[viper.ConfigFileNotFoundError](err); !ok { - require.NoError(t, err) - } -} - // TestConfigSourceDottedOverlap reloads the file each time: map iteration // order differs per load, so a single load could hide a nondeterministic result. func TestConfigSourceDottedOverlap(t *testing.T) { @@ -607,14 +479,13 @@ func TestConfigSourceDottedKeyAlone(t *testing.T) { dir := t.TempDir() require.NoError(t, os.WriteFile(filepath.Join(dir, "config.yaml"), []byte("log.level: debug\ndns:\n magic_dns: false\ndns.nameservers.global: [1.1.1.1]\n"), 0o600)) - loadViper(t, dir) for range 50 { s, err := newConfigSource(dir, false, nil) require.NoError(t, err) - require.Equal(t, viper.GetString("log.level"), s.String("log.level")) - require.Equal(t, viper.GetBool("dns.magic_dns"), s.Bool("dns.magic_dns")) - require.Equal(t, viper.GetStringSlice("dns.nameservers.global"), s.StringSlice("dns.nameservers.global")) + require.Equal(t, "debug", s.String("log.level")) + require.False(t, s.Bool("dns.magic_dns")) + require.Equal(t, []string{"1.1.1.1"}, s.StringSlice("dns.nameservers.global")) } } @@ -706,27 +577,23 @@ func TestConfigSourceStrictBeforeExpansion(t *testing.T) { } } -func TestConfigSourceProviderEnvParity(t *testing.T) { +func TestConfigSourceProviderEnv(t *testing.T) { dir := t.TempDir() require.NoError(t, os.WriteFile(filepath.Join(dir, "config.yaml"), []byte("dns:\n challenge:\n provider:\n name: file\n other: x\n"), 0o600)) t.Setenv("HEADSCALE_DNS_CHALLENGE_PROVIDER", `{"api_token":"x"}`) t.Setenv("HEADSCALE_DNS_CHALLENGE_PROVIDER_NAME", "cloudflare") - loadViper(t, dir) s, err := newConfigSource(dir, false, os.Environ()) require.NoError(t, err) assert.Empty(t, s.Warnings()) - assert.Equal(t, viper.GetString("dns.challenge.provider.name"), s.String("dns.challenge.provider.name")) + assert.Equal(t, "cloudflare", s.String("dns.challenge.provider.name")) - // The env value replaces the file's map, as in viper; the per-field + // The env value replaces the file's map; the per-field // variable is layered on top of it. got := s.StringMap("dns.challenge.provider") - for k, v := range viper.GetStringMap("dns.challenge.provider") { - assert.Equal(t, v, got[k]) - } - + assert.Equal(t, "x", got["api_token"]) assert.NotContains(t, got, "other") }