107 lines
4.1 KiB
Go
107 lines
4.1 KiB
Go
package engine
|
|
|
|
import (
|
|
"bytes"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestSourceInspectionRetainsRawOverlayHistoryAndSensitivity(t *testing.T) {
|
|
type settings struct {
|
|
Count int `yaml:"count"`
|
|
Private map[string]string `yaml:"private" configkit:"secret"`
|
|
Default string `yaml:"default"`
|
|
}
|
|
base := parseCompositionDocument(t, "source-1", "count: 1\nprivate: {token: old-canary}\n")
|
|
composed, diagnostics := composeDocuments(base)
|
|
if len(diagnostics) != 0 {
|
|
t.Fatal(diagnostics)
|
|
}
|
|
overlay := parseCompositionDocument(t, "source-2", "count: invalid\nprivate: {token: new-canary}\n")
|
|
composed, diagnostics = overlayDocuments(composed, overlay)
|
|
if len(diagnostics) != 0 {
|
|
t.Fatal(diagnostics)
|
|
}
|
|
input := assembledInput{document: composed.root, composition: composed}
|
|
schema, issue := buildSchema(reflect.TypeFor[settings](), testSecretBinding())
|
|
if issue != nil {
|
|
t.Fatal(issue)
|
|
}
|
|
definition := Definition[settings]{Defaults: func() settings { return settings{Default: "invented"} }}
|
|
if _, diagnostics := decodeLoadInput(schema, definition, input); len(diagnostics) == 0 {
|
|
t.Fatal("expected the raw count to fail typed decoding")
|
|
}
|
|
inspection, ok := prepareSourceInspection(input, newRetainedNodeCounter(historyNodeLimit))
|
|
if !ok {
|
|
t.Fatal("source inspection unavailable")
|
|
}
|
|
accumulator := &diagnosticAccumulator{}
|
|
failure := accumulator.loadError()
|
|
finishInspection(schema, nil, nil, nil, inspection, nil, accumulator, failure, InspectionStageInherited)
|
|
if _, found := inspection.Metadata().Lookup(Keys("default")); found {
|
|
t.Fatal("source inspection invented a default")
|
|
}
|
|
var output bytes.Buffer
|
|
if err := inspection.WriteEffective(&output); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !strings.Contains(output.String(), "count: invalid") || strings.Contains(output.String(), "canary") {
|
|
t.Fatalf("unsafe or incorrect effective output: %s", output.String())
|
|
}
|
|
explanation, found := inspection.Explain(Keys("private", "token"))
|
|
if !found || len(explanation.History) < 2 {
|
|
t.Fatalf("missing displaced history: %#v", explanation)
|
|
}
|
|
for _, event := range explanation.History {
|
|
if strings.Contains(event.Before.YAML+event.After.YAML, "canary") {
|
|
t.Fatalf("unredacted history: %#v", event)
|
|
}
|
|
}
|
|
info, found := inspection.Metadata().Lookup(Keys("count"))
|
|
if !found || !info.Explicit || len(info.Origin.Locations) == 0 || info.Origin.Locations[0].SourceID != "source-2" {
|
|
t.Fatalf("wrong source origin: %#v", info)
|
|
}
|
|
if _, ok := prepareSourceInspection(input, newRetainedNodeCounter(0)); ok {
|
|
t.Fatal("source inspection bypassed history budget")
|
|
}
|
|
}
|
|
|
|
func TestSourceInspectionRetainsCompletedInheritance(t *testing.T) {
|
|
type entry struct {
|
|
Count int `yaml:"count"`
|
|
}
|
|
type settings struct {
|
|
Parent entry `yaml:"parent"`
|
|
Child entry `yaml:"child"`
|
|
}
|
|
root := parseCompositionDocument(t, "source-1", "parent: {count: invalid}\nchild: {extends: parent}\n")
|
|
composed, diagnostics := composeDocuments(root)
|
|
if len(diagnostics) != 0 {
|
|
t.Fatal(diagnostics)
|
|
}
|
|
input, _, expanded, diagnostics := expandStructuralInheritance(assembledInput{document: composed.root, composition: composed}, nil, sequencePolicy{})
|
|
if !expanded || len(diagnostics) != 0 {
|
|
t.Fatalf("inheritance: expanded=%v diagnostics=%v", expanded, diagnostics)
|
|
}
|
|
schema, issue := buildSchema(reflect.TypeFor[settings](), testSecretBinding())
|
|
if issue != nil {
|
|
t.Fatal(issue)
|
|
}
|
|
if _, diagnostics := decodeLoadInput(schema, Definition[settings]{}, input); len(diagnostics) == 0 {
|
|
t.Fatal("expected inherited invalid value to fail decoding")
|
|
}
|
|
inspection, ok := prepareSourceInspection(input, newRetainedNodeCounter(historyNodeLimit))
|
|
if !ok {
|
|
t.Fatal("source inspection unavailable")
|
|
}
|
|
explanation, found := inspection.Explain(Keys("child", "count"))
|
|
if !found || !strings.Contains(explanation.Effective.YAML, "invalid") || len(explanation.History) == 0 {
|
|
t.Fatalf("inherited value or history unavailable: %#v", explanation)
|
|
}
|
|
info, found := inspection.Metadata().Lookup(Keys("child", "count"))
|
|
if !found || !info.Explicit || len(info.Origin.Inheritance) == 0 {
|
|
t.Fatalf("inherited origin unavailable: %#v", info)
|
|
}
|
|
}
|