/
loader_snapshot_test.go
147 lines (129 loc) · 3.99 KB
/
loader_snapshot_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package configload
import (
"os"
"path/filepath"
"reflect"
"testing"
"github.com/davecgh/go-spew/spew"
"github.com/go-test/deep"
)
func TestLoadConfigWithSnapshot(t *testing.T) {
fixtureDir := filepath.Clean("testdata/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{
"": "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",
}
problems := deep.Equal(wantModuleDirs, gotModuleDirs)
for _, problem := range problems {
t.Errorf(problem)
}
if len(problems) > 0 {
return
}
}
gotRoot := got.Modules[""]
wantRoot := &SnapshotModule{
Dir: "testdata/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 TestLoadConfigWithSnapshot_invalidSource(t *testing.T) {
fixtureDir := filepath.Clean("testdata/already-installed-now-invalid")
old, _ := os.Getwd()
os.Chdir(fixtureDir)
defer os.Chdir(old)
loader, err := NewLoader(&Config{
ModulesDir: ".terraform/modules",
})
if err != nil {
t.Fatalf("unexpected error from NewLoader: %s", err)
}
_, _, diags := loader.LoadConfigWithSnapshot(".")
assertDiagnosticCount(t, diags, 2)
d := diags[0]
if got, want := d.Summary, "Variables not allowed"; got != want {
t.Errorf("Wrong diag summary\ngot: %s\nwant: %s", got, want)
}
if got, want := d.Subject.Filename, "foo/main.tf"; got != want {
t.Errorf("Wrong diag filename\ngot: %s\nwant: %s", got, want)
}
}
func TestSnapshotRoundtrip(t *testing.T) {
fixtureDir := filepath.Clean("testdata/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, "testdata/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, "testdata/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)
}
}