2018-06-16 02:23:53 +02:00
|
|
|
package configload
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path/filepath"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/davecgh/go-spew/spew"
|
|
|
|
"github.com/go-test/deep"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestLoadConfigWithSnapshot(t *testing.T) {
|
2019-06-30 09:38:36 +02:00
|
|
|
fixtureDir := filepath.Clean("testdata/already-installed")
|
2018-06-16 02:23:53 +02:00
|
|
|
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{
|
2019-06-30 09:38:36 +02:00
|
|
|
"": "testdata/already-installed",
|
|
|
|
"child_a": "testdata/already-installed/.terraform/modules/child_a",
|
|
|
|
"child_a.child_c": "testdata/already-installed/.terraform/modules/child_a/child_c",
|
|
|
|
"child_b": "testdata/already-installed/.terraform/modules/child_b",
|
|
|
|
"child_b.child_d": "testdata/already-installed/.terraform/modules/child_b.child_d",
|
2018-06-16 02:23:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
problems := deep.Equal(wantModuleDirs, gotModuleDirs)
|
|
|
|
for _, problem := range problems {
|
|
|
|
t.Errorf(problem)
|
|
|
|
}
|
|
|
|
if len(problems) > 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gotRoot := got.Modules[""]
|
|
|
|
wantRoot := &SnapshotModule{
|
2019-06-30 09:38:36 +02:00
|
|
|
Dir: "testdata/already-installed",
|
2018-06-16 02:23:53 +02:00
|
|
|
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) {
|
2019-06-30 09:38:36 +02:00
|
|
|
fixtureDir := filepath.Clean("testdata/already-installed")
|
2018-06-16 02:23:53 +02:00
|
|
|
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")
|
|
|
|
}
|
2019-06-30 09:38:36 +02:00
|
|
|
if got, want := config.Module.SourceDir, "testdata/already-installed"; got != want {
|
2018-06-16 02:23:53 +02:00
|
|
|
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")
|
|
|
|
}
|
2019-06-30 09:38:36 +02:00
|
|
|
if got, want := childA.Module.SourceDir, "testdata/already-installed/.terraform/modules/child_a"; got != want {
|
2018-06-16 02:23:53 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|