-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain_test.go
353 lines (313 loc) · 9.41 KB
/
main_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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package main
import (
"fmt"
"os"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/scylladb/go-set"
"github.com/scylladb/go-set/strset"
)
// (Ab)use the "Example" feature of the testing package to assert on the
// output of the program. See https://pkg.go.dev/testing#hdr-Examples
func Example_version() {
os.Args = []string{"terravalet", "version"}
if err := run(); err != nil {
panic(err)
}
// Output:
// terravalet unknown
}
func TestParseSuccess(t *testing.T) {
testCases := []struct {
name string
line string
wantCreate *strset.Set
wantDestroy *strset.Set
}{
{
name: "destroyed is recorded",
line: " # aws_instance.bar will be destroyed",
wantCreate: set.NewStringSet(),
wantDestroy: set.NewStringSet("aws_instance.bar"),
},
{
name: "created is recorded",
line: " # aws_instance.bar will be created",
wantCreate: set.NewStringSet("aws_instance.bar"),
wantDestroy: set.NewStringSet(),
},
{
name: "read is skipped",
line: " # data.foo.bar will be read during apply",
wantCreate: set.NewStringSet(),
wantDestroy: set.NewStringSet(),
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
rd := strings.NewReader(tc.line)
haveCreate, haveDestroy, err := parse(rd)
if err != nil {
t.Fatalf("\nhave: %q\nwant: no error", err)
}
if diff := cmp.Diff(tc.wantCreate, haveCreate, setCmp); diff != "" {
t.Errorf("\ncreate: mismatch (-want +have):\n%s", diff)
}
if diff := cmp.Diff(tc.wantDestroy, haveDestroy, setCmp); diff != "" {
t.Errorf("\ndestroy: mismatch (-want +have):\n%s", diff)
}
})
}
}
func TestParseFailure(t *testing.T) {
testCases := []struct {
name string
line string
wantErr string
}{
{
name: "vaporized is not an expected action",
line: " # aws_instance.bar will be vaporized",
wantErr: `line " # aws_instance.bar will be vaporized", unexpected action "vaporized"`,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
rd := strings.NewReader(tc.line)
_, _, err := parse(rd)
if err == nil {
t.Fatalf("\nhave: no error\nwant: %q", tc.wantErr)
}
if diff := cmp.Diff(tc.wantErr, err.Error()); diff != "" {
t.Errorf("error message mismatch (-want +have):\n%s", diff)
}
})
}
}
func TestMatchExactZeroUnmatched(t *testing.T) {
testCases := []struct {
name string
create *strset.Set
destroy *strset.Set
wantUpMatches map[string]string
wantDownMatches map[string]string
}{
{
name: "increase depth, len 1",
create: set.NewStringSet("a.b"),
destroy: set.NewStringSet("b"),
wantUpMatches: map[string]string{"b": "a.b"},
wantDownMatches: map[string]string{"a.b": "b"},
},
{
name: "decrease depth, len 1",
create: set.NewStringSet("b"),
destroy: set.NewStringSet("a.b"),
wantUpMatches: map[string]string{"a.b": "b"},
wantDownMatches: map[string]string{"b": "a.b"},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
haveUpMatches, haveDownMatches := matchExact(tc.create, tc.destroy)
if diff := cmp.Diff(tc.wantUpMatches, haveUpMatches); diff != "" {
t.Errorf("\nupMatches: mismatch (-want +have):\n%s", diff)
}
if diff := cmp.Diff(tc.wantDownMatches, haveDownMatches); diff != "" {
t.Errorf("\ndownMatches: mismatch (-want +have):\n%s", diff)
}
if have := tc.create.Size(); have != 0 {
t.Errorf("\nsize(create): have: %d; want: 0", have)
}
if have := tc.destroy.Size(); have != 0 {
t.Errorf("\nsize(destroy): have: %d; want: 0", have)
}
})
}
}
func TestMatchExactSomeUnmatched(t *testing.T) {
testCases := []struct {
name string
create *strset.Set
destroy *strset.Set
wantCreate *strset.Set
wantDestroy *strset.Set
}{
{
name: "len(create) == len(destroy), no match",
create: set.NewStringSet("a.b"),
destroy: set.NewStringSet("j.k"),
wantCreate: set.NewStringSet("a.b"),
wantDestroy: set.NewStringSet("j.k"),
},
{
name: "len(create) > len(destroy), match",
create: set.NewStringSet("a.b", "a.j.k"),
destroy: set.NewStringSet("j.k"),
wantCreate: set.NewStringSet("a.b"),
wantDestroy: set.NewStringSet(),
},
{
name: "len(create) < len(destroy), match",
create: set.NewStringSet("a.b"),
destroy: set.NewStringSet("j.k", "x.a.b"),
wantCreate: set.NewStringSet(),
wantDestroy: set.NewStringSet("j.k"),
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
matchExact(tc.create, tc.destroy)
if diff := cmp.Diff(tc.wantCreate, tc.create, setCmp); diff != "" {
t.Errorf("\nUnmatched create: (-want +have):\n%s", diff)
}
if diff := cmp.Diff(tc.wantDestroy, tc.destroy, setCmp); diff != "" {
t.Errorf("\nUnmatched destroy (-want +have):\n%s", diff)
}
})
}
}
func TestMatchFuzzyZeroUnmatched(t *testing.T) {
testCases := []struct {
name string
create *strset.Set
destroy *strset.Set
wantUpMatches map[string]string
wantDownMatches map[string]string
}{
{
name: "1 fuzzy match",
create: set.NewStringSet(`foo.loopback["bar"]`),
destroy: set.NewStringSet(`foo.bar_loopback`),
wantUpMatches: map[string]string{`foo.bar_loopback`: `foo.loopback["bar"]`},
wantDownMatches: map[string]string{`foo.loopback["bar"]`: `foo.bar_loopback`},
},
{
name: "3 fuzzy matches",
create: set.NewStringSet(
`foo.loopback["bar"]`,
`foo.private["bar"]`,
`foo.public["bar"]`),
destroy: set.NewStringSet(
`foo.bar_loopback`,
`foo.bar_private`,
`foo.bar`),
wantUpMatches: map[string]string{
`foo.bar_loopback`: `foo.loopback["bar"]`,
`foo.bar_private`: `foo.private["bar"]`,
`foo.bar`: `foo.public["bar"]`},
wantDownMatches: map[string]string{
`foo.loopback["bar"]`: `foo.bar_loopback`,
`foo.private["bar"]`: `foo.bar_private`,
`foo.public["bar"]`: `foo.bar`},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
haveUpMatches, haveDownMatches, err := matchFuzzy(tc.create, tc.destroy)
if err != nil {
t.Fatalf("have: %s; want: no error", err)
}
if diff := cmp.Diff(tc.wantUpMatches, haveUpMatches); diff != "" {
t.Errorf("\nupMatches: mismatch (-want +have):\n%s", diff)
}
if diff := cmp.Diff(tc.wantDownMatches, haveDownMatches); diff != "" {
t.Errorf("\ndownMatches: mismatch (-want +have):\n%s", diff)
}
if have := tc.create.Size(); have != 0 {
t.Errorf("\nsize(create): have: %d; want: 0", have)
}
if have := tc.destroy.Size(); have != 0 {
t.Errorf("\nsize(destroy): have: %d; want: 0", have)
}
})
}
}
func TestMatchFuzzyError(t *testing.T) {
create := set.NewStringSet(`abcde`, `abdecde`)
destroy := set.NewStringSet(`abdcde`, `hfjabd`)
_, _, err := matchFuzzy(create, destroy)
if err == nil {
t.Fatalf("have: no error; want: an ambiguous migration error")
}
haveMsg := err.Error()
var msg string
want := "ambiguous migration:"
if !strings.HasPrefix(haveMsg, want) {
msg += fmt.Sprintf("error message does not start with %q\n", want)
}
want = "{abcde} -> {abdcde}"
if !strings.Contains(haveMsg, want) {
msg += fmt.Sprintf("error message does not contain %q", want)
}
want = "{abdecde} -> {abdcde}"
if !strings.Contains(haveMsg, want) {
msg += fmt.Sprintf("error message does not contain %q", want)
}
if msg != "" {
t.Fatal(msg)
}
}
// Used to compare sets.
var setCmp = cmp.Comparer(func(s1, s2 *strset.Set) bool {
return s1.IsEqual(s2)
})
func runSuccess(t *testing.T, args []string, wantUpPath string, wantDownPath string) {
wantUp, err := os.ReadFile(wantUpPath)
if err != nil {
t.Fatalf("reading want up file: %v", err)
}
wantDown, err := os.ReadFile(wantDownPath)
if err != nil {
t.Fatalf("reading want down file: %v", err)
}
tmpDir, err := os.MkdirTemp("", "terravalet")
if err != nil {
t.Fatalf("creating temporary dir: %v", err)
}
defer os.RemoveAll(tmpDir)
tmpUpPath := tmpDir + "/up"
tmpDownPath := tmpDir + "/down"
args = append(args, "--up", tmpUpPath, "--down", tmpDownPath)
os.Args = args
if err := run(); err != nil {
t.Fatalf("run: args: %s\nhave: %q\nwant: no error", args, err)
}
tmpUp, err := os.ReadFile(tmpUpPath)
if err != nil {
t.Fatalf("reading tmp up file: %v", err)
}
tmpDown, err := os.ReadFile(tmpDownPath)
if err != nil {
t.Fatalf("reading tmp down file: %v", err)
}
if diff := cmp.Diff(string(wantUp), string(tmpUp)); diff != "" {
t.Errorf("\nup script: mismatch (-want +have):\n"+
"(want path: %s)\n"+
"%s", wantUpPath, diff)
}
if diff := cmp.Diff(string(wantDown), string(tmpDown)); diff != "" {
t.Errorf("\ndown script: mismatch (-want +have):\n"+
"(want path: %s)\n"+
"%s", wantDownPath, diff)
}
}
func runFailure(t *testing.T, args []string, wantErr string) {
tmpDir, err := os.MkdirTemp("", "terravalet")
if err != nil {
t.Fatalf("creating temporary dir: %v", err)
}
defer os.RemoveAll(tmpDir)
tmpUpPath := tmpDir + "/up"
tmpDownPath := tmpDir + "/down"
args = append(args, "--up", tmpUpPath, "--down", tmpDownPath)
os.Args = args
err = run()
if err == nil {
t.Fatalf("run: args: %s\nhave: no error\nwant: %q", args, wantErr)
}
if diff := cmp.Diff(wantErr, err.Error()); diff != "" {
t.Errorf("error message mismatch (-want +have):\n%s", diff)
}
}