Preserve captured filesystem contexts during loading
This commit is contained in:
@@ -78,6 +78,13 @@ func acquireInput(request inputAcquisitionRequest, policy acquisitionPolicy, sta
|
||||
}
|
||||
return input, diagnostics
|
||||
}
|
||||
if selectedSource.captured != nil {
|
||||
resolved, diagnostic := resolveFilesystemContext(selectedSource, request.input.filesystem, parsed, tier.hostToken)
|
||||
if diagnostic != nil {
|
||||
return input, []Diagnostic{*diagnostic}
|
||||
}
|
||||
request.resolved = &resolved
|
||||
}
|
||||
directiveContext := request.directiveContext
|
||||
if directiveContext == 0 {
|
||||
directiveContext = rootDirectiveContextImportsOnly
|
||||
@@ -130,8 +137,10 @@ func acquireStandaloneInput(input assembledInput, request inputAcquisitionReques
|
||||
input.document = processing.payload
|
||||
}
|
||||
if request.resolved != nil {
|
||||
if diagnostic := validateResolvedFilesystemContext(*request.resolved); diagnostic != nil {
|
||||
return input, []Diagnostic{*diagnostic}
|
||||
if input.source.captured == nil || request.input.filesystem != nil {
|
||||
if diagnostic := validateResolvedFilesystemContext(*request.resolved); diagnostic != nil {
|
||||
return input, []Diagnostic{*diagnostic}
|
||||
}
|
||||
}
|
||||
input.resolvedFilesystem = request.resolved
|
||||
}
|
||||
|
||||
@@ -17,6 +17,9 @@ type resolvedFilesystemContext struct {
|
||||
}
|
||||
|
||||
func resolveFilesystemContext(source Source, context *FilesystemContext, main parsedSource, hostToken *namespaceToken) (resolvedFilesystemContext, *Diagnostic) {
|
||||
if source.captured != nil {
|
||||
return resolveCapturedFilesystemContext(source, context, main, hostToken)
|
||||
}
|
||||
if source.kind == sourceNone && context != nil {
|
||||
return resolveAbsentFilesystemContext(context)
|
||||
}
|
||||
@@ -26,6 +29,30 @@ func resolveFilesystemContext(source Source, context *FilesystemContext, main pa
|
||||
return resolveHostFilesystemContext(source, context, main, hostToken)
|
||||
}
|
||||
|
||||
func resolveCapturedFilesystemContext(source Source, context *FilesystemContext, main parsedSource, hostToken *namespaceToken) (resolvedFilesystemContext, *Diagnostic) {
|
||||
captured := source.captured
|
||||
resolved := resolvedFilesystemContext{hostToken: hostToken, rootPath: captured.rootPath, basePath: captured.basePath, mainPath: captured.mainPath}
|
||||
if context == nil {
|
||||
return resolved, nil
|
||||
}
|
||||
if context.FS != nil || context.BaseDir != "" {
|
||||
value := optionDiagnostic(CodeOptionsInvalid, "prepared source requires a compatible host filesystem context")
|
||||
return resolvedFilesystemContext{}, &value
|
||||
}
|
||||
// The source record carries the captured canonical main path, so this does
|
||||
// not follow the original filename again after preparation.
|
||||
explicit, diagnostic := resolveHostFilesystemContext(source, context, main, hostToken)
|
||||
if diagnostic != nil || !sameHostPath(explicit.rootPath, resolved.rootPath) || !sameHostPath(explicit.basePath, resolved.basePath) || !sameHostPath(explicit.mainPath, resolved.mainPath) {
|
||||
value := optionDiagnostic(CodeOptionsInvalid, "prepared source requires a compatible host filesystem context")
|
||||
return resolvedFilesystemContext{}, &value
|
||||
}
|
||||
return resolved, nil
|
||||
}
|
||||
|
||||
func sameHostPath(left, right string) bool {
|
||||
return containedPath(left, right) && containedPath(right, left)
|
||||
}
|
||||
|
||||
func resolveAbsentFilesystemContext(context *FilesystemContext) (resolvedFilesystemContext, *Diagnostic) {
|
||||
if context.FS != nil {
|
||||
return resolveNativeFilesystemContext(Source{kind: sourceNone}, context)
|
||||
|
||||
103
internal/engine/prepared_context_test.go
Normal file
103
internal/engine/prepared_context_test.go
Normal file
@@ -0,0 +1,103 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user