122 lines
5.6 KiB
Go
122 lines
5.6 KiB
Go
package engine
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestCapturedMainLoadsLiveImportsWithIndependentEvidence(t *testing.T) {
|
|
root := t.TempDir()
|
|
mainBody := "version: 1\nconfig: {imports: [{path: ../shared.yml}]}\nsettings: {main: captured}\n"
|
|
main := writeImportFile(t, root, "app/main.yml", mainBody)
|
|
child := writeImportFile(t, root, "shared.yml", "version: 2\nsettings: {child: original}\n")
|
|
prepared, _, diagnostics := captureHostMain(File(main), &FilesystemContext{Root: root})
|
|
if len(diagnostics) != 0 {
|
|
t.Fatal(diagnostics)
|
|
}
|
|
if err := os.WriteFile(main, []byte("settings: {main: changed}\n"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
type config struct {
|
|
Version int `yaml:"version"`
|
|
Settings map[string]string `yaml:"settings"`
|
|
}
|
|
options := Options[config]{Imports: true, SourceVersionMode: sourceVersionModeRequired,
|
|
Definition: Definition[config]{Version: &VersionRule{Path: Keys("version"), Check: func(Version, *Validation) error { return nil }}},
|
|
}
|
|
for _, value := range []string{"first", "second"} {
|
|
if err := os.WriteFile(child, []byte("version: 2\nsettings: {child: "+value+"}\n"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
loaded, err := testLoad(prepared, options)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if loaded.Config.Settings["main"] != "captured" || loaded.Config.Settings["child"] != value {
|
|
t.Fatalf("configuration = %#v", loaded.Config)
|
|
}
|
|
sources := loaded.Sources()
|
|
if len(sources) != 2 || sources[0].ID != "source-1" || sources[1].ID != "source-2" || sources[0].VersionCheck == nil || sources[0].VersionCheck.Version.Text != "1" || sources[1].VersionCheck == nil || sources[1].VersionCheck.Version.Text != "2" {
|
|
t.Fatalf("source evidence = %#v", sources)
|
|
}
|
|
sources[0].Label = "mutated"
|
|
if loaded.Sources()[0].Label == "mutated" {
|
|
t.Fatal("source evidence aliases returned records")
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestCapturedMainImportIdentityAndAccounting(t *testing.T) {
|
|
root := t.TempDir()
|
|
mainBody := "config: {imports: [{path: child.yml}]}\nsettings: {main: captured}\n"
|
|
childBody := "settings: {child: value}\n"
|
|
main := writeImportFile(t, root, "main.yml", mainBody)
|
|
child := writeImportFile(t, root, "child.yml", childBody)
|
|
prepared, _, diagnostics := captureHostMain(File(main), nil)
|
|
if len(diagnostics) != 0 {
|
|
t.Fatal(diagnostics)
|
|
}
|
|
accounting := &importAccounting{}
|
|
state := &acquisitionState{accounting: accounting}
|
|
policy := acquisitionPolicy{importLimits: defaultImportLimits()}
|
|
input, diagnostics := acquireInput(inputAcquisitionRequest{
|
|
input: loadInput{source: prepared, imports: true, destination: Keys("service")},
|
|
role: sourceVersionMain, directiveContext: rootDirectiveContextSource,
|
|
}, policy, state, &tierAcquisitionState{visited: make(map[resourceIdentity]importRoute)})
|
|
mainParsed, _ := parseSource(Bytes("main", []byte(mainBody)), "main", defaultParseLimits())
|
|
childParsed, _ := parseSource(Bytes("child", []byte(childBody)), "child", defaultParseLimits())
|
|
wantNodes := countInputNodes(mainParsed.document) + countInputNodes(childParsed.document) + destinationWrapperNodeCount(Keys("service"))*2
|
|
if len(diagnostics) != 0 || len(input.sources) != 2 || accounting.sources != 2 || accounting.bytes != int64(len(mainBody)+len(childBody)) || accounting.nodes != wantNodes {
|
|
t.Fatalf("acquisition = %#v, diagnostics = %#v, accounting = %#v, want nodes %d", input, diagnostics, accounting, wantNodes)
|
|
}
|
|
if err := os.WriteFile(child, []byte("config: {imports: [{path: main.yml}]}\n"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
requireLoadCode(t, prepared, Options[importTestConfig]{Imports: true}, CodeImportCycle)
|
|
if err := os.WriteFile(child, []byte("settings: {child: value}\n"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, err := LoadLayered(Input{Source: NoSource()}, testSecretBinding(), LayeredOptions[importTestConfig]{
|
|
Underlays: []Underlay{{Inputs: []Input{{Source: prepared, Imports: true}, {Source: prepared, Imports: true}}}},
|
|
})
|
|
var loadErr *LoadError
|
|
if !errors.As(err, &loadErr) || len(loadErr.Diagnostics()) == 0 || loadErr.Diagnostics()[0].Code != CodeImportRepeated {
|
|
t.Fatalf("repeated captured file: %v", err)
|
|
}
|
|
if err := os.WriteFile(main, []byte("changed: true\n"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for range 2 {
|
|
if _, err := testLoad(prepared, Options[discoveryLoadConfig]{Imports: true}); err != nil {
|
|
t.Fatalf("independent load: %v", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestCapturedMainBindsLayeredCompanions(t *testing.T) {
|
|
root := t.TempDir()
|
|
main := writeImportFile(t, root, "app/main.yml", "settings: {main: yes-value}\n")
|
|
writeImportFile(t, root, "app/lower.yml", "settings: {lower: underlay}\n")
|
|
writeImportFile(t, root, "app/overlays/dev.yml", "settings: {selected: overlay}\n")
|
|
prepared, _, diagnostics := captureHostMain(File(main), &FilesystemContext{Root: root})
|
|
if len(diagnostics) != 0 {
|
|
t.Fatal(diagnostics)
|
|
}
|
|
underlay := Input{Source: Bytes("companion", []byte("config: {imports: [{path: lower.yml}]}\n")), Imports: true, MainRelative: &MainRelativeContext{RequireBase: true}}
|
|
loaded, err := LoadLayered(Input{Source: prepared}, testSecretBinding(), LayeredOptions[discoveryLoadConfig]{
|
|
Underlays: []Underlay{{Inputs: []Input{underlay}}}, Overlay: "dev",
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if loaded.Config.Settings["main"] != "yes-value" || loaded.Config.Settings["lower"] != "underlay" || loaded.Config.Settings["selected"] != "overlay" {
|
|
t.Fatalf("layered configuration = %#v", loaded.Config)
|
|
}
|
|
sources := loaded.Inspection.Sources()
|
|
if len(sources) != 4 || sources[0].Path != canonicalTestPath(t, main) || sources[2].Path != canonicalTestPath(t, filepath.Join(root, "app/lower.yml")) || sources[3].Layer != LayerOverlay {
|
|
t.Fatalf("layered sources = %#v", sources)
|
|
}
|
|
}
|