solutions: Solutions Guide engine (catalog, rules, wizard state, native config, knowledge index, CLI)
New nested module yetano.dev/yetano/core/solutions: loads the catalog in assets/solutions on top of the Big Picture, evaluates questions and conditions, resolves packages/modules/capabilities, generates native configuration from typed fragments with per-value origins, and builds architecture graph, plan, lock file and handbook. Knowledge index with yetano: references, search and lint (incl. manual sections). CLI with yet's conventions (solution, task, knowledge, open), standalone as yetano-solutions. No execution, no network. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
586e76889c
commit
2481a0dd27
48 files changed
+10304
No files matched your search
@@ -0,0 +1,408 @@
|
||||
package engine
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"yetano.dev/yetano/core/solutions/catalog"
|
||||
)
|
||||
|
||||
func testCat(t *testing.T) *catalog.Catalog {
|
||||
t.Helper()
|
||||
c, err := catalog.Load("testdata/cat")
|
||||
if err != nil {
|
||||
for _, i := range c.Issues {
|
||||
t.Log(i)
|
||||
}
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, i := range c.Validate() {
|
||||
if i.Severity == "error" {
|
||||
t.Fatal(i)
|
||||
}
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
func eval(t *testing.T, e *Engine, mode catalog.Level, params map[string]any, p *Preset) *State {
|
||||
t.Helper()
|
||||
in := NewInstance("t1", e.Cat.Solutions["app-run"], mode)
|
||||
for k, v := range params {
|
||||
in.Spec.Parameters[k] = v
|
||||
}
|
||||
st, err := e.Evaluate(in, p)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return st
|
||||
}
|
||||
|
||||
func pkg(st *State, id string) *PackageState {
|
||||
for i := range st.Packages {
|
||||
if st.Packages[i].ID == id {
|
||||
return &st.Packages[i]
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func capOf(st *State, id string) *CapState {
|
||||
for i := range st.Capabilities {
|
||||
if st.Capabilities[i].ID == id {
|
||||
return &st.Capabilities[i]
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func file(st *State, path string) string {
|
||||
for _, f := range st.Files {
|
||||
if f.Path == path {
|
||||
return f.Content
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func question(st *State, id string) QuestionState { return findQuestion(st, id) }
|
||||
|
||||
func findQuestion(st *State, id string) QuestionState {
|
||||
for _, q := range st.Questions {
|
||||
if q.ID == id {
|
||||
return q
|
||||
}
|
||||
}
|
||||
return QuestionState{}
|
||||
}
|
||||
|
||||
func hasMsg(st *State, sub string) bool {
|
||||
for _, m := range st.Messages {
|
||||
if strings.Contains(m.Text, sub) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func TestQuickDefaultPath(t *testing.T) {
|
||||
e := New(testCat(t))
|
||||
st := eval(t, e, catalog.LevelQuick, nil, nil)
|
||||
if st.Complete || len(st.Progress.Missing) != 1 || st.Progress.Missing[0] != "hosts" {
|
||||
t.Fatalf("want only hosts missing, got %v", st.Progress.Missing)
|
||||
}
|
||||
if st.Next != "hosts" {
|
||||
t.Errorf("next = %q", st.Next)
|
||||
}
|
||||
// quick asks size and hosts only; guided questions take their defaults
|
||||
var asked []string
|
||||
for _, q := range st.Questions {
|
||||
if q.Asked {
|
||||
asked = append(asked, q.ID)
|
||||
}
|
||||
}
|
||||
if strings.Join(asked, ",") != "size,hosts" {
|
||||
t.Errorf("asked %v", asked)
|
||||
}
|
||||
st = eval(t, e, catalog.LevelQuick, map[string]any{"hosts": "a.example.org, b.example.org"}, nil)
|
||||
if !st.Complete {
|
||||
t.Fatalf("incomplete: %v %v", st.Progress.Missing, st.Messages)
|
||||
}
|
||||
if p := pkg(st, "app"); p == nil || !p.Selected || p.Profile != "base" {
|
||||
t.Fatalf("app: %+v", p)
|
||||
}
|
||||
if c := capOf(st, "auth.users"); c == nil || c.Provider != "app/users" {
|
||||
t.Errorf("auth.users provider: %+v", c)
|
||||
}
|
||||
if c := capOf(st, "backup.files"); c == nil || c.Status != StRecommended {
|
||||
t.Errorf("backup.files should be recommended: %+v", c)
|
||||
}
|
||||
if c := capOf(st, "feature.x"); c != nil && c.Status != StExcluded {
|
||||
t.Errorf("feature.x should be excluded or absent: %+v", c)
|
||||
}
|
||||
cfg := file(st, "config/app/app.yaml")
|
||||
for _, want := range []string{"hosts:\n - a.example.org\n - b.example.org", "owner: admin@a.example.org", "listen: :8080", "first: a.example.org", "n: 2", "max: 10"} {
|
||||
if !strings.Contains(cfg, want) {
|
||||
t.Errorf("config lacks %q:\n%s", want, cfg)
|
||||
}
|
||||
}
|
||||
if file(st, "config/bak/bak.yaml") != "" || file(st, "config/idp/idp.yaml") != "" {
|
||||
t.Error("files for packages that are not selected")
|
||||
}
|
||||
if len(st.Steps) != 3 { // 2 hosts + 1 program (server; extra is disabled)
|
||||
t.Errorf("steps: %+v", st.Steps)
|
||||
}
|
||||
}
|
||||
|
||||
// Scenario B: local -> central identity and back (ACC-B, RULE-06).
|
||||
func TestIdentitySwitch(t *testing.T) {
|
||||
e := New(testCat(t))
|
||||
base := map[string]any{"hosts": []any{"a.example.org"}}
|
||||
st := eval(t, e, catalog.LevelGuided, base, nil)
|
||||
if pkg(st, "idp") != nil {
|
||||
t.Fatal("idp without central identity")
|
||||
}
|
||||
central := map[string]any{"hosts": []any{"a.example.org"}, "identity": "central"}
|
||||
st = eval(t, e, catalog.LevelGuided, central, nil)
|
||||
if !question(st, "realm").Visible || st.Complete {
|
||||
t.Fatal("realm must appear and be required")
|
||||
}
|
||||
central["realm"] = "corp"
|
||||
st = eval(t, e, catalog.LevelGuided, central, nil)
|
||||
if !st.Complete {
|
||||
t.Fatalf("incomplete: %v %v", st.Progress.Missing, st.Messages)
|
||||
}
|
||||
if p := pkg(st, "idp"); p == nil || !p.Selected {
|
||||
t.Fatal("idp must be selected")
|
||||
}
|
||||
if c := capOf(st, "auth.sso"); c == nil || c.Provider != "idp/sso" {
|
||||
t.Fatalf("auth.sso must come from idp, not the unimplemented module: %+v", c)
|
||||
}
|
||||
if !strings.Contains(file(st, "config/app/app.yaml"), "realm: corp") || file(st, "config/idp/idp.yaml") == "" {
|
||||
t.Error("central identity config missing")
|
||||
}
|
||||
edge := false
|
||||
for _, ed := range st.Graph.Edges {
|
||||
edge = edge || (ed.From == "pkg:app" && ed.To == "pkg:idp" && ed.Type == "authenticatesVia")
|
||||
}
|
||||
if !edge {
|
||||
t.Error("confirmed Big Picture connection app -> idp missing from the graph")
|
||||
}
|
||||
// back to local: realm is stale, not in the config, idp gone
|
||||
central["identity"] = "local"
|
||||
st = eval(t, e, catalog.LevelGuided, central, nil)
|
||||
if pkg(st, "idp") != nil || strings.Contains(file(st, "config/app/app.yaml"), "realm") {
|
||||
t.Error("central leftovers after switching back")
|
||||
}
|
||||
if len(st.Stale) != 1 || st.Stale[0].Question != "realm" {
|
||||
t.Errorf("stale: %+v", st.Stale)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNotImplementedOptionIsVisibleButNotSelectable(t *testing.T) {
|
||||
e := New(testCat(t))
|
||||
st := eval(t, e, catalog.LevelGuided, map[string]any{"hosts": []any{"a.example.org"}, "identity": "ghost"}, nil)
|
||||
q := question(st, "identity")
|
||||
var ghost OptionState
|
||||
for _, o := range q.Options {
|
||||
if o.ID == "ghost" {
|
||||
ghost = o
|
||||
}
|
||||
}
|
||||
if ghost.Enabled || ghost.Disabled != "not built" {
|
||||
t.Errorf("ghost option: %+v", ghost)
|
||||
}
|
||||
if len(st.Stale) != 1 || q.Value != "local" {
|
||||
t.Errorf("the unselectable answer must be stale and the default used: %+v %v", st.Stale, q.Value)
|
||||
}
|
||||
}
|
||||
|
||||
func TestModeSwitchKeepsAnswers(t *testing.T) {
|
||||
e := New(testCat(t))
|
||||
params := map[string]any{"hosts": []any{"a.example.org"}, "port": 9090}
|
||||
adv := eval(t, e, catalog.LevelAdvanced, params, nil)
|
||||
quick := eval(t, e, catalog.LevelQuick, params, nil)
|
||||
if file(adv, "config/app/app.yaml") != file(quick, "config/app/app.yaml") {
|
||||
t.Error("the mode must not change the result")
|
||||
}
|
||||
if !strings.Contains(file(quick, "config/app/app.yaml"), ":9090") {
|
||||
t.Error("an advanced answer is lost in quick mode")
|
||||
}
|
||||
if question(quick, "port").Asked {
|
||||
t.Error("quick mode asks an advanced question")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidationAndSecrets(t *testing.T) {
|
||||
e := New(testCat(t))
|
||||
st := eval(t, e, catalog.LevelAdvanced, map[string]any{"hosts": []any{"not a host"}, "token": "hunter2"}, nil)
|
||||
if len(question(st, "hosts").Errors) == 0 {
|
||||
t.Error("an invalid host name is accepted")
|
||||
}
|
||||
if len(question(st, "token").Errors) == 0 {
|
||||
t.Error("a plain secret is accepted")
|
||||
}
|
||||
st = eval(t, e, catalog.LevelAdvanced, map[string]any{"hosts": []any{"a.example.org"}, "token": "secret://app/token"}, nil)
|
||||
if !st.Complete || !strings.Contains(file(st, "config/app/app.yaml"), "token: secret://app/token") {
|
||||
t.Errorf("secret reference not taken: %v", st.Messages)
|
||||
}
|
||||
for _, p := range e.Handbook(st, nil) {
|
||||
if strings.Contains(string(p.Content), "secret://app/token") && strings.HasSuffix(p.Path, "index.md") {
|
||||
t.Error("the handbook's decision table shows a secret reference")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Scenario G: a preset answers everything but the instance-specific questions.
|
||||
func TestPreset(t *testing.T) {
|
||||
e := New(testCat(t))
|
||||
in := NewInstance("t1", e.Cat.Solutions["app-run"], catalog.LevelGuided)
|
||||
in.Spec.Parameters = map[string]any{"hosts": []any{"a.example.org"}, "size": "big", "backup": "mine", "token": "secret://x"}
|
||||
p := PresetFrom("mine", in, e.Cat.Solutions["app-run"])
|
||||
if _, ok := p.Spec.Parameters["hosts"]; ok {
|
||||
t.Error("an instance-specific answer in the preset")
|
||||
}
|
||||
if _, ok := p.Spec.Parameters["token"]; ok {
|
||||
t.Error("a secret in the preset")
|
||||
}
|
||||
st := eval(t, e, catalog.LevelQuick, map[string]any{"hosts": []any{"new.example.org"}}, p)
|
||||
if !st.Complete || pkg(st, "app").Profile != "big" || st.Answers["size"].Origin != "preset:mine" {
|
||||
t.Fatalf("preset not applied: %+v %v", st.Answers["size"], st.Messages)
|
||||
}
|
||||
if question(st, "size").Asked {
|
||||
t.Error("a question the preset answered is asked again")
|
||||
}
|
||||
if !question(st, "hosts").Asked {
|
||||
t.Error("the instance-specific question must be asked")
|
||||
}
|
||||
if !hasMsg(st, "Big needs more memory") {
|
||||
t.Error("rule message missing")
|
||||
}
|
||||
if !strings.Contains(file(st, "config/app/extra.yaml"), "on: true") || !strings.Contains(file(st, "config/app/app.yaml"), "max: 100") {
|
||||
t.Error("profile inheritance (big extends base) not applied")
|
||||
}
|
||||
if c := capOf(st, "backup.files"); c == nil || c.Status != StExternal {
|
||||
t.Errorf("backup by own tool must be external: %+v", c)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExplainAndOverride(t *testing.T) {
|
||||
e := New(testCat(t))
|
||||
in := NewInstance("t1", e.Cat.Solutions["app-run"], catalog.LevelQuick)
|
||||
in.Spec.Parameters["hosts"] = []any{"a.example.org"}
|
||||
in.Spec.Overrides = []Override{{File: "app/app.yaml", Path: "limits.min", Value: 5}}
|
||||
st, err := e.Evaluate(in, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ex := Explain(st, "app/app.yaml", "owner")
|
||||
if !ex.Found || ex.Origin.Source != "solution:app-run#main" || ex.Origins["owner"] != "default" {
|
||||
t.Errorf("explain owner: %+v", ex)
|
||||
}
|
||||
if ex := Explain(st, "config/app/app.yaml", "limits.max"); ex.Origin.Source != "profile:app/base" {
|
||||
t.Errorf("explain limits.max: %+v", ex)
|
||||
}
|
||||
if ex := Explain(st, "app/app.yaml", "limits.min"); ex.Origin.Layer != "override" {
|
||||
t.Errorf("explain override: %+v", ex)
|
||||
}
|
||||
if !strings.Contains(file(st, "config/app/app.yaml"), "min: 5") {
|
||||
t.Error("override not applied")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeterministicAndJSON(t *testing.T) {
|
||||
e := New(testCat(t))
|
||||
p := map[string]any{"hosts": []any{"a.example.org", "b.example.org"}, "identity": "central", "realm": "r"}
|
||||
a, _ := json.Marshal(eval(t, e, catalog.LevelGuided, p, nil))
|
||||
for i := 0; i < 5; i++ {
|
||||
b, _ := json.Marshal(eval(t, e, catalog.LevelGuided, p, nil))
|
||||
if string(a) != string(b) {
|
||||
t.Fatal("evaluation is not deterministic")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateWriteAndProtect(t *testing.T) {
|
||||
e := New(testCat(t))
|
||||
in := NewInstance("t1", e.Cat.Solutions["app-run"], catalog.LevelQuick)
|
||||
in.Spec.Parameters["hosts"] = []any{"a.example.org"}
|
||||
st, _ := e.Evaluate(in, nil)
|
||||
out, err := e.Generate(st, in, nil, false)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
dir := t.TempDir()
|
||||
if err := Write(dir, out, false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// regenerating unchanged is fine
|
||||
if err := Write(dir, out, false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// a hand edit blocks regeneration (CONF-06)
|
||||
cfg := filepath.Join(dir, "config", "app", "app.yaml")
|
||||
if err := os.WriteFile(cfg, []byte("# mine\n"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := Write(dir, out, false); !errors.Is(err, ErrModified) {
|
||||
t.Fatalf("hand edit overwritten: %v", err)
|
||||
}
|
||||
if err := Write(dir, out, true); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// an incomplete state does not generate
|
||||
in2 := NewInstance("t2", e.Cat.Solutions["app-run"], catalog.LevelQuick)
|
||||
st2, _ := e.Evaluate(in2, nil)
|
||||
if _, err := e.Generate(st2, in2, nil, false); !errors.Is(err, ErrIncomplete) {
|
||||
t.Errorf("incomplete generated: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLockDrift(t *testing.T) {
|
||||
e := New(testCat(t))
|
||||
in := NewInstance("t1", e.Cat.Solutions["app-run"], catalog.LevelQuick)
|
||||
in.Spec.Parameters["hosts"] = []any{"a.example.org"}
|
||||
st, _ := e.Evaluate(in, nil)
|
||||
out, _ := e.Generate(st, in, nil, false)
|
||||
out2, _ := e.Generate(st, in, nil, false)
|
||||
if d := LockDrift(out.Lock, out2.Lock); len(d) != 0 {
|
||||
t.Errorf("drift without change: %v", d)
|
||||
}
|
||||
changed := *out.Lock
|
||||
changed.Spec.Profiles = append([]Pinned{}, out.Lock.Spec.Profiles...)
|
||||
changed.Spec.Profiles[0].Hash = "x"
|
||||
if d := LockDrift(&changed, out2.Lock); len(d) != 1 || !strings.Contains(d[0], "profile app/base changed") {
|
||||
t.Errorf("profile change not detected: %v", d)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStepsChecksGraph(t *testing.T) {
|
||||
e := New(testCat(t))
|
||||
st := eval(t, e, catalog.LevelQuick, map[string]any{"hosts": []any{"a.example.org"}, "size": "big"}, nil)
|
||||
if st.Steps[0].Phase != "install" || len(st.Steps) != 3 {
|
||||
t.Errorf("steps: %+v", st.Steps)
|
||||
}
|
||||
if st.Steps[0].Run != "yet install app-server" || st.Steps[1].Run != "yet install app-extra" {
|
||||
t.Errorf("module steps: %+v", st.Steps)
|
||||
}
|
||||
for _, c := range st.Checks {
|
||||
switch c.ID {
|
||||
case "port":
|
||||
if c.Status != CheckNotChecked || c.Params["port"] != "8080" {
|
||||
t.Errorf("port check: %+v", c)
|
||||
}
|
||||
case "bigonly":
|
||||
if c.Status != CheckNotChecked {
|
||||
t.Errorf("bigonly: %+v", c)
|
||||
}
|
||||
}
|
||||
}
|
||||
found := false
|
||||
for _, n := range st.Graph.Nodes {
|
||||
found = found || n.Title == "Users of a.example.org"
|
||||
}
|
||||
if !found {
|
||||
t.Error("node title not interpolated")
|
||||
}
|
||||
if !strings.Contains(GraphText(st.Graph), "app-extra") {
|
||||
t.Error(GraphText(st.Graph))
|
||||
}
|
||||
}
|
||||
|
||||
func TestConditionOrderIsEnforced(t *testing.T) {
|
||||
c := testCat(t)
|
||||
s := c.Solutions["app-run"]
|
||||
q, _ := s.Question("size")
|
||||
q.VisibleWhen = catalog.Cond{"hosts": map[string]any{"set": true}}
|
||||
defer func() { q.VisibleWhen = nil }()
|
||||
found := false
|
||||
for _, i := range c.Validate() {
|
||||
found = found || i.Code == "condition-order"
|
||||
}
|
||||
if !found {
|
||||
t.Error("a condition on a later question is not reported")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user