Files
configkit/internal/engine/main_relative_integration_test.go

97 lines
4.3 KiB
Go

package engine
import (
"path/filepath"
"strconv"
"testing"
"testing/fstest"
)
func TestMainRelativeAcquisitionClosesHandlesOnFailure(t *testing.T) {
for _, fail := range []string{"parse", "read"} {
t.Run(fail, func(t *testing.T) {
body := "items: [invalid\n"
if fail == "read" {
body = "items: {key: value}\n"
}
filesystem := &observedFilesystem{files: fstest.MapFS{"dir/main.yml": {Data: []byte("{}\n")}, "dir/child.yml": {Data: []byte(body)}}}
if fail == "read" {
filesystem.failRead = "dir/child.yml"
}
namespace := newNativeNamespace("bundle", filesystem)
main := Input{Source: filesystemFile(namespace, "dir/main.yml")}
underlay := Input{Source: Bytes("declarations", []byte("config: {imports: [{path: child.yml}]}\n")), Imports: true, MainRelative: &MainRelativeContext{}}
_, err := LoadLayered[importTestConfig](main, SecretBinding{}, LayeredOptions[importTestConfig]{Underlays: []Underlay{{Inputs: []Input{underlay}}}})
if err == nil {
t.Fatal("invalid selected child succeeded")
}
filesystem.assertClosed(t)
})
}
}
func TestMainRelativeSharesAcquisitionBudget(t *testing.T) {
limits := defaultImportLimits()
limits.sources = 1
accounting := &importAccounting{}
state := &acquisitionState{accounting: accounting}
group := loadGroup{inputs: []loadInput{
{source: Bytes("one", []byte("items: {one: value}\n")), mainRelative: &MainRelativeContext{}},
{source: Bytes("two", []byte("items: {two: value}\n")), imports: true, mainRelative: &MainRelativeContext{}},
}}
_, diagnostics := acquireUnderlayGroupScopes(group, 1, nil, state, acquisitionPolicy{importLimits: limits}, nil)
if len(diagnostics) == 0 || diagnostics[0].Code != CodeImportLimitExceeded {
t.Fatalf("diagnostics = %#v", diagnostics)
}
}
func TestMainRelativeUnboundedImportsAndAnchoredOverlay(t *testing.T) {
t.Parallel()
root := canonicalTestPath(t, t.TempDir())
external := writeImportFile(t, root, "shared/external.yml", "items: {external: shared}\n")
main := writeImportFile(t, root, "app/main.yml", "config:\n imports: [{path: "+strconv.Quote(external)+"}]\nitems: {main: selected}\n")
writeImportFile(t, root, "app/extra.yml", "items: {companion: local}\n")
writeImportFile(t, root, "app/overlays/test.yml", "items: {overlay: app}\n")
writeImportFile(t, root, "overlays/test.yml", "items: {overlay: outer}\n")
base := filepath.Dir(main)
companion := Input{Source: Bytes("companion", []byte("config: {imports: [{path: extra.yml}]}\n")), Imports: true, MainRelative: &MainRelativeContext{Fallback: &FilesystemContext{BaseDir: base}}}
options := LayeredOptions[importTestConfig]{Underlays: []Underlay{{Inputs: []Input{companion}}}, Overlay: "test", OverlayFallback: &FilesystemContext{BaseDir: base}}
for _, entry := range []struct {
name string
main Input
}{
{"discovered", Input{Source: SearchConfig("", Paths(main)), Imports: true}},
{"optional exhausted", Input{Source: SearchConfig("", Paths(filepath.Join(root, "missing.yml")))}},
{"root-only absent", Input{Source: NoSource(), Filesystem: &FilesystemContext{Root: base}}},
} {
t.Run(entry.name, func(t *testing.T) {
result, err := LoadLayered(entry.main, testSecretBinding(), options)
if err != nil {
t.Fatal(err)
}
if result.Config.Items["companion"] != "local" || result.Config.Items["overlay"] != "app" {
t.Fatalf("items=%#v", result.Config.Items)
}
if entry.name == "discovered" && result.Config.Items["external"] != "shared" {
t.Fatal("external main import missing")
}
for _, source := range result.Inspection.Sources() {
if source.Label == "companion" {
context, ok := result.Inspection.SourceContext(source.ID)
if !ok || context.BaseDir != "." {
t.Fatalf("derived resource context=%#v, %v", context, ok)
}
}
}
})
}
// Explicit input contexts override the inherited main context.
override := writeImportFile(t, root, "override/extra.yml", "items: {companion: explicit}\n")
companion.Filesystem = &FilesystemContext{BaseDir: filepath.Dir(override)}
options.Underlays = []Underlay{{Inputs: []Input{companion}}}
result, err := LoadLayered(Input{Source: File(main), Imports: true}, testSecretBinding(), options)
if err != nil || result.Config.Items["companion"] != "explicit" {
t.Fatalf("explicit companion=%#v, %v", result, err)
}
}