104 lines
3.9 KiB
Go
104 lines
3.9 KiB
Go
package engine
|
|
|
|
import (
|
|
"errors"
|
|
"io/fs"
|
|
"os"
|
|
"path/filepath"
|
|
"reflect"
|
|
"runtime"
|
|
"testing"
|
|
"testing/fstest"
|
|
)
|
|
|
|
func TestCapturedMainUsesItsContextDuringOrdinaryLoad(t *testing.T) {
|
|
root := t.TempDir()
|
|
main := writeImportFile(t, root, "app/main.yml", "settings: {main: retained}\n")
|
|
writeImportFile(t, root, "app/resource.txt", "resource")
|
|
writeImportFile(t, root, "overlays/dev.yml", "settings: {overlay: applied}\n")
|
|
context := &FilesystemContext{Root: root}
|
|
prepared, _, diagnostics := captureHostMain(File(main), context)
|
|
if len(diagnostics) != 0 {
|
|
t.Fatal(diagnostics)
|
|
}
|
|
options := Options[discoveryLoadConfig]{Filesystem: context, Destination: Keys("service"), Overlay: "dev"}
|
|
ordinary, err := testLoad(File(main), options)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
options.Filesystem = nil
|
|
captured, err := testLoad(prepared, options)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !reflect.DeepEqual(captured.Config, ordinary.Config) || !reflect.DeepEqual(captured.Sources(), ordinary.Sources()) {
|
|
t.Fatalf("ordinary = %#v, captured = %#v, sources = %#v / %#v", ordinary.Config, captured.Config, ordinary.Sources(), captured.Sources())
|
|
}
|
|
resource, ok := captured.Inspection.SourceContext("source-1")
|
|
if !ok || resource.BaseDir != "app" {
|
|
t.Fatalf("resource context = %#v, %v", resource, ok)
|
|
}
|
|
data, err := fs.ReadFile(resource.FS, "app/resource.txt")
|
|
if err != nil || string(data) != "resource" {
|
|
t.Fatalf("resource = %q, %v", data, err)
|
|
}
|
|
if err := os.Remove(main); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, filesystem := range []*FilesystemContext{nil, {Root: root}} {
|
|
options.Filesystem = filesystem
|
|
loaded, err := testLoad(prepared, options)
|
|
if err != nil || !reflect.DeepEqual(loaded.Config, captured.Config) {
|
|
t.Fatalf("load after main removal: %#v, %v", loaded, err)
|
|
}
|
|
}
|
|
for _, filesystem := range []*FilesystemContext{{Root: filepath.Join(root, "app")}, {Root: root, BaseDir: root}, {Root: ".", FS: filesystemPointer(NewFilesystem("native", fstest.MapFS{}))}} {
|
|
for _, imports := range []bool{false, true} {
|
|
options.Filesystem, options.Imports = filesystem, imports
|
|
_, err := testLoad(prepared, options)
|
|
var loadErr *LoadError
|
|
if !errors.As(err, &loadErr) || len(loadErr.Diagnostics()) == 0 || loadErr.Diagnostics()[0].Code != CodeOptionsInvalid {
|
|
t.Fatalf("incompatible context %#v, imports %v: %v", filesystem, imports, err)
|
|
}
|
|
}
|
|
}
|
|
_, err = LoadLayered(Input{Source: prepared, Filesystem: &FilesystemContext{Root: filepath.Join(root, "app")}}, testSecretBinding(), LayeredOptions[discoveryLoadConfig]{Overlay: "missing"})
|
|
var loadErr *LoadError
|
|
if !errors.As(err, &loadErr) || len(loadErr.Diagnostics()) == 0 || loadErr.Diagnostics()[0].Code != CodeOptionsInvalid {
|
|
t.Fatalf("incompatible layered context: %v", err)
|
|
}
|
|
}
|
|
|
|
func filesystemPointer(filesystem Filesystem) *Filesystem { return &filesystem }
|
|
|
|
func TestCapturedMainUsesCanonicalTargetAfterSymlinkChanges(t *testing.T) {
|
|
if runtime.GOOS == "windows" {
|
|
t.Skip("symlink creation may require privileges")
|
|
}
|
|
root := t.TempDir()
|
|
main := writeImportFile(t, root, "actual/main.yml", "value: original\n")
|
|
other := writeImportFile(t, root, "other.yml", "value: changed\n")
|
|
link := filepath.Join(root, "link.yml")
|
|
if err := os.Symlink(main, link); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
prepared, _, diagnostics := captureHostMain(File(link), &FilesystemContext{Root: root})
|
|
if len(diagnostics) != 0 {
|
|
t.Fatal(diagnostics)
|
|
}
|
|
if err := os.Remove(link); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Symlink(other, link); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
rootAlias := filepath.Join(t.TempDir(), "root-link")
|
|
if err := os.Symlink(root, rootAlias); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
loaded, err := testLoad(prepared, Options[discoveryLoadConfig]{Filesystem: &FilesystemContext{Root: rootAlias}})
|
|
if err != nil || loaded.Config.Value != "original" || loaded.Sources()[0].Path != canonicalTestPath(t, main) {
|
|
t.Fatalf("load = %#v, %v", loaded, err)
|
|
}
|
|
}
|