Files
configkit/facade_failure_checkpoint_test.go

77 lines
3.0 KiB
Go

package configkit_test
import (
"bytes"
"errors"
"strings"
"testing"
"testing/fstest"
ck "go.maximumdirect.net/configkit"
)
func TestFailureMainCheckpointUsesLowerCollectionPolicy(t *testing.T) {
files := fstest.MapFS{
"main.yml": {Data: []byte("config: {imports: [{path: a.yml}, {path: b.yml}]}\n")},
"a.yml": {Data: []byte("items: [{id: a, value: first}]\n")},
"b.yml": {Data: []byte("items: [{id: b, value: second}]\n")},
}
filesystem := ck.NewFilesystem("checkpoint", files)
main := ck.Input{Source: filesystem.File("main.yml"), Imports: true}
lower := ck.Underlay{Inputs: []ck.Input{{Source: ck.Bytes("lower", []byte("config:\n sequences: [{destination: [items], key: id}]\nitems: [{id: lower, value: base}]\n"))}}}
options := ck.LayeredOptions[map[string]any]{Underlays: []ck.Underlay{lower}, OverlayFilesystem: &ck.FilesystemContext{FS: &filesystem, Root: ".", BaseDir: "."}}
if _, err := ck.LoadLayered(main, options); err != nil {
t.Fatal(err)
}
for _, boundary := range []string{"overlay", "underlay", "version"} {
t.Run(boundary, func(t *testing.T) {
opts := options
switch boundary {
case "overlay":
opts.Overlay = "missing"
case "underlay":
opts.Underlays = append([]ck.Underlay{lower}, ck.Underlay{Inputs: []ck.Input{{Source: ck.Bytes("bad", []byte("[\n"))}}})
case "version":
opts.Definition.Version = &ck.VersionRule{Path: ck.Keys("version"), Check: func(_ ck.Version, v *ck.Validation) error { v.Error(ck.Keys("version"), "rejected"); return nil }}
}
result, err := ck.LoadLayered(main, opts)
var failure *ck.LoadError
if result != nil || !errors.As(err, &failure) {
t.Fatalf("result=%v error=%v", result, err)
}
view, ok := failure.InspectSubtree(ck.Path{})
if !ok || view.Stage() != ck.InspectionStageMain {
t.Fatalf("available=%v stage=%s", ok, view.Stage())
}
var output bytes.Buffer
if err := view.WriteEffective(&output); err != nil {
t.Fatal(err)
}
if !strings.Contains(output.String(), "first") || !strings.Contains(output.String(), "second") || strings.Contains(output.String(), "base") {
t.Fatalf("wrong main snapshot: %s", &output)
}
for index, label := range []string{"a.yml", "b.yml"} {
path := ck.Keys("items").Index(index).Key("value")
info, found := view.Metadata().Lookup(path)
if !found || len(info.Origin.Locations) == 0 || len(view.Metadata().History(path)) == 0 {
t.Fatalf("missing evidence at %s", path)
}
source, found := view.Source(info.Origin.Locations[0].SourceID)
if !found || !strings.HasSuffix(source.Label, label) {
t.Fatalf("wrong source: %#v", source)
}
}
})
}
for _, invalid := range []string{"items: [{id: a}, {id: a}]\n", "items: [{other: a}]\n"} {
_, err := ck.LoadLayered(ck.Input{Source: ck.Bytes("invalid", []byte(invalid))}, options)
var failure *ck.LoadError
if !errors.As(err, &failure) {
t.Fatal("expected composition failure")
}
if _, ok := failure.InspectSubtree(ck.Path{}); ok {
t.Fatal("failed main composition was presented as completed")
}
}
}