54 lines
1.9 KiB
Go
54 lines
1.9 KiB
Go
package consumers
|
|
|
|
import (
|
|
"bytes"
|
|
"strings"
|
|
"testing"
|
|
"testing/fstest"
|
|
|
|
configkit "go.maximumdirect.net/configkit"
|
|
)
|
|
|
|
func TestComponentHandoff(t *testing.T) {
|
|
filesystem := configkit.NewFilesystem("application", fstest.MapFS{
|
|
"config/main.yml": &fstest.MapFile{Data: []byte("component: {name: ' example ', resource: asset.txt, token: disposable-canary}\n")},
|
|
"config/asset.txt": &fstest.MapFile{Data: []byte("resource data")},
|
|
})
|
|
result, err := LoadComponentApplication(filesystem.File("config/main.yml"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
runtime, err := NewComponent(result)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if runtime.Settings.Name != "example" || len(runtime.Settings.Entries) != 1 || runtime.Settings.Entries[0].ID != "default" || string(runtime.Resource) != "resource data" {
|
|
t.Fatalf("component runtime = %#v", runtime)
|
|
}
|
|
entries, found := result.Metadata().Lookup(configkit.Keys("component", "entries"))
|
|
if !result.Metadata().IsExplicit(configkit.Keys("component", "resource")) || !found || entries.Origin.Layer != configkit.LayerUnderlay {
|
|
t.Fatal("presence did not retain source distinction")
|
|
}
|
|
view, ok := InspectComponent(result)
|
|
if !ok {
|
|
t.Fatal("component view unavailable")
|
|
}
|
|
var output bytes.Buffer
|
|
if err := view.WriteEffective(&output); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if strings.Contains(output.String(), "disposable-canary") || !strings.Contains(output.String(), "example") {
|
|
t.Fatalf("unsafe report: %s", output.String())
|
|
}
|
|
if _, ok := view.Explain(configkit.Keys("component", "resource")); !ok {
|
|
t.Fatal("resource explanation unavailable")
|
|
}
|
|
if diff, err := view.Diff(view); err != nil || len(diff) != 0 {
|
|
t.Fatalf("view diff = %#v, %v", diff, err)
|
|
}
|
|
_, err = LoadComponentApplication(configkit.Bytes("invalid.yml", []byte("component: {name: ' '}\n")))
|
|
if err == nil {
|
|
t.Fatal("component validation did not fail")
|
|
}
|
|
}
|