mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-30 10:47:14 -06:00
eb7aaf2414
Here we introduce a new idea of a "configuration snapshot", which is an in-memory copy of the source code of each of the files that make up the configuration. The primary intended purpose for this is as an intermediate step before writing the configuration files into a plan file, and then reading them out when that plan file is later applied. During earlier configs package development we expected to use an afero vfs implementation to read directly from the zip file, but that doesn't work in practice because we need to preserve module paths from the source file system that might include parent directory traversals (../) while retaining the original path for use in error messages. The result, for now, is a bit of an abstraction inversion: we implement a specialized afero vfs implementation that makes the sparse filesystem representation from a snapshot appear like a normal filesystem just well enough that the config loader and parser can work with it. In future we may wish to rework the internals here so that the main abstraction is at a similar level to the snapshot and then that API is mapped to the native filesystem in the normal case, removing afero. For now though, this approach avoids the need for a significant redesign of the parser/loader internals, at the expense of some trickiness in the case where we're reading from a snapshot. This commit does not yet include the reading and writing of snapshots into plan files. That will follow in a subsequent commit.
122 lines
3.3 KiB
Go
122 lines
3.3 KiB
Go
package configload
|
|
|
|
import (
|
|
"path/filepath"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"github.com/davecgh/go-spew/spew"
|
|
"github.com/go-test/deep"
|
|
)
|
|
|
|
func TestLoadConfigWithSnapshot(t *testing.T) {
|
|
fixtureDir := filepath.Clean("test-fixtures/already-installed")
|
|
loader, err := NewLoader(&Config{
|
|
ModulesDir: filepath.Join(fixtureDir, ".terraform/modules"),
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("unexpected error from NewLoader: %s", err)
|
|
}
|
|
|
|
_, got, diags := loader.LoadConfigWithSnapshot(fixtureDir)
|
|
assertNoDiagnostics(t, diags)
|
|
if got == nil {
|
|
t.Fatalf("snapshot is nil; want non-nil")
|
|
}
|
|
|
|
t.Log(spew.Sdump(got))
|
|
|
|
{
|
|
gotModuleDirs := map[string]string{}
|
|
for k, m := range got.Modules {
|
|
gotModuleDirs[k] = m.Dir
|
|
}
|
|
wantModuleDirs := map[string]string{
|
|
"": "test-fixtures/already-installed",
|
|
"child_a": "test-fixtures/already-installed/.terraform/modules/child_a",
|
|
"child_a.child_c": "test-fixtures/already-installed/.terraform/modules/child_a/child_c",
|
|
"child_b": "test-fixtures/already-installed/.terraform/modules/child_b",
|
|
"child_b.child_d": "test-fixtures/already-installed/.terraform/modules/child_b.child_d",
|
|
}
|
|
|
|
problems := deep.Equal(wantModuleDirs, gotModuleDirs)
|
|
for _, problem := range problems {
|
|
t.Errorf(problem)
|
|
}
|
|
if len(problems) > 0 {
|
|
return
|
|
}
|
|
}
|
|
|
|
gotRoot := got.Modules[""]
|
|
wantRoot := &SnapshotModule{
|
|
Dir: "test-fixtures/already-installed",
|
|
Files: map[string][]byte{
|
|
"root.tf": []byte(`
|
|
module "child_a" {
|
|
source = "example.com/foo/bar_a/baz"
|
|
version = ">= 1.0.0"
|
|
}
|
|
|
|
module "child_b" {
|
|
source = "example.com/foo/bar_b/baz"
|
|
version = ">= 1.0.0"
|
|
}
|
|
`),
|
|
},
|
|
}
|
|
if !reflect.DeepEqual(gotRoot, wantRoot) {
|
|
t.Errorf("wrong root module snapshot\ngot: %swant: %s", spew.Sdump(gotRoot), spew.Sdump(wantRoot))
|
|
}
|
|
|
|
}
|
|
|
|
func TestSnapshotRoundtrip(t *testing.T) {
|
|
fixtureDir := filepath.Clean("test-fixtures/already-installed")
|
|
loader, err := NewLoader(&Config{
|
|
ModulesDir: filepath.Join(fixtureDir, ".terraform/modules"),
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("unexpected error from NewLoader: %s", err)
|
|
}
|
|
|
|
_, snap, diags := loader.LoadConfigWithSnapshot(fixtureDir)
|
|
assertNoDiagnostics(t, diags)
|
|
if snap == nil {
|
|
t.Fatalf("snapshot is nil; want non-nil")
|
|
}
|
|
|
|
snapLoader := NewLoaderFromSnapshot(snap)
|
|
if loader == nil {
|
|
t.Fatalf("loader is nil; want non-nil")
|
|
}
|
|
|
|
config, diags := snapLoader.LoadConfig(fixtureDir)
|
|
assertNoDiagnostics(t, diags)
|
|
if config == nil {
|
|
t.Fatalf("config is nil; want non-nil")
|
|
}
|
|
if config.Module == nil {
|
|
t.Fatalf("config has no root module")
|
|
}
|
|
if got, want := config.Module.SourceDir, "test-fixtures/already-installed"; got != want {
|
|
t.Errorf("wrong root module sourcedir %q; want %q", got, want)
|
|
}
|
|
if got, want := len(config.Module.ModuleCalls), 2; got != want {
|
|
t.Errorf("wrong number of module calls in root module %d; want %d", got, want)
|
|
}
|
|
childA := config.Children["child_a"]
|
|
if childA == nil {
|
|
t.Fatalf("child_a config is nil; want non-nil")
|
|
}
|
|
if childA.Module == nil {
|
|
t.Fatalf("child_a config has no module")
|
|
}
|
|
if got, want := childA.Module.SourceDir, "test-fixtures/already-installed/.terraform/modules/child_a"; got != want {
|
|
t.Errorf("wrong child_a sourcedir %q; want %q", got, want)
|
|
}
|
|
if got, want := len(childA.Module.ModuleCalls), 1; got != want {
|
|
t.Errorf("wrong number of module calls in child_a %d; want %d", got, want)
|
|
}
|
|
}
|