package configfamily import ( "bytes" "errors" "os" "path/filepath" "strings" "testing" "gopkg.in/yaml.v3" coreconfig "yetano.dev/yetano/core/config" "yetano.dev/yetano/core/doctor" "yetano.dev/yetano/core/paths" ) type sharedSchema struct { Storage struct { Path string `yaml:"path"` } `yaml:"storage"` LogLevel string `yaml:"log-level"` } type webSchema struct { Listen string `yaml:"listen"` Storage struct { Path string `yaml:"path"` } `yaml:"storage"` } var family = Family{ Name: "fam", Programs: []string{"web", "authd"}, Shared: sharedSchema{}, SharedTemplate: "# shared\nlog-level: info\n", } func testOptions(root string) []coreconfig.Option { return []coreconfig.Option{ coreconfig.WithEnvironment(map[string]string{"HOME": "/home/test"}), coreconfig.WithPathsOptions(paths.WithPlatform("linux"), paths.WithRoot(root)), } } func testSpec(root string) Spec { return Spec{ Program: family.Service("fam-web"), Optional: true, Minimal: "listen: \":8080\"\n", Full: "listen: \":8080\"\nstorage:\n path: \"\"\n", Validate: func(merged []byte) error { var w webSchema if err := yaml.Unmarshal(merged, &w); err != nil { return err } if w.Listen == "bad" { return errors.New("listen is bad") } return nil }, Schema: webSchema{}, Options: testOptions(root), } } func writeFile(t *testing.T, path, content string) { t.Helper() if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { t.Fatal(err) } if err := os.WriteFile(path, []byte(content), 0o600); err != nil { t.Fatal(err) } } func dirs(root string) (system, user string) { return filepath.Join(root, "etc", "fam"), filepath.Join(root, "home", "test", ".config", "fam") } func TestLayersOrder(t *testing.T) { root := t.TempDir() system, user := dirs(root) writeFile(t, filepath.Join(user, "fam.yaml"), "web:\n listen: \":1\"\n") explicit := filepath.Join(root, "explicit.yaml") writeFile(t, explicit, "listen: \":2\"\n") layers, err := Layers(family.Service("fam-web"), explicit, testOptions(root)...) if err != nil { t.Fatal(err) } want := []struct { kind LayerKind path string ok bool }{ {ExplicitLayer, explicit, true}, {ProgramLayer, filepath.Join(user, "fam-web.yaml"), false}, {SectionLayer, filepath.Join(user, "fam.yaml"), true}, {SharedLayer, filepath.Join(user, "fam.yaml"), true}, {ProgramLayer, filepath.Join(system, "fam-web.yaml"), false}, {SectionLayer, filepath.Join(system, "fam.yaml"), false}, {SharedLayer, filepath.Join(system, "fam.yaml"), false}, } if len(layers) != len(want) { t.Fatalf("got %d layers, want %d: %+v", len(layers), len(want), layers) } for i, w := range want { l := layers[i] if l.Kind != w.kind || l.Path != w.path || l.Exists != w.ok { t.Errorf("layer %d = %+v, want kind %d path %s exists %v", i, l, w.kind, w.path, w.ok) } } if _, err := Layers(family.Service("fam-web"), filepath.Join(root, "missing.yaml"), testOptions(root)...); err == nil { t.Error("a missing --config file is not an error") } } func TestLoadMergesByPriority(t *testing.T) { root := t.TempDir() system, user := dirs(root) writeFile(t, filepath.Join(system, "fam.yaml"), "log-level: info\nstorage:\n path: /var/lib/fam/db\nweb:\n listen: \":1\"\nauthd:\n listen: \":9\"\n") writeFile(t, filepath.Join(system, "fam-web.yaml"), "listen: \":2\"\n") writeFile(t, filepath.Join(user, "fam.yaml"), "log-level: debug\n") l, err := Load(family.Service("fam-web"), "", testOptions(root)...) if err != nil { t.Fatal(err) } if got := l.Merged["listen"]; got != ":2" { t.Errorf("listen = %v, want the program file's :2 over the section's :1", got) } if got := l.Merged["log-level"]; got != "debug" { t.Errorf("log-level = %v, want the user tier's debug", got) } if _, ok := l.Merged["authd"]; ok { t.Error("another program's section leaked into the merged configuration") } if _, ok := l.Merged["web"]; ok { t.Error("the program's own section was merged as a key instead of its content") } if got := l.Origins["listen"]; got != filepath.Join(system, "fam-web.yaml") { t.Errorf("origin of listen = %q", got) } if !strings.Contains(l.Describe(), "[web]") { t.Errorf("Describe() = %q, want the section layer named", l.Describe()) } } func TestLoadWithoutFiles(t *testing.T) { _, err := Load(family.Service("fam-web"), "", testOptions(t.TempDir())...) if !errors.Is(err, ErrNotFound) { t.Errorf("Load = %v, want ErrNotFound", err) } } func TestUnknown(t *testing.T) { root := t.TempDir() system, _ := dirs(root) // log-level is a shared option fam-web does not read: valid // everywhere. typo is unknown in every layer; other programs' // sections are not fam-web's business. writeFile(t, filepath.Join(system, "fam.yaml"), "log-level: info\ntypo1: x\nweb:\n listen: \":1\"\n typo2: x\nauthd:\n anything: x\n") writeFile(t, filepath.Join(system, "fam-web.yaml"), "listen: \":2\"\nlog-level: debug\ntypo3: x\n") l, err := Load(family.Service("fam-web"), "", testOptions(root)...) if err != nil { t.Fatal(err) } unknown, err := l.Unknown(webSchema{}) if err != nil { t.Fatal(err) } got := map[string]bool{} for _, k := range unknown { got[k.Key] = true } for _, want := range []string{"typo1", "web.typo2", "typo3"} { if !got[want] { t.Errorf("unknown options %v lack %s", unknown, want) } } if len(unknown) != 3 { t.Errorf("unknown options = %v, want exactly typo1, web.typo2, typo3", unknown) } } func run(t *testing.T, s Spec, explicit string, args ...string) (int, string, string) { t.Helper() var out, errOut bytes.Buffer code := Run(s, explicit, args, &out, &errOut) return code, out.String(), errOut.String() } func TestConfigCommandOptionalWithoutFiles(t *testing.T) { s := testSpec(t.TempDir()) if code, out, errOut := run(t, s, "", "validate"); code != 0 || !strings.Contains(out, "built-in defaults") { t.Errorf("validate: exit %d, out %q, err %q", code, out, errOut) } if code, out, _ := run(t, s, "", "path"); code != 0 || !strings.Contains(out, "fam-web.yaml") || !strings.Contains(out, "fam.yaml [web]") { t.Errorf("path: exit %d, out %q", code, out) } r := doctor.Report{Program: "fam-web"} if l := Doctor(&r, s, ""); l != nil { t.Error("Doctor returned a configuration without any file") } if r.Problems() != 0 { t.Errorf("Doctor reports problems for an optional program without a file: %+v", r) } s.Optional = false if code, _, _ := run(t, s, "", "validate"); code != 1 { t.Errorf("validate of a required configuration without files: exit %d, want 1", code) } } func TestConfigCommandValidateAndCreate(t *testing.T) { root := t.TempDir() system, user := dirs(root) s := testSpec(root) if code, out, errOut := run(t, s, "", "create"); code != 0 || !strings.Contains(out, filepath.Join(user, "fam-web.yaml")) { t.Fatalf("create: exit %d, out %q, err %q", code, out, errOut) } if code, out, errOut := run(t, s, "", "create", "--shared"); code != 0 || !strings.Contains(out, filepath.Join(user, "fam.yaml")) { t.Fatalf("create --shared: exit %d, out %q, err %q", code, out, errOut) } if code, _, _ := run(t, s, "", "create", "--shared"); code != 1 { t.Errorf("create --shared over an existing file: exit %d, want 1", code) } if code, out, errOut := run(t, s, "", "validate"); code != 0 || !strings.Contains(out, "valid") { t.Errorf("validate: exit %d, out %q, err %q", code, out, errOut) } writeFile(t, filepath.Join(system, "fam-web.yaml"), "listen: bad\n") writeFile(t, filepath.Join(user, "fam-web.yaml"), "storage:\n path: /x\n") if code, _, errOut := run(t, s, "", "validate"); code != 1 || !strings.Contains(errOut, "listen is bad") { t.Errorf("validate of an invalid merge: exit %d, err %q", code, errOut) } } func TestSection(t *testing.T) { if got := family.Service("fam-authd").Section(); got != "authd" { t.Errorf("Section() = %q", got) } if got := family.CLI("fam-ctl").Filename(); got != "fam-ctl.yaml" { t.Errorf("Filename() = %q", got) } }