Files
core/solutions/cli/cli_test.go
T

168 lines
6.8 KiB
Go
Raw Normal View History

package cli
import (
"bytes"
"context"
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"
)
type result struct {
code int
stdout, stderr string
}
func run(t *testing.T, store string, args ...string) result {
t.Helper()
var out, errb bytes.Buffer
code := Run(context.Background(), args, Env{
Stdin: strings.NewReader(""), Stdout: &out, Stderr: &errb,
Program: "yetano-solutions", StoreDir: store, Catalogs: []string{"../engine/testdata/cat"},
Getenv: func(string) string { return "" },
})
return result{code, out.String(), errb.String()}
}
// Scenario D: CI use without a terminal (ACC-D, CLI-03).
func TestNonInteractive(t *testing.T) {
store := t.TempDir()
out := filepath.Join(t.TempDir(), "gen")
// missing answers: exit 2, never a prompt
r := run(t, store, "solution", "setup", "app-run", "--json")
if r.code != ExitUsage || !strings.Contains(r.stdout, `"missing"`) || !strings.Contains(r.stdout, "hosts") {
t.Fatalf("missing: %d %s %s", r.code, r.stdout, r.stderr)
}
// invalid value: exit 2
if r := run(t, store, "solution", "setup", "app-run", "--set", "hosts=bad_host!"); r.code != ExitUsage {
t.Errorf("invalid value: %d %s", r.code, r.stderr)
}
// unknown question: exit 2
if r := run(t, store, "solution", "setup", "app-run", "--set", "nope=1"); r.code != ExitUsage {
t.Errorf("unknown question: %d", r.code)
}
// unknown solution: exit 3
if r := run(t, store, "solution", "show", "nope"); r.code != ExitNotFound {
t.Errorf("unknown solution: %d", r.code)
}
// complete: files written, instance saved
r = run(t, store, "solution", "setup", "app-run", "--set", "hosts=a.example.org", "--name", "prod", "--save", "--output", out)
if r.code != 0 {
t.Fatalf("setup: %d %s", r.code, r.stderr)
}
for _, f := range []string{"solution.yaml", "solution.lock.yaml", "config/app/app.yaml", "deployment-plan.yaml", "handbook/index.md", "architecture/graph.json"} {
if _, err := os.Stat(filepath.Join(out, f)); err != nil {
t.Errorf("missing %s", f)
}
}
// regenerate from the manifest: same files, exit 0
if r := run(t, store, "solution", "generate", "--file", filepath.Join(out, "solution.yaml"), "--output", out, "--locked"); r.code != 0 {
t.Fatalf("regenerate: %d %s", r.code, r.stderr)
}
// a hand edit: exit 4 (conflict)
os.WriteFile(filepath.Join(out, "config/app/app.yaml"), []byte("# mine\n"), 0o644)
if r := run(t, store, "solution", "generate", "--file", filepath.Join(out, "solution.yaml"), "--output", out); r.code != ExitConflict {
t.Errorf("hand edit: %d %s", r.code, r.stderr)
}
// stored instance works by name
if r := run(t, store, "solution", "validate", "prod"); r.code != 0 {
t.Errorf("validate prod: %d %s %s", r.code, r.stdout, r.stderr)
}
r = run(t, store, "solution", "instance", "list", "--json")
var rows []map[string]string
if err := json.Unmarshal([]byte(r.stdout), &rows); err != nil || len(rows) != 1 || rows[0]["status"] != "Configured" {
t.Errorf("instance list: %s %v", r.stdout, err)
}
// preset from it; a new instance needs only the instance-specific answer
if r := run(t, store, "solution", "preset", "save", "mine", "--from", "prod"); r.code != 0 {
t.Fatalf("preset: %d %s", r.code, r.stderr)
}
if r := run(t, store, "solution", "setup", "app-run", "--preset", "mine", "--set", "hosts=b.example.org", "--json"); r.code != 0 {
t.Errorf("setup with preset: %d %s", r.code, r.stderr)
}
// explain
r = run(t, store, "solution", "config", "explain", "prod", "app/app.yaml", "limits.max")
if r.code != 0 || !strings.Contains(r.stdout, "profile:app/base") {
t.Errorf("explain: %d %s %s", r.code, r.stdout, r.stderr)
}
}
func TestStateJSONIsTheAPI(t *testing.T) {
store := t.TempDir()
f := filepath.Join(t.TempDir(), "s.yaml")
os.WriteFile(f, []byte("apiVersion: yetano.dev/v1\nkind: WizardSession\nmetadata: {name: x}\nspec:\n solution: {id: app-run}\n mode: guided\n parameters: {hosts: [a.example.org], identity: central}\n"), 0o644)
r := run(t, store, "solution", "state", "--file", f)
var st map[string]any
if err := json.Unmarshal([]byte(r.stdout), &st); err != nil {
t.Fatalf("%v: %s %s", err, r.stdout, r.stderr)
}
if st["complete"] != false || st["next"] != "realm" {
t.Errorf("state: complete=%v next=%v", st["complete"], st["next"])
}
}
func TestSearchOpenKnowledge(t *testing.T) {
store := t.TempDir()
if r := run(t, store, "solution", "search", "run", "app"); r.code != 0 || !strings.Contains(r.stdout, "yetano:solution/app-run") {
t.Errorf("search: %d %s", r.code, r.stdout)
}
if r := run(t, store, "open", "yetano:solution/app-run"); r.code != 0 {
t.Errorf("open: %d %s", r.code, r.stderr)
}
if r := run(t, store, "open", "yetano:solution/missing"); r.code != ExitNotFound {
t.Errorf("open missing: %d", r.code)
}
if r := run(t, store, "open", "not a ref"); r.code != ExitUsage {
t.Errorf("open bad: %d", r.code)
}
if r := run(t, store, "solution", "catalog", "validate"); r.code != 0 {
t.Errorf("catalog validate: %d %s %s", r.code, r.stdout, r.stderr)
}
if r := run(t, store, "nonsense"); r.code != ExitUsage {
t.Errorf("unknown command: %d", r.code)
}
}
func TestInteractiveWizard(t *testing.T) {
store := t.TempDir()
out := filepath.Join(t.TempDir(), "gen")
var o, e bytes.Buffer
// size: Enter (default small), hosts: a.example.org, then actions: 6 export, 8 quit
in := strings.NewReader("\na.example.org\n6\n8\n")
code := Run(context.Background(), []string{"solution", "setup", "app-run", "--output", out}, Env{
Stdin: in, Stdout: &o, Stderr: &e, Interactive: true, Program: "yetano-solutions", StoreDir: store,
Catalogs: []string{"../engine/testdata/cat"}, Getenv: func(string) string { return "" },
})
if code != 0 {
t.Fatalf("%d\n%s\n%s", code, o.String(), e.String())
}
for _, want := range []string{"[1/2] How big?", "(recommended)", "YOUR SOLUTION", "Actions:"} {
if !strings.Contains(o.String(), want) {
t.Errorf("output lacks %q:\n%s", want, o.String())
}
}
if _, err := os.Stat(filepath.Join(out, "config/app/app.yaml")); err != nil {
t.Error("export did not write the configuration")
}
}
// Ctrl-C: a cancelled context is exit 3, the family-wide "interrupted"
// (docs/program-conventions.md); "not found" moved to 7 for it.
func TestInterrupted(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
var out, errb bytes.Buffer
code := Run(ctx, []string{"solution", "show", "nope"}, Env{
Stdin: strings.NewReader(""), Stdout: &out, Stderr: &errb,
Program: "yetano-solutions", StoreDir: t.TempDir(), Catalogs: []string{"../engine/testdata/cat"},
Getenv: func(string) string { return "" },
})
if code != ExitInterrupted || ExitInterrupted != 3 || ExitNotFound != 7 {
t.Fatalf("cancelled: exit %d (%s)", code, errb.String())
}
if r := run(t, t.TempDir(), "solution", "show", "nope"); r.code != 7 {
t.Fatalf("not found: exit %d", r.code)
}
}