429 lines
14 KiB
Go
429 lines
14 KiB
Go
package configcmd
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"bytes"
|
||
|
|
"errors"
|
||
|
|
"os"
|
||
|
|
"path/filepath"
|
||
|
|
"strings"
|
||
|
|
"testing"
|
||
|
|
|
||
|
|
"gopkg.in/yaml.v3"
|
||
|
|
coreconfig "yetano.dev/yetano/core/config"
|
||
|
|
"yetano.dev/yetano/core/paths"
|
||
|
|
|
||
|
|
"yetano.dev/yetano/core"
|
||
|
|
)
|
||
|
|
|
||
|
|
const fullTemplate = `# yamase-test full configuration.
|
||
|
|
|
||
|
|
# Minimum log level: debug, info, warn, error.
|
||
|
|
log-level: info
|
||
|
|
|
||
|
|
listen:
|
||
|
|
- address: "0.0.0.0:25"
|
||
|
|
tls: false
|
||
|
|
|
||
|
|
limits:
|
||
|
|
# Largest message accepted, in bytes.
|
||
|
|
max-message-size-bytes: 52428800
|
||
|
|
# Not yet enforced.
|
||
|
|
connection-rate-per-source: 20
|
||
|
|
|
||
|
|
storage:
|
||
|
|
queue:
|
||
|
|
# Only "filesystem" is implemented.
|
||
|
|
driver: filesystem
|
||
|
|
root: /var/lib/yamase/queue
|
||
|
|
|
||
|
|
tls:
|
||
|
|
cert-file: /etc/yamase/tls/test.crt
|
||
|
|
key-file: /etc/yamase/tls/test.key
|
||
|
|
`
|
||
|
|
|
||
|
|
const minimalTemplate = `listen:
|
||
|
|
- address: "0.0.0.0:25"
|
||
|
|
storage:
|
||
|
|
queue:
|
||
|
|
driver: filesystem
|
||
|
|
root: /var/lib/yamase/queue
|
||
|
|
`
|
||
|
|
|
||
|
|
type schema struct {
|
||
|
|
LogLevel string `yaml:"log-level"`
|
||
|
|
Listen []struct {
|
||
|
|
Address string `yaml:"address"`
|
||
|
|
TLS string `yaml:"tls"`
|
||
|
|
} `yaml:"listen"`
|
||
|
|
Limits struct {
|
||
|
|
MaxMessageSizeBytes int64 `yaml:"max-message-size-bytes"`
|
||
|
|
ConnectionRate int `yaml:"connection-rate-per-source"`
|
||
|
|
} `yaml:"limits"`
|
||
|
|
Storage struct {
|
||
|
|
Queue struct {
|
||
|
|
Driver string `yaml:"driver"`
|
||
|
|
Root string `yaml:"root"`
|
||
|
|
} `yaml:"queue"`
|
||
|
|
} `yaml:"storage"`
|
||
|
|
TLS *struct {
|
||
|
|
CertFile string `yaml:"cert-file"`
|
||
|
|
KeyFile string `yaml:"key-file"`
|
||
|
|
} `yaml:"tls"`
|
||
|
|
}
|
||
|
|
|
||
|
|
func validate(path string) error {
|
||
|
|
data, err := os.ReadFile(path)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
var s schema
|
||
|
|
if err := yaml.Unmarshal(data, &s); err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
if len(s.Listen) == 0 {
|
||
|
|
return errors.New("listen: at least one listener is required")
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// testSpec returns a Spec for a simulated Linux host rooted at root,
|
||
|
|
// with the working directory at root/work.
|
||
|
|
func testSpec(root string) Spec {
|
||
|
|
return Spec{
|
||
|
|
Name: "yamase-test",
|
||
|
|
App: core.Application{Name: "yamase", Kind: core.Service},
|
||
|
|
Minimal: minimalTemplate,
|
||
|
|
Full: fullTemplate,
|
||
|
|
Validate: validate,
|
||
|
|
Schema: schema{},
|
||
|
|
Options: []coreconfig.Option{
|
||
|
|
coreconfig.WithEnvironment(map[string]string{"HOME": "/home/test"}),
|
||
|
|
coreconfig.WithPathsOptions(paths.WithPlatform("linux"), paths.WithRoot(root)),
|
||
|
|
coreconfig.WithWorkdirPath(filepath.Join(root, "work")),
|
||
|
|
},
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
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 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), 0o644); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestUsage(t *testing.T) {
|
||
|
|
s := testSpec(t.TempDir())
|
||
|
|
if code, _, _ := run(t, s, ""); code != ExitUsage {
|
||
|
|
t.Errorf("no subcommand: exit %d, want %d", code, ExitUsage)
|
||
|
|
}
|
||
|
|
if code, _, _ := run(t, s, "", "frobnicate"); code != ExitUsage {
|
||
|
|
t.Errorf("unknown subcommand: exit %d, want %d", code, ExitUsage)
|
||
|
|
}
|
||
|
|
if code, _, _ := run(t, s, "", "validate", "extra"); code != ExitUsage {
|
||
|
|
t.Errorf("positional argument: exit %d, want %d", code, ExitUsage)
|
||
|
|
}
|
||
|
|
if code, _, _ := run(t, s, "", "create", "--minimal", "--full"); code != ExitUsage {
|
||
|
|
t.Errorf("--minimal --full: exit %d, want %d", code, ExitUsage)
|
||
|
|
}
|
||
|
|
if code, out, _ := run(t, s, "", "help"); code != ExitOK || !strings.Contains(out, "upgrade") {
|
||
|
|
t.Errorf("help: exit %d, output %q", code, out)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestCreateDefaultsToUserConfigDir(t *testing.T) {
|
||
|
|
if os.Geteuid() == 0 {
|
||
|
|
t.Skip("as root, create defaults to the system config dir")
|
||
|
|
}
|
||
|
|
root := t.TempDir()
|
||
|
|
s := testSpec(root)
|
||
|
|
code, _, errOut := run(t, s, "", "create")
|
||
|
|
if code != ExitOK {
|
||
|
|
t.Fatalf("create: exit %d: %s", code, errOut)
|
||
|
|
}
|
||
|
|
want := filepath.Join(root, "home", "test", ".config", "yamase", "yamase-test.yaml")
|
||
|
|
got, err := os.ReadFile(want)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatalf("create did not write %s: %v", want, err)
|
||
|
|
}
|
||
|
|
if string(got) != minimalTemplate {
|
||
|
|
t.Errorf("create wrote %q, want the minimal template", got)
|
||
|
|
}
|
||
|
|
|
||
|
|
// The file now exists: a second create refuses, --force overwrites.
|
||
|
|
if code, _, _ := run(t, s, "", "create", "--full"); code != ExitProblem {
|
||
|
|
t.Errorf("create over an existing file: exit %d, want %d", code, ExitProblem)
|
||
|
|
}
|
||
|
|
if code, _, errOut := run(t, s, "", "create", "--full", "--force"); code != ExitOK {
|
||
|
|
t.Fatalf("create --force: exit %d: %s", code, errOut)
|
||
|
|
}
|
||
|
|
if got, _ := os.ReadFile(want); string(got) != fullTemplate {
|
||
|
|
t.Errorf("create --full --force did not write the full template")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestCreateExplicitAndOutput(t *testing.T) {
|
||
|
|
root := t.TempDir()
|
||
|
|
s := testSpec(root)
|
||
|
|
explicit := filepath.Join(root, "x", "explicit.yaml")
|
||
|
|
if code, _, errOut := run(t, s, explicit, "create"); code != ExitOK {
|
||
|
|
t.Fatalf("create --config: exit %d: %s", code, errOut)
|
||
|
|
}
|
||
|
|
if _, err := os.Stat(explicit); err != nil {
|
||
|
|
t.Errorf("create --config did not write %s: %v", explicit, err)
|
||
|
|
}
|
||
|
|
output := filepath.Join(root, "out.yaml")
|
||
|
|
if code, _, errOut := run(t, s, explicit, "create", "--output", output, "--full"); code != ExitOK {
|
||
|
|
t.Fatalf("create --output: exit %d: %s", code, errOut)
|
||
|
|
}
|
||
|
|
if got, _ := os.ReadFile(output); string(got) != fullTemplate {
|
||
|
|
t.Errorf("create --output --full did not write the full template to --output")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestValidate(t *testing.T) {
|
||
|
|
root := t.TempDir()
|
||
|
|
s := testSpec(root)
|
||
|
|
if code, _, errOut := run(t, s, "", "validate"); code != ExitProblem || !strings.Contains(errOut, "no configuration file found") {
|
||
|
|
t.Errorf("validate without a file: exit %d, stderr %q", code, errOut)
|
||
|
|
}
|
||
|
|
file := filepath.Join(root, "work", "yamase-test.yaml")
|
||
|
|
writeFile(t, file, "log-level: info\n")
|
||
|
|
if code, _, errOut := run(t, s, "", "validate"); code != ExitProblem || !strings.Contains(errOut, "listen") {
|
||
|
|
t.Errorf("validate invalid file: exit %d, stderr %q", code, errOut)
|
||
|
|
}
|
||
|
|
writeFile(t, file, minimalTemplate)
|
||
|
|
if code, out, errOut := run(t, s, "", "validate"); code != ExitOK || !strings.Contains(out, file) {
|
||
|
|
t.Errorf("validate valid file: exit %d, stdout %q, stderr %q", code, out, errOut)
|
||
|
|
}
|
||
|
|
// An unknown option fails validate, with its path and line, though
|
||
|
|
// the program itself would start (it ignores the key).
|
||
|
|
writeFile(t, file, minimalTemplate+"log-levle: debug\n")
|
||
|
|
if code, out, errOut := run(t, s, "", "validate"); code != ExitProblem || !strings.Contains(errOut, "unknown option log-levle (line") || strings.Contains(out, "is valid") {
|
||
|
|
t.Errorf("validate with an unknown option: exit %d, stdout %q, stderr %q", code, out, errOut)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestPathNeverCreates(t *testing.T) {
|
||
|
|
root := t.TempDir()
|
||
|
|
s := testSpec(root)
|
||
|
|
code, out, _ := run(t, s, "", "path")
|
||
|
|
if code != ExitOK || !strings.Contains(out, "no configuration file found") {
|
||
|
|
t.Errorf("path: exit %d, output %q", code, out)
|
||
|
|
}
|
||
|
|
for _, f := range []string{"work/yamase-test.yaml", "etc/yamase/yamase-test.yaml"} {
|
||
|
|
if !strings.Contains(out, filepath.Join(root, f)) {
|
||
|
|
t.Errorf("path output does not list %s:\n%s", f, out)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
entries, _ := os.ReadDir(root)
|
||
|
|
if len(entries) != 0 {
|
||
|
|
t.Errorf("path created %d entries under the root", len(entries))
|
||
|
|
}
|
||
|
|
|
||
|
|
writeFile(t, filepath.Join(root, "etc", "yamase", "yamase-test.yaml"), minimalTemplate)
|
||
|
|
if _, out, _ := run(t, s, "", "path"); !strings.Contains(out, "* "+filepath.Join(root, "etc", "yamase", "yamase-test.yaml")) {
|
||
|
|
t.Errorf("path does not mark the used file:\n%s", out)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestUpgradePreviewWriteIdempotent(t *testing.T) {
|
||
|
|
root := t.TempDir()
|
||
|
|
s := testSpec(root)
|
||
|
|
file := filepath.Join(root, "work", "yamase-test.yaml")
|
||
|
|
original := minimalTemplate + "limits:\n max-message-size-bytes: 1000\n"
|
||
|
|
writeFile(t, file, original)
|
||
|
|
|
||
|
|
code, out, errOut := run(t, s, "", "upgrade")
|
||
|
|
if code != ExitOK {
|
||
|
|
t.Fatalf("upgrade: exit %d: %s", code, errOut)
|
||
|
|
}
|
||
|
|
for _, k := range []string{"log-level", "limits.connection-rate-per-source", "tls"} {
|
||
|
|
if !strings.Contains(out, k) {
|
||
|
|
t.Errorf("upgrade preview does not mention %s:\n%s", k, out)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
for _, k := range []string{"storage.queue", "listen[]", "limits.max-message-size-bytes"} {
|
||
|
|
if strings.Contains(out, "# --- "+k) {
|
||
|
|
t.Errorf("upgrade preview offers %s, which the file already has:\n%s", k, out)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if got, _ := os.ReadFile(file); string(got) != original {
|
||
|
|
t.Fatal("upgrade without --write changed the file")
|
||
|
|
}
|
||
|
|
|
||
|
|
if code, _, errOut := run(t, s, "", "upgrade", "--write"); code != ExitOK {
|
||
|
|
t.Fatalf("upgrade --write: exit %d: %s", code, errOut)
|
||
|
|
}
|
||
|
|
upgraded, _ := os.ReadFile(file)
|
||
|
|
|
||
|
|
// The result still loads, with the same effective values: the
|
||
|
|
// additions are comments only.
|
||
|
|
var before, after schema
|
||
|
|
if err := yaml.Unmarshal([]byte(original), &before); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if err := yaml.Unmarshal(upgraded, &after); err != nil {
|
||
|
|
t.Fatalf("upgraded file does not parse: %v\n%s", err, upgraded)
|
||
|
|
}
|
||
|
|
if !equalYAML(t, before, after) {
|
||
|
|
t.Errorf("upgrade changed the effective configuration:\n%s", upgraded)
|
||
|
|
}
|
||
|
|
if !strings.Contains(string(upgraded), " # Not yet enforced.\n # connection-rate-per-source: 20\n") {
|
||
|
|
t.Errorf("connection-rate-per-source not added into limits: with its comment:\n%s", upgraded)
|
||
|
|
}
|
||
|
|
|
||
|
|
// Running again finds nothing and changes nothing.
|
||
|
|
code, out, _ = run(t, s, "", "upgrade", "--write")
|
||
|
|
if code != ExitOK || !strings.Contains(out, "already mentions every option") {
|
||
|
|
t.Errorf("second upgrade: exit %d, output %q", code, out)
|
||
|
|
}
|
||
|
|
if again, _ := os.ReadFile(file); !bytes.Equal(again, upgraded) {
|
||
|
|
t.Errorf("second upgrade changed the file:\n%s", again)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func equalYAML(t *testing.T, a, b any) bool {
|
||
|
|
t.Helper()
|
||
|
|
ya, _ := yaml.Marshal(a)
|
||
|
|
yb, _ := yaml.Marshal(b)
|
||
|
|
return bytes.Equal(ya, yb)
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestMissingRespectsCommentsAndNonMappings(t *testing.T) {
|
||
|
|
file := `listen:
|
||
|
|
- address: ":25"
|
||
|
|
# log-level: debug
|
||
|
|
tls:
|
||
|
|
storage:
|
||
|
|
queue: {driver: filesystem}
|
||
|
|
limits:
|
||
|
|
max-message-size-bytes: 1
|
||
|
|
# connection-rate-per-source: 5
|
||
|
|
`
|
||
|
|
adds, err := Missing([]byte(file), []byte(fullTemplate), schema{})
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if len(adds) != 0 {
|
||
|
|
t.Errorf("Missing() = %+v, want nothing (commented, empty and flow values count as present)", adds)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestMissingEmptyFile(t *testing.T) {
|
||
|
|
adds, err := Missing([]byte("# nothing yet\n"), []byte(fullTemplate), nil)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if len(adds) != 5 {
|
||
|
|
t.Errorf("Missing() on an empty file = %d additions, want every top-level option (5)", len(adds))
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestMissingRejectsNonMapping(t *testing.T) {
|
||
|
|
if _, err := Missing([]byte("- a\n- b\n"), []byte(fullTemplate), nil); err == nil {
|
||
|
|
t.Error("Missing() on a top-level list succeeded, want an error")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestUndocumentedKeys(t *testing.T) {
|
||
|
|
got, err := UndocumentedKeys(fullTemplate, schema{})
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if len(got) != 0 {
|
||
|
|
t.Errorf("UndocumentedKeys(full) = %v, want none", got)
|
||
|
|
}
|
||
|
|
got, _ = UndocumentedKeys(minimalTemplate, schema{})
|
||
|
|
want := "log-level listen[].tls limits tls"
|
||
|
|
if strings.Join(got, " ") != want {
|
||
|
|
t.Errorf("UndocumentedKeys(minimal) = %v, want %s", got, want)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestMissingDoesNotOfferEntriesOfOperatorNamedMaps(t *testing.T) {
|
||
|
|
type backend struct {
|
||
|
|
Driver string `yaml:"driver"`
|
||
|
|
Root string `yaml:"root"`
|
||
|
|
}
|
||
|
|
type ctl struct {
|
||
|
|
Backends map[string]backend `yaml:"backends"`
|
||
|
|
}
|
||
|
|
tmpl := "backends:\n inbound:\n driver: filesystem\n root: /q\n"
|
||
|
|
file := "backends:\n queue:\n driver: filesystem\n root: /x\n"
|
||
|
|
adds, err := Missing([]byte(file), []byte(tmpl), ctl{})
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if len(adds) != 0 {
|
||
|
|
t.Errorf("Missing() with schema = %+v, want nothing (backends names are operator data)", adds)
|
||
|
|
}
|
||
|
|
// Without the schema the template's example entry is offered.
|
||
|
|
if adds, _ := Missing([]byte(file), []byte(tmpl), nil); len(adds) != 1 || adds[0].Key != "backends.inbound" {
|
||
|
|
t.Errorf("Missing() without schema = %+v, want backends.inbound", adds)
|
||
|
|
}
|
||
|
|
// A file without the map at all is offered the whole block.
|
||
|
|
if adds, _ := Missing([]byte("{}\n"), []byte(tmpl), ctl{}); len(adds) != 1 || adds[0].Key != "backends" {
|
||
|
|
t.Errorf("Missing() on a file without backends = %+v, want backends", adds)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestOptionalWithoutFile(t *testing.T) {
|
||
|
|
root := t.TempDir()
|
||
|
|
s := testSpec(root)
|
||
|
|
s.Optional = true
|
||
|
|
s.Validate = func(path string) error {
|
||
|
|
if path != "" {
|
||
|
|
t.Errorf("Validate got %q, want the defaults (\"\")", path)
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
code, out, errOut := run(t, s, "", "validate")
|
||
|
|
if code != ExitOK || !strings.Contains(out, "built-in defaults") {
|
||
|
|
t.Fatalf("validate: code %d, out %q, err %q", code, out, errOut)
|
||
|
|
}
|
||
|
|
code, out, _ = run(t, s, "", "upgrade")
|
||
|
|
if code != ExitOK || !strings.Contains(out, "nothing to upgrade") {
|
||
|
|
t.Fatalf("upgrade: code %d, out %q", code, out)
|
||
|
|
}
|
||
|
|
// An explicit --config that does not exist is still an error.
|
||
|
|
if code, _, _ = run(t, s, filepath.Join(root, "nope.yaml"), "validate"); code != ExitProblem {
|
||
|
|
t.Fatalf("validate --config missing: code %d, want %d", code, ExitProblem)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestNone(t *testing.T) {
|
||
|
|
s := Spec{Name: "yatool", None: true}
|
||
|
|
for _, sub := range Subcommands {
|
||
|
|
code, out, _ := run(t, s, "", sub)
|
||
|
|
if code != ExitOK || out != "yatool has no configuration file\n" {
|
||
|
|
t.Errorf("%s: code %d, out %q", sub, code, out)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if code, _, _ := run(t, s, "", "bogus"); code != ExitUsage {
|
||
|
|
t.Errorf("bogus: code %d, want %d", code, ExitUsage)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestFilenameAndFind(t *testing.T) {
|
||
|
|
root := t.TempDir()
|
||
|
|
s := testSpec(root)
|
||
|
|
s.Filename = "custom.yml"
|
||
|
|
if _, err := s.Find(""); !errors.Is(err, ErrNotFound) {
|
||
|
|
t.Fatalf("Find without file: %v, want ErrNotFound", err)
|
||
|
|
}
|
||
|
|
writeFile(t, filepath.Join(root, "work", "custom.yml"), minimalTemplate)
|
||
|
|
l, err := s.Find("")
|
||
|
|
if err != nil || filepath.Base(l.Path) != "custom.yml" {
|
||
|
|
t.Fatalf("Find: %v, %v", l.Path, err)
|
||
|
|
}
|
||
|
|
}
|