127 lines
4.5 KiB
Go
127 lines
4.5 KiB
Go
package configkit_test
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
configkit "go.maximumdirect.net/configkit"
|
|
"io/fs"
|
|
"os"
|
|
"path"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestAbsoluteImportMountedInheritanceAndFailureInspection(t *testing.T) {
|
|
t.Parallel()
|
|
root := t.TempDir()
|
|
write := func(name, content string) string {
|
|
t.Helper()
|
|
file := filepath.Join(root, name)
|
|
if err := os.MkdirAll(filepath.Dir(file), 0700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(file, []byte(content), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return file
|
|
}
|
|
fragment := write("shared/fragment.yml", "parent: {resource: asset.txt, enabled: false, count: 0}\n")
|
|
write("shared/asset.txt", "external resource")
|
|
main := write("app/main.yml", "config:\n imports:\n - path: "+strconv.Quote(fragment)+"\n destination: [entries]\nentries:\n child: {extends: parent}\n")
|
|
load := func(reject bool) configkit.Inspection {
|
|
options := configkit.Options[inheritedConfiguration]{Imports: true, Definition: configkit.Definition[inheritedConfiguration]{
|
|
Sensitive: func(p configkit.Path) bool { return strings.HasSuffix(p.String(), "/resource") },
|
|
Validate: func(_ *inheritedConfiguration, v *configkit.Validation) {
|
|
if reject {
|
|
v.Error(configkit.Keys("entries", "child"), "rejected")
|
|
}
|
|
},
|
|
}}
|
|
result, err := configkit.Load(configkit.File(main), options)
|
|
if reject {
|
|
var failure *configkit.LoadError
|
|
if result != nil || !errors.As(err, &failure) {
|
|
t.Fatalf("rejected=%v, %v", result, err)
|
|
}
|
|
view, ok := failure.InspectSubtree(configkit.Path{})
|
|
if !ok {
|
|
t.Fatal("missing failed inspection")
|
|
}
|
|
return view
|
|
}
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if child := result.Config.Entries["child"]; child.Resource != "asset.txt" || child.Enabled || child.Count != 0 {
|
|
t.Fatalf("child=%#v", child)
|
|
}
|
|
view, ok := result.InspectSubtree(configkit.Path{})
|
|
if !ok {
|
|
t.Fatal("missing accepted inspection")
|
|
}
|
|
return view
|
|
}
|
|
accepted, rejected := load(false), load(true)
|
|
if difference, err := accepted.Diff(rejected); err != nil || len(difference) != 0 {
|
|
t.Fatalf("failure changed evidence=%#v, %v", difference, err)
|
|
}
|
|
for _, view := range []configkit.Inspection{accepted, rejected} {
|
|
resource := configkit.Keys("entries", "child", "resource")
|
|
info, ok := view.Metadata().Lookup(resource)
|
|
if !ok || len(info.Origin.Locations) == 0 || len(info.Origin.Inheritance) == 0 {
|
|
t.Fatalf("inherited origin=%#v", info)
|
|
}
|
|
id := info.Origin.Locations[0].SourceID
|
|
context, ok := view.SourceContext(id)
|
|
if !ok || context.BaseDir != "." {
|
|
t.Fatalf("external context=%#v, %v", context, ok)
|
|
}
|
|
data, err := fs.ReadFile(context.FS, path.Join(context.BaseDir, "asset.txt"))
|
|
if err != nil || string(data) != "external resource" {
|
|
t.Fatalf("resource=%q, %v", data, err)
|
|
}
|
|
sources := view.Sources()
|
|
if len(sources) != 2 || sources[1].ID != id || sources[1].Destination.String() != "/entries" {
|
|
t.Fatalf("placement=%#v", sources)
|
|
}
|
|
explanation, ok := view.Explain(resource)
|
|
if !ok || len(explanation.History) == 0 || !explanation.Effective.Redacted {
|
|
t.Fatalf("explanation=%#v", explanation)
|
|
}
|
|
var output bytes.Buffer
|
|
if err := view.WriteEffective(&output); err != nil || strings.Contains(output.String(), "asset.txt") {
|
|
t.Fatalf("unsafe inspection=%q, %v", output.String(), err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestAbsoluteImportUsesSourceVersionProcessing(t *testing.T) {
|
|
t.Parallel()
|
|
root := t.TempDir()
|
|
child := filepath.Join(root, "versioned.yml")
|
|
if err := os.WriteFile(child, []byte("version: v1\nvalue: imported\n"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
main := filepath.Join(t.TempDir(), "main.yml")
|
|
if err := os.WriteFile(main, []byte("config: {imports: [{path: "+strconv.Quote(child)+"}]}\nversion: v1\n"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
checked := 0
|
|
result, err := configkit.Load[map[string]any](configkit.File(main), configkit.Options[map[string]any]{Imports: true, SourceVersionMode: configkit.SourceVersionModeIfPresent, Definition: configkit.Definition[map[string]any]{Version: &configkit.VersionRule{Path: configkit.Keys("version"), Check: func(version configkit.Version, _ *configkit.Validation) error {
|
|
checked++
|
|
if version.Text != "v1" {
|
|
return errors.New("unexpected version")
|
|
}
|
|
return nil
|
|
}}}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
sources := result.Sources()
|
|
if checked != 2 || len(sources) != 2 || sources[1].VersionEvidence == nil || sources[1].VersionEvidence.Version.Text != "v1" || result.Config["value"] != "imported" {
|
|
t.Fatalf("checks=%d, sources=%#v, config=%#v", checked, sources, result.Config)
|
|
}
|
|
}
|