Document and demonstrate native and layered configuration loading
This commit is contained in:
71
examples/consumers/layered_test.go
Normal file
71
examples/consumers/layered_test.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package consumers_test
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
configkit "gitea.maximumdirect.net/eric/ConfigKit"
|
||||
"gitea.maximumdirect.net/eric/ConfigKit/examples/consumers"
|
||||
)
|
||||
|
||||
func Example_nativeFilesystem() {
|
||||
result, err := consumers.LoadBundled()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
fmt.Println(result.Config.Name, result.Config.Enabled)
|
||||
// Output: embedded false
|
||||
}
|
||||
|
||||
func Example_layeredCatalog() {
|
||||
result, err := consumers.LoadCatalog(fixture("layered-main", "config.yml"))
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
welcome, err := consumers.ReadWelcome(result)
|
||||
fmt.Println(result.Config.Mode, result.Config.Enabled, result.Config.Workers,
|
||||
result.Config.Labels["origin"], result.Config.Labels["tenant"], strings.TrimSpace(welcome), err)
|
||||
// Output: production false 0 bundle example Welcome from the bundle. <nil>
|
||||
}
|
||||
|
||||
func TestCatalogRetainsPresenceAndSourceContext(t *testing.T) {
|
||||
result, err := consumers.LoadCatalog(fixture("layered-main", "config.yml"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, key := range []string{"enabled", "workers"} {
|
||||
info, found := result.Metadata().Lookup(configkit.Keys(key))
|
||||
if !found || !info.Explicit || info.Origin.Layer != configkit.LayerBase {
|
||||
t.Fatalf("%s presence = %#v, %v", key, info, found)
|
||||
}
|
||||
}
|
||||
info, found := result.Metadata().Lookup(configkit.Keys("templates", "welcome"))
|
||||
if !found || info.Origin.Layer != configkit.LayerUnderlay || len(info.Origin.Locations) != 1 {
|
||||
t.Fatalf("welcome origin = %#v, %v", info, found)
|
||||
}
|
||||
context, found := result.SourceContext(info.Origin.Locations[0].SourceID)
|
||||
if !found || context.BaseDir != "defaults" {
|
||||
t.Fatalf("welcome context = %#v, %v", context, found)
|
||||
}
|
||||
welcome, err := consumers.ReadWelcome(result)
|
||||
if err != nil || strings.TrimSpace(welcome) != "Welcome from the bundle." {
|
||||
t.Fatalf("welcome = %q, %v", welcome, err)
|
||||
}
|
||||
if result.Config.Labels["tenant"] != "example" || result.Config.Labels["origin"] != "bundle" {
|
||||
t.Fatalf("recursive underlay labels = %#v", result.Config.Labels)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCatalogValidationRejectsInvalidMode(t *testing.T) {
|
||||
_, err := consumers.LoadCatalog(fixture("layered-main", "invalid.yml"))
|
||||
var loadError *configkit.LoadError
|
||||
if !errors.As(err, &loadError) || len(loadError.Diagnostics()) == 0 ||
|
||||
loadError.Diagnostics()[0].Code != configkit.CodeValidateError ||
|
||||
loadError.Diagnostics()[0].Path.String() != "/mode" {
|
||||
t.Fatalf("invalid mode error = %v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user