package engine import ( "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) } }