configcmd: config show, Doctor, KnownKey and a Layered hook

"config show [--json] [--show-secrets]" prints the configuration with
the origin of every value (secrets redacted), for every program.
Spec.Layered lets a program whose configuration is merged from several
inputs (family file, a section of it, its own file -- the yamase
family) run path, validate and show over all layers, with its own
unknown-option rules; unknown options are warnings there. Doctor(report,
spec, explicit) is the config part of a doctor command. UnknownKey
carries the layer it is in; KnownKey tells whether a dotted key is an
option of a config struct.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
terminarandClaude Opus 5.5 committed 2026-10-25 17:29:19 +01:00
1 parent 185211e304
commit fa52990e3e
7 files changed
+758 -11

No files matched your search

+213
View File
@@ -0,0 +1,213 @@
package configcmd
import (
"bytes"
"encoding/json"
"os"
"path/filepath"
"reflect"
"strings"
"testing"
"gopkg.in/yaml.v3"
coreconfig "yetano.dev/yetano/core/config"
"yetano.dev/yetano/core/doctor"
)
func TestShowSingleFile(t *testing.T) {
root := t.TempDir()
s := testSpec(root)
file := filepath.Join(root, "etc", "yamase", "yamase-test.yaml")
writeFile(t, file, minimalTemplate+"log-level: debug\nunread: 1\n")
code, out, errOut := run(t, s, "", "show")
if code != ExitOK {
t.Fatalf("show: exit %d: %s", code, errOut)
}
for _, want := range []string{
`log-level: "debug"`,
"# " + file,
`root: "/var/lib/yamase/queue"`,
"unread: 1",
"not used by yamase-test",
} {
if !strings.Contains(out, want) {
t.Errorf("show output lacks %q:\n%s", want, out)
}
}
code, out, _ = run(t, s, "", "show", "--json")
var values []Value
if code != ExitOK || json.Unmarshal([]byte(out), &values) != nil || len(values) == 0 {
t.Errorf("show --json: exit %d, output %q", code, out)
}
}
// fakeLayered serves two layers of one directory, as a family-wide file
// plus a program file.
func fakeLayered(dir string) *Layered {
shared := filepath.Join(dir, "family.yaml")
own := filepath.Join(dir, "own.yaml")
layers := func(string) ([]LayerInfo, error) {
return []LayerInfo{
{Name: own, Path: own, Source: "test", Exists: statOK(own)},
{Name: shared, Path: shared, Source: "test", Exists: statOK(shared)},
}, nil
}
return &Layered{
Layers: layers,
Load: func(explicit string) (*Effective, error) {
ls, _ := layers(explicit)
var sources []coreconfig.Source
for i := len(ls) - 1; i >= 0; i-- {
if !ls[i].Exists {
continue
}
data, err := coreconfig.ReadFile(ls[i].Path)
if err != nil {
return nil, err
}
sources = append(sources, coreconfig.Source{Name: ls[i].Name, Data: data})
}
if len(sources) == 0 {
return nil, ErrNotFound
}
merged, origins := coreconfig.Merge(sources...)
return &Effective{Name: "yamase-test", Layers: ls, Merged: merged, Origins: origins}, nil
},
Validate: func(merged []byte) error {
var s schema
if err := yaml.Unmarshal(merged, &s); err != nil {
return err
}
if len(s.Listen) == 0 {
return errNoListener
}
return nil
},
}
}
type testErr string
func (e testErr) Error() string { return string(e) }
const errNoListener = testErr("listen: at least one listener is required")
func statOK(path string) bool { return statExistsForTest(path) }
func TestLayered(t *testing.T) {
root := t.TempDir()
dir := filepath.Join(root, "cfg")
s := testSpec(root)
s.Layered = fakeLayered(dir)
if code, out, _ := run(t, s, "", "path"); code != ExitOK || !strings.Contains(out, "no configuration file found") {
t.Errorf("path without files: exit %d, %q", code, out)
}
if code, _, errOut := run(t, s, "", "validate"); code != ExitProblem || !strings.Contains(errOut, ErrNotFound.Error()) {
t.Errorf("validate without files: exit %d, %q", code, errOut)
}
writeFile(t, filepath.Join(dir, "family.yaml"), "log-level: info\nlisten:\n - address: \":25\"\n")
writeFile(t, filepath.Join(dir, "own.yaml"), "log-level: debug\nlog-levle: x\n")
code, out, _ := run(t, s, "", "path")
if code != ExitOK || strings.Count(out, "(test, used)") != 2 {
t.Errorf("path: exit %d, %q", code, out)
}
// Unknown options are warnings for a layered program.
code, out, errOut := run(t, s, "", "validate")
if code != ExitOK || !strings.Contains(out, "warning: unknown option log-levle ("+filepath.Join(dir, "own.yaml")+", line 2)") || !strings.Contains(out, ": valid") {
t.Errorf("validate: exit %d, stdout %q, stderr %q", code, out, errOut)
}
code, out, _ = run(t, s, "", "show")
if code != ExitOK || !strings.Contains(out, `log-level: "debug"`) || !strings.Contains(out, "# "+filepath.Join(dir, "family.yaml")) {
t.Errorf("show: exit %d:\n%s", code, out)
}
if !strings.Contains(out, "layers, lowest priority first: "+filepath.Join(dir, "family.yaml")+" + "+filepath.Join(dir, "own.yaml")) {
t.Errorf("show header:\n%s", out)
}
writeFile(t, filepath.Join(dir, "family.yaml"), "log-level: info\n")
if code, _, errOut := run(t, s, "", "validate"); code != ExitProblem || !strings.Contains(errOut, "listen") {
t.Errorf("validate of an invalid merge: exit %d, %q", code, errOut)
}
}
func TestDoctor(t *testing.T) {
root := t.TempDir()
s := testSpec(root)
r := doctor.Report{Program: "yamase-test"}
if e := Doctor(&r, s, ""); e != nil {
t.Error("Doctor without a file returned a configuration")
}
writeFile(t, filepath.Join(root, "etc", "yamase", "yamase-test.yaml"), minimalTemplate+"log-levle: x\n")
r = doctor.Report{Program: "yamase-test"}
e := Doctor(&r, s, "")
if e == nil {
t.Fatal("Doctor with a file returned nil")
}
var out bytes.Buffer
r.Run([]string{"--json"}, &out, &out)
if !strings.Contains(out.String(), "log-levle (") || !strings.Contains(out.String(), "config file found") {
t.Errorf("doctor report = %s", out.String())
}
}
func TestValuesRedaction(t *testing.T) {
merged, origins := coreconfig.Merge(coreconfig.Source{Name: "f", Data: map[string]any{
"srs": map[string]any{"secrets": []any{"plain", "env:X", "base64:abc"}},
"yaais": map[string]any{"token": "file:/run/t", "password": "hunter2"},
}})
e := &Effective{Name: "p", Merged: merged, Origins: origins}
got := map[string]any{}
for _, v := range e.Values(nil, false) {
got[v.Key] = v.Value
}
want := map[string]any{
"srs.secrets": []any{"<redacted>", "env:X", "<redacted>"},
"yaais.token": "file:/run/t",
"yaais.password": "<redacted>",
}
if !reflect.DeepEqual(got, want) {
t.Errorf("Values = %v, want %v", got, want)
}
}
type inlineBase struct {
Enabled bool `yaml:"enabled"`
}
func TestKnownKey(t *testing.T) {
type cfg struct {
Throttle struct {
inlineBase `yaml:",inline"`
Window string `yaml:"window"`
} `yaml:"throttle"`
Listen []struct {
Address string `yaml:"address"`
} `yaml:"listen"`
Backends map[string]struct{ Root string } `yaml:"backends"`
Custom selfDecoding `yaml:"custom"`
}
for key, want := range map[string]bool{
"throttle.enabled": true,
"throttle.window": true,
"throttle.nope": false,
"listen[0].address": true,
"listen[].address": true,
"backends.any.root": true,
"backends.any.nope": false,
"custom.anything": true,
"missing": false,
} {
if got := KnownKey(cfg{}, key); got != want {
t.Errorf("KnownKey(%q) = %v, want %v", key, got, want)
}
}
}
func statExistsForTest(path string) bool {
_, err := os.Stat(path)
return err == nil
}