-
Notifications
You must be signed in to change notification settings - Fork 5
/
cmdmoverename_test.go
370 lines (330 loc) · 9.66 KB
/
cmdmoverename_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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
package main
import (
"fmt"
"io"
"os"
"path"
"path/filepath"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestRunRenameSuccess(t *testing.T) {
testCases := []struct {
name string
options []string
planPath string
wantUpPath string
wantDownPath string
}{
{
name: "exact match",
options: []string{},
planPath: "testdata/rename/01_exact-match.plan.txt",
wantUpPath: "testdata/rename/01_exact-match.up.sh",
wantDownPath: "testdata/rename/01_exact-match.down.sh",
},
{
name: "q-gram fuzzy match simple",
options: []string{"--fuzzy-match"},
planPath: "testdata/rename/02_fuzzy-match.plan.txt",
wantUpPath: "testdata/rename/02_fuzzy-match.up.sh",
wantDownPath: "testdata/rename/02_fuzzy-match.down.sh",
},
{
name: "q-gram fuzzy match complicated",
options: []string{"--fuzzy-match"},
planPath: "testdata/rename/03_fuzzy-match.plan.txt",
wantUpPath: "testdata/rename/03_fuzzy-match.up.sh",
wantDownPath: "testdata/rename/03_fuzzy-match.down.sh",
},
{
name: "q-gram fuzzy match complicated (regression)",
options: []string{"--fuzzy-match"},
planPath: "testdata/rename/07_fuzzy-match.plan.txt",
wantUpPath: "testdata/rename/07_fuzzy-match.up.sh",
wantDownPath: "testdata/rename/07_fuzzy-match.down.sh",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
args := []string{"terravalet", "rename", "--plan", tc.planPath}
args = append(args, tc.options...)
runSuccess(t, args, tc.wantUpPath, tc.wantDownPath)
})
}
}
func TestRunRenameFailure(t *testing.T) {
testCases := []struct {
name string
planPath string
wantErr string
}{
{
name: "plan file doesn't exist",
planPath: "nonexisting",
wantErr: "opening the terraform plan file: open nonexisting: no such file or directory",
},
{
name: "matchExact failure",
planPath: "testdata/rename/02_fuzzy-match.plan.txt",
wantErr: `matchExact:
unmatched create:
aws_route53_record.localhostnames_public["artifactory"]
aws_route53_record.loopback["artifactory"]
aws_route53_record.private["artifactory"]
unmatched destroy:
aws_route53_record.artifactory
aws_route53_record.artifactory_loopback
aws_route53_record.artifactory_private`,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
args := []string{"terravalet", "rename", "--plan", tc.planPath}
runFailure(t, args, tc.wantErr)
})
}
}
func TestRunMoveAfterSuccess(t *testing.T) {
testCases := []struct {
name string
before string
after string
wantScript string
}{
{
name: "exact match",
before: "testdata/move-after/04-before",
after: "testdata/move-after/04-after",
wantScript: "testdata/move-after/04-want",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
args := []string{"terravalet", "move-after"}
runMoveSuccess(t, args, tc.before, tc.after, tc.wantScript)
})
}
}
func TestRunMoveAfterFailure(t *testing.T) {
testCases := []struct {
name string
before string // special value: "non-existing"
after string // special value: "non-existing"
wantErr string
}{
{
name: "non existing before tfplan",
before: "non-existing",
after: "non-existing",
wantErr: "opening the terraform BEFORE plan file: open non-existing.tfplan: no such file or directory",
},
{
name: "non existing after tfplan",
before: "testdata/move-after/05-before",
after: "non-existing",
wantErr: "opening the terraform AFTER plan file: open non-existing.tfplan: no such file or directory",
},
{
name: "before tfplan must only destroy",
before: "testdata/move-after/05-before",
after: "testdata/move-after/05-after",
wantErr: "BEFORE plan contains resources to create: [aws_batch_job_definition.foo]",
},
{
name: "after tfplan must only create",
before: "testdata/move-after/06-before",
after: "testdata/move-after/06-after",
wantErr: "AFTER plan contains resources to destroy: [aws_batch_job_definition.foo]",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
args := []string{"terravalet", "move-after"}
runMoveFailure(t, args, tc.before, tc.after, tc.wantErr)
})
}
}
func TestRunMoveBeforeSuccess(t *testing.T) {
testCases := []struct {
name string
before string
after string // special prefix: dummy
wantScript string
}{
{
name: "happy path simple",
before: "testdata/move-before/01-before",
after: "dummy-01-after",
wantScript: "testdata/move-before/01-want",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
args := []string{"terravalet", "move-before"}
runMoveSuccess(t, args, tc.before, tc.after, tc.wantScript)
})
}
}
func TestRunMoveBeforeFailure(t *testing.T) {
testCases := []struct {
name string
before string // special value: "non-existing"
wantErr string
}{
{
name: "non existing BEFORE plan",
before: "non-existing",
wantErr: "opening the terraform BEFORE plan file: open non-existing.tfplan: no such file or directory",
},
{
name: "BEFORE plan must not contain resources to destroy",
before: "testdata/move-before/02-before",
wantErr: "BEFORE plan contains resources to destroy: [aws_batch_compute_environment.foo_batch]",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
args := []string{"terravalet", "move-before",
"--before=" + tc.before, "--after=testdata/move-before/dummy-after",
}
runMoveFailure(t, args, tc.before, "non-existing", tc.wantErr)
})
}
}
// If after has special prefix "dummy", it will not attempt to copy the
// corresponding tfplan files, to accomodate for move-before.
func runMoveSuccess(t *testing.T, args []string, before, after, wantScript string) {
wantUpPath := wantScript + "_up.sh"
wantUp, err := os.ReadFile(wantUpPath)
if err != nil {
t.Fatalf("reading want up file: %v", err)
}
wantDownPath := wantScript + "_down.sh"
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)
// Copy the required input files to the tmpdir.
if err := copyfile(before+".tfplan",
filepath.Join(tmpDir, path.Base(before)+".tfplan")); err != nil {
t.Fatal(err)
}
if !strings.HasPrefix(after, "dummy") {
if err := copyfile(after+".tfplan",
filepath.Join(tmpDir, path.Base(after)+".tfplan")); err != nil {
t.Fatal(err)
}
}
// Change directory to the tmpdir.
cwd, err := os.Getwd()
if err != nil {
t.Fatal("getwd:", err)
}
if err := os.Chdir(tmpDir); err != nil {
t.Fatal("chdir:", err)
}
defer func() {
if err := os.Chdir(cwd); err != nil {
panic(err)
}
}()
tmpScript := path.Base(wantScript)
tmpUpPath := tmpScript + "_up.sh"
tmpDownPath := tmpScript + "_down.sh"
args = append(args, "--before="+path.Base(before), "--after="+path.Base(after),
"--script="+tmpScript)
os.Args = args
if err := run(); err != nil {
t.Fatalf("run: args: %s\nhave: %q\nwant: no error", args, err)
}
t.Log("SUT ran successfully")
tmpUp, err := os.ReadFile(tmpUpPath)
if err != nil {
t.Fatalf("reading tmp up file: %s", err)
}
tmpDown, err := os.ReadFile(tmpDownPath)
if err != nil {
t.Fatalf("reading tmp down file: %s", 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)
}
}
// If before or after have the special value "non-existing", it will not attempt to copy the
// corresponding tfplan files, allowing to test a missing file.
func runMoveFailure(t *testing.T, args []string, before, after, wantErr string) {
tmpDir, err := os.MkdirTemp("", "terravalet")
if err != nil {
t.Fatalf("creating temporary dir: %v", err)
}
defer os.RemoveAll(tmpDir)
// Copy the required input files to the tmpdir.
if before != "non-existing" {
if err := copyfile(before+".tfplan",
filepath.Join(tmpDir, path.Base(before)+".tfplan")); err != nil {
t.Fatal(err)
}
}
if after != "non-existing" {
if err := copyfile(after+".tfplan",
filepath.Join(tmpDir, path.Base(after)+".tfplan")); err != nil {
t.Fatal(err)
}
}
// Change directory to the tmpdir.
cwd, err := os.Getwd()
if err != nil {
t.Fatal("getwd:", err)
}
if err := os.Chdir(tmpDir); err != nil {
t.Fatal("chdir:", err)
}
defer func() {
if err := os.Chdir(cwd); err != nil {
panic(err)
}
}()
args = append(args, "--before="+path.Base(before), "--after="+path.Base(after),
"--script=dummy-script")
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)
}
}
// copyfile copies file src to dst. It is not robust for production use (a lot of OS-dependent
// corner cases) but is good enough for tests.
func copyfile(src, dst string) error {
srcFile, err := os.Open(src)
if err != nil {
return fmt.Errorf("opening src file: %s", err)
}
defer srcFile.Close()
// Create (or truncate) the dst file
dstFile, err := os.Create(dst)
if err != nil {
return fmt.Errorf("creating dst file: %s", err)
}
defer dstFile.Close()
if _, err := io.Copy(dstFile, srcFile); err != nil {
return err
}
return nil
}