Parse main-scoped secrets bootstrap controls
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package configkit_test
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"testing/fstest"
|
||||
@@ -8,6 +10,72 @@ import (
|
||||
configkit "go.maximumdirect.net/configkit"
|
||||
)
|
||||
|
||||
func TestMainBootstrapControlsAreConsumedWithoutEnvironmentWrites(t *testing.T) {
|
||||
t.Setenv("CONFIGKIT_CONTROL_SENTINEL", "unchanged")
|
||||
type nested struct {
|
||||
Config struct {
|
||||
Directory string `yaml:"secrets_dir"`
|
||||
} `yaml:"config"`
|
||||
}
|
||||
type app struct {
|
||||
Service nested `yaml:"service"`
|
||||
}
|
||||
for _, imports := range []bool{false, true} {
|
||||
body := "config: {secrets_dir: missing, secrets_overwrite: true}\nservice: {config: {secrets_dir: ordinary}}\n"
|
||||
fs := configkit.NewFilesystem("bundle", fstest.MapFS{"main.yml": {Data: []byte(body)}})
|
||||
result, err := configkit.Load[app](fs.File("main.yml"), configkit.Options[app]{Imports: imports})
|
||||
if err != nil || result.Config.Service.Config.Directory != "ordinary" || os.Getenv("CONFIGKIT_CONTROL_SENTINEL") != "unchanged" {
|
||||
t.Fatalf("imports %v: result = %#v, error = %v", imports, result, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestBootstrapControlsRejectMisplacedAndMalformedValues(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
name, body string
|
||||
code configkit.Code
|
||||
path string
|
||||
}{
|
||||
{"directory", "config: {secrets_dir: null}\n", configkit.CodeSecretsInvalidPath, "/config/secrets_dir"},
|
||||
{"overwrite", "config: {secrets_overwrite: 'false'}\n", configkit.CodeSecretsInvalidValue, "/config/secrets_overwrite"},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
_, err := configkit.Load[struct{}](configkit.Bytes("main", []byte(test.body)))
|
||||
var loadErr *configkit.LoadError
|
||||
if !errors.As(err, &loadErr) || len(loadErr.Diagnostics()) != 1 || loadErr.Diagnostics()[0].Code != test.code || loadErr.Diagnostics()[0].Path.String() != test.path || loadErr.Diagnostics()[0].Primary == nil {
|
||||
t.Fatalf("error = %#v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
fs := configkit.NewFilesystem("bundle", fstest.MapFS{
|
||||
"main.yml": {Data: []byte("config: {imports: [{path: other.yml}]}\n")},
|
||||
"other.yml": {Data: []byte("config: {secrets_overwrite: false}\n")},
|
||||
})
|
||||
_, err := configkit.Load[struct{}](fs.File("main.yml"), configkit.Options[struct{}]{Imports: true})
|
||||
var loadErr *configkit.LoadError
|
||||
if !errors.As(err, &loadErr) || len(loadErr.Diagnostics()) != 1 || loadErr.Diagnostics()[0].Code != configkit.CodeImportInvalid || loadErr.Diagnostics()[0].Path.String() != "/config/secrets_overwrite" {
|
||||
t.Fatalf("import error = %#v", err)
|
||||
}
|
||||
_, err = configkit.LoadLayered(configkit.Input{Source: configkit.Bytes("main", []byte("{}\n"))}, configkit.LayeredOptions[struct{}]{
|
||||
Underlays: []configkit.Underlay{{Inputs: []configkit.Input{{Source: configkit.Bytes("underlay", []byte("config: {secrets_dir: secrets}\n"))}}}},
|
||||
})
|
||||
if !errors.As(err, &loadErr) || len(loadErr.Diagnostics()) != 1 || loadErr.Diagnostics()[0].Code != configkit.CodeImportInvalid {
|
||||
t.Fatalf("underlay error = %#v", err)
|
||||
}
|
||||
overlays := configkit.NewFilesystem("overlays", fstest.MapFS{
|
||||
"main.yml": {Data: []byte("{}\n")},
|
||||
"overlays/dev.yml": {Data: []byte("config: {secrets_overwrite: false}\n")},
|
||||
})
|
||||
_, err = configkit.Load[struct{}](overlays.File("main.yml"), configkit.Options[struct{}]{Overlay: "dev"})
|
||||
if !errors.As(err, &loadErr) || len(loadErr.Diagnostics()) != 1 || loadErr.Diagnostics()[0].Code != configkit.CodeOverlayDirective || loadErr.Diagnostics()[0].Primary == nil {
|
||||
t.Fatalf("overlay error = %#v", err)
|
||||
}
|
||||
_, err = configkit.Load[struct{}](configkit.Bytes("main", []byte("config: {imports: [], secrets_overwrite: false}\n")))
|
||||
if !errors.As(err, &loadErr) || len(loadErr.Diagnostics()) != 1 || loadErr.Diagnostics()[0].Code != configkit.CodeImportDisabled {
|
||||
t.Fatalf("disabled imports error = %#v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDocumentRootControlsAndNestedApplicationConfig(t *testing.T) {
|
||||
type settings struct {
|
||||
Imports string `yaml:"imports"`
|
||||
|
||||
Reference in New Issue
Block a user