233 lines
9.2 KiB
Go
233 lines
9.2 KiB
Go
package engine
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestFailedLoadRetainsDecodedOrResolverEntry(t *testing.T) {
|
|
type settings struct {
|
|
Count int `yaml:"count"`
|
|
Label string `yaml:"label"`
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
overrides []Override
|
|
resolve func(*settings, *Resolution) error
|
|
checkpoint InspectionStage
|
|
wantCount string
|
|
wantLabel string
|
|
}{
|
|
{
|
|
name: "failed override batch",
|
|
overrides: []Override{
|
|
{Path: Keys("label"), Value: "changed", Flag: "label"},
|
|
{Path: Keys("count"), Value: "invalid", Flag: "count"},
|
|
},
|
|
checkpoint: InspectionStageDecoded, wantCount: "count: 3", wantLabel: "label: original",
|
|
},
|
|
{
|
|
name: "resolver error",
|
|
overrides: []Override{{Path: Keys("label"), Value: "cli", Flag: "label"}},
|
|
resolve: func(value *settings, _ *Resolution) error {
|
|
value.Label = "failed-edit"
|
|
return errors.New("reject")
|
|
},
|
|
checkpoint: InspectionStageOverridden, wantCount: "count: 3", wantLabel: "label: cli",
|
|
},
|
|
{
|
|
name: "unrecorded resolver mutation",
|
|
overrides: []Override{{Path: Keys("label"), Value: "cli", Flag: "label"}},
|
|
resolve: func(value *settings, _ *Resolution) error {
|
|
value.Label = "failed-edit"
|
|
return nil
|
|
},
|
|
checkpoint: InspectionStageOverridden, wantCount: "count: 3", wantLabel: "label: cli",
|
|
},
|
|
}
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
result, err := testLoad(Bytes("input.yml", []byte("count: 3\nlabel: original\n")), Options[settings]{
|
|
Definition: Definition[settings]{Resolve: test.resolve}, Overrides: test.overrides,
|
|
})
|
|
var failure *LoadError
|
|
if result != nil || !errors.As(err, &failure) || failure.Inspection() == nil {
|
|
t.Fatalf("failed load: result=%v error=%v", result, err)
|
|
}
|
|
view, ok := NewInspectionView(failure.Inspection(), Path{})
|
|
if !ok || view.Stage() != test.checkpoint {
|
|
t.Fatalf("inspection checkpoint = %q, available=%v", view.Stage(), ok)
|
|
}
|
|
var output bytes.Buffer
|
|
if err := view.WriteEffective(&output); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !strings.Contains(output.String(), test.wantCount) || !strings.Contains(output.String(), test.wantLabel) || strings.Contains(output.String(), "failed-edit") {
|
|
t.Fatalf("wrong entry values: %q", output.String())
|
|
}
|
|
info, found := view.Inspection().Metadata().Lookup(Keys("label"))
|
|
if !found || (test.checkpoint == InspectionStageOverridden && info.Origin.Layer != LayerCLI) || (test.checkpoint == InspectionStageDecoded && info.Origin.Layer != LayerMain) {
|
|
t.Fatalf("wrong label origin: %#v", info)
|
|
}
|
|
if explanation, found := view.Inspection().Explain(Keys("label")); !found || len(explanation.History) == 0 {
|
|
t.Fatalf("missing entry history: %#v", explanation)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestFailedFinalValidationRetainsValidatorInput(t *testing.T) {
|
|
type settings struct {
|
|
Value string `yaml:"value"`
|
|
}
|
|
result, err := testLoad(Bytes("input.yml", []byte("value: before\n")), Options[settings]{Definition: Definition[settings]{
|
|
Validate: func(value *settings, validation *Validation) {
|
|
validation.Error(Keys("value"), "rejected")
|
|
value.Value = "after"
|
|
},
|
|
}})
|
|
var failure *LoadError
|
|
if result != nil || !errors.As(err, &failure) || failure.Inspection() == nil {
|
|
t.Fatalf("failed load: result=%v error=%v", result, err)
|
|
}
|
|
view, _ := NewInspectionView(failure.Inspection(), Path{})
|
|
if view.Stage() != InspectionStageResolved {
|
|
t.Fatalf("inspection checkpoint = %q", view.Stage())
|
|
}
|
|
var output bytes.Buffer
|
|
if err := view.WriteEffective(&output); err != nil || !strings.Contains(output.String(), "before") || strings.Contains(output.String(), "after") {
|
|
t.Fatalf("validator entry output = %q, %v", output.String(), err)
|
|
}
|
|
var semantic, mutation bool
|
|
for _, diagnostic := range failure.Diagnostics() {
|
|
semantic = semantic || diagnostic.Code == CodeValidateError
|
|
mutation = mutation || diagnostic.Code == CodeValidateMutation
|
|
}
|
|
if !semantic || !mutation {
|
|
t.Fatalf("missing diagnostics: %#v", failure.Diagnostics())
|
|
}
|
|
}
|
|
|
|
func TestFailedSecretLookupRetainsOnlyResolvedReferences(t *testing.T) {
|
|
type settings struct {
|
|
Good Secret `yaml:"good"`
|
|
Bad Secret `yaml:"bad"`
|
|
}
|
|
result, err := testLoadWithSecretLookup(Bytes("input.yml", []byte("good: GOOD_SECRET\nbad: MISSING_SECRET\n")), func(name string) (string, bool) {
|
|
return "credential-canary", name == "GOOD_SECRET"
|
|
}, Options[settings]{})
|
|
var failure *LoadError
|
|
if result != nil || !errors.As(err, &failure) || failure.Inspection() == nil {
|
|
t.Fatalf("failed load: result=%v error=%v", result, err)
|
|
}
|
|
inspection := failure.Inspection()
|
|
good, ok := inspection.Explain(Keys("good"))
|
|
if !ok || good.SecretReference == nil || good.SecretReference.Name != "GOOD_SECRET" {
|
|
t.Fatalf("resolved reference missing: %#v", good)
|
|
}
|
|
bad, ok := inspection.Explain(Keys("bad"))
|
|
if !ok || bad.SecretReference != nil {
|
|
t.Fatalf("unresolved reference was reported as resolved: %#v", bad)
|
|
}
|
|
var output bytes.Buffer
|
|
if err := inspection.WriteEffective(&output); err != nil || strings.Contains(output.String(), "credential-canary") {
|
|
t.Fatalf("credential in report: %q, %v", output.String(), err)
|
|
}
|
|
}
|
|
|
|
func TestFailedFinalIdentityRetainsResolvedInspection(t *testing.T) {
|
|
type item struct {
|
|
ID string `yaml:"id"`
|
|
}
|
|
type settings struct {
|
|
Items []item `yaml:"items"`
|
|
}
|
|
definition := DefinitionWithSequenceIdentityRegistrations(Definition[settings]{
|
|
Defaults: func() settings { return settings{Items: []item{{ID: "same"}, {ID: "same"}}} },
|
|
}, []SequenceIdentityRegistrationInput{{ExactPath: pathPointer(Keys("items")), IdentityFields: []string{"id"}}})
|
|
result, err := testLoad(NoSource(), Options[settings]{Definition: definition})
|
|
var failure *LoadError
|
|
if result != nil || !errors.As(err, &failure) || failure.Inspection() == nil {
|
|
t.Fatalf("identity failure: result=%v error=%v", result, err)
|
|
}
|
|
view, _ := NewInspectionView(failure.Inspection(), Path{})
|
|
if view.Stage() != InspectionStageResolved {
|
|
t.Fatalf("identity checkpoint = %q", view.Stage())
|
|
}
|
|
if info, found := failure.Inspection().Metadata().Lookup(Keys("items").Index(1).Key("id")); !found || info.Origin.Layer != LayerDefault {
|
|
t.Fatalf("default identity missing: %#v", info)
|
|
}
|
|
}
|
|
|
|
func TestFailureInspectionCheckpointsAcrossLoadPaths(t *testing.T) {
|
|
type settings struct {
|
|
Version string `yaml:"version"`
|
|
Value int `yaml:"value"`
|
|
Parent struct {
|
|
Value int `yaml:"value"`
|
|
} `yaml:"parent"`
|
|
Child struct {
|
|
Value int `yaml:"value"`
|
|
} `yaml:"child"`
|
|
}
|
|
version := &VersionRule{Path: Keys("version"), Check: func(_ Version, validation *Validation) error {
|
|
validation.Error(Keys("version"), "unsupported")
|
|
return nil
|
|
}}
|
|
tests := []struct {
|
|
name string
|
|
request loadRequest[settings]
|
|
checkpoint InspectionStage
|
|
contains string
|
|
}{
|
|
{name: "malformed source", request: loadRequest[settings]{main: loadInput{source: Bytes("main", []byte("[\n"))}}, checkpoint: ""},
|
|
{name: "main version", request: loadRequest[settings]{main: loadInput{source: Bytes("main", []byte("version: bad\nvalue: 3\n"))}, definition: Definition[settings]{Version: version}}, checkpoint: InspectionStageMain, contains: "value: 3"},
|
|
{name: "layer failure", request: loadRequest[settings]{main: loadInput{source: Bytes("main", []byte("value: 3\n"))}, underlays: []loadGroup{{inputs: []loadInput{{source: Bytes("bad", []byte("[\n"))}}}}}, checkpoint: InspectionStageMain, contains: "value: 3"},
|
|
{name: "inheritance failure", request: loadRequest[settings]{main: loadInput{source: Bytes("main", []byte("child: {extends: missing}\n"))}}, checkpoint: InspectionStageComposed, contains: "extends: missing"},
|
|
{name: "decode failure", request: loadRequest[settings]{main: loadInput{source: Bytes("main", []byte("value: wrong\n"))}}, checkpoint: InspectionStageInherited, contains: "value: wrong"},
|
|
{name: "no source override preflight", request: loadRequest[settings]{main: loadInput{source: NoSource()}, overrides: []Override{{Path: Path{}, Value: 1, Flag: "value"}}}, checkpoint: ""},
|
|
}
|
|
for _, test := range tests {
|
|
for _, ordinary := range []bool{true, false} {
|
|
if ordinary && len(test.request.underlays) != 0 {
|
|
continue
|
|
}
|
|
name := test.name
|
|
if ordinary {
|
|
name += " ordinary"
|
|
} else {
|
|
name += " layered"
|
|
}
|
|
t.Run(name, func(t *testing.T) {
|
|
var result *Loaded[settings]
|
|
var err error
|
|
if ordinary {
|
|
result, err = loadWithSecretLookup(test.request.main.source, testSecretBinding(), nil, Options[settings]{Definition: test.request.definition, Overrides: test.request.overrides})
|
|
} else {
|
|
result, err = loadGroupedWithSecretLookup(test.request, testSecretBinding(), nil)
|
|
}
|
|
var failure *LoadError
|
|
if result != nil || !errors.As(err, &failure) {
|
|
t.Fatalf("expected failure: result=%v err=%v", result, err)
|
|
}
|
|
if test.checkpoint == "" {
|
|
if failure.Inspection() != nil {
|
|
t.Fatal("unexpected inspection")
|
|
}
|
|
return
|
|
}
|
|
view, ok := NewInspectionView(failure.Inspection(), Path{})
|
|
if !ok || view.Stage() != test.checkpoint {
|
|
t.Fatalf("checkpoint = %q, available = %v", view.Stage(), ok)
|
|
}
|
|
var output bytes.Buffer
|
|
if err := view.WriteEffective(&output); err != nil || !strings.Contains(output.String(), test.contains) {
|
|
t.Fatalf("output = %q, %v", output.String(), err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
}
|