70 lines
3.0 KiB
Go
70 lines
3.0 KiB
Go
package consumers
|
|
|
|
import (
|
|
"io/fs"
|
|
"path"
|
|
"testing"
|
|
"testing/fstest"
|
|
|
|
configkit "go.maximumdirect.net/configkit"
|
|
)
|
|
|
|
func TestComposedConsumersShareOneLoad(t *testing.T) {
|
|
filesystem := configkit.NewFilesystem("components", fstest.MapFS{
|
|
"config/component.yml": &fstest.MapFile{Data: []byte("name: ' component '\nresource: asset.txt\nentries:\n - {id: parent, resource: asset.txt}\n - {id: child, extends: {id: parent}}\n")},
|
|
"config/asset.txt": &fstest.MapFile{Data: []byte("shared asset")},
|
|
})
|
|
component := configkit.Input{Source: filesystem.File("config/component.yml")}
|
|
load := func(main string) *configkit.Result[ComposedApplication] {
|
|
t.Helper()
|
|
result, err := LoadComposedApplication(configkit.Input{Source: configkit.Bytes("app.yml", []byte(main))}, component)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return result
|
|
}
|
|
first := load("label: ' app '\naudit: {enabled: false, rules: [{id: first}]}\n")
|
|
second := load("label: second\naudit: {rules: [{id: second}]}\n")
|
|
componentRuntime, err := NewComponent(first)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
audit, err := NewAuditSettings(first)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if first.Config.Label != "app" || componentRuntime.Settings.Name != "component" || string(componentRuntime.Resource) != "shared asset" || audit.Enabled || audit.Rules[0].ID != "first" {
|
|
t.Fatalf("first result = %#v %#v", componentRuntime, audit)
|
|
}
|
|
secondAudit, err := NewAuditSettings(second)
|
|
if err != nil || !secondAudit.Enabled || secondAudit.Rules[0].ID != "second" || audit.Enabled {
|
|
t.Fatalf("second audit = %#v, %v", secondAudit, err)
|
|
}
|
|
resource := configkit.Keys("component", "entries").Index(1).Key("resource")
|
|
info, ok := first.Metadata().Lookup(resource)
|
|
if !ok || len(info.Origin.Inheritance) == 0 || len(info.Origin.Locations) == 0 {
|
|
t.Fatalf("inherited origin = %#v, %v", info, ok)
|
|
}
|
|
context, ok := first.SourceContext(info.Origin.Locations[0].SourceID)
|
|
if !ok {
|
|
t.Fatal("resource context unavailable")
|
|
}
|
|
data, err := fs.ReadFile(context.FS, path.Join(context.BaseDir, first.Config.Component.Entries[1].Resource))
|
|
if err != nil || string(data) != "shared asset" {
|
|
t.Fatalf("inherited resource = %q, %v", data, err)
|
|
}
|
|
_, err = LoadComposedApplication(configkit.Input{Source: configkit.Bytes("invalid.yml", []byte("label: app\naudit: {rules: [{id: same}, {id: same}]}\n"))}, component)
|
|
if err == nil {
|
|
t.Fatal("duplicate audit identities accepted")
|
|
}
|
|
badComponent := configkit.Input{Source: configkit.Bytes("bad-component.yml", []byte("name: good\nentries: [{id: duplicate}, {id: duplicate}]\n"))}
|
|
_, err = LoadComposedApplication(configkit.Input{Source: configkit.Bytes("app.yml", []byte("label: app\naudit: {rules: [{id: valid}]}\n"))}, badComponent)
|
|
if err == nil {
|
|
t.Fatal("duplicate component identities accepted")
|
|
}
|
|
_, err = LoadComposedApplication(configkit.Input{Source: configkit.Bytes("invalid.yml", []byte("label: ' '\naudit: {rules: [{id: valid}]}\n"))}, component)
|
|
if err == nil {
|
|
t.Fatal("application validation did not run")
|
|
}
|
|
}
|