-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain_test.go
189 lines (168 loc) · 4.11 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
package main
import (
"bytes"
"errors"
"io/ioutil"
"os"
"strings"
"testing"
"github.com/pkg/diff"
)
func TestSuccess(t *testing.T) {
testCases := []struct {
planPath string
wantUpPath string
wantDownPath string
}{
{
"testdata/plan-synthetic-01.txt",
"testdata/001_synthetic.up.sh",
"testdata/001_synthetic.down.sh",
},
}
for _, tc := range testCases {
t.Run(tc.planPath, func(t *testing.T) {
wantUp, err := ioutil.ReadFile(tc.wantUpPath)
if err != nil {
t.Fatalf("reading want up file: %v", err)
}
wantDown, err := ioutil.ReadFile(tc.wantDownPath)
if err != nil {
t.Fatalf("reading want down file: %v", err)
}
tmpDir, err := ioutil.TempDir("", "terravalet")
if err != nil {
t.Fatalf("creating temporary dir: %v", err)
}
defer os.RemoveAll(tmpDir)
tmpUpPath := tmpDir + "/up"
tmpDownPath := tmpDir + "/down"
args := []string{"-plan", tc.planPath, "-up", tmpUpPath, "-down", tmpDownPath}
if err := run(args); err != nil {
t.Fatalf("got error: %v; want: no error", err)
}
tmpUp, err := ioutil.ReadFile(tmpUpPath)
if err != nil {
t.Fatalf("reading tmp up file: %v", err)
}
tmpDown, err := ioutil.ReadFile(tmpDownPath)
if err != nil {
t.Fatalf("reading tmp down file: %v", err)
}
if !bytes.Equal(tmpUp, wantUp) {
var outDiff bytes.Buffer
diff.Text("got", tc.wantUpPath, tmpUp, wantUp, &outDiff)
t.Errorf("up script: got the following differences:\n%v", outDiff.String())
}
if !bytes.Equal(tmpDown, wantDown) {
var outDiff bytes.Buffer
diff.Text("got", tc.wantDownPath, tmpDown, wantDown, &outDiff)
t.Errorf("down script: got the following differences:\n%v", outDiff.String())
}
})
}
}
func TestFailure(t *testing.T) {
testCases := []struct {
args []string
wantError string
}{
{
[]string{},
"missing value for -plan",
},
{
[]string{"-plan=nonexisting", "-up=up", "-down=down"},
"opening the terraform plan file: open nonexisting: no such file or directory",
},
}
for _, tc := range testCases {
t.Run(strings.Join(tc.args, "_"), func(t *testing.T) {
err := run(tc.args)
if err == nil {
t.Fatalf("\ngot: no error\nwant: %v", tc.wantError)
}
if err.Error() != tc.wantError {
t.Fatalf("\ngot: %v\nwant: %v", err, tc.wantError)
}
})
}
}
func TestParseSuccess(t *testing.T) {
testCases := []struct {
description string
line string
wantCreate []string
wantDestroy []string
}{
{
"destroyed is recorded",
" # aws_instance.bar will be destroyed",
[]string{},
[]string{"aws_instance.bar"},
},
{
"created is recorded",
" # aws_instance.bar will be created",
[]string{"aws_instance.bar"},
[]string{},
},
{
"read is skipped",
" # data.foo.bar will be read during apply",
[]string{},
[]string{},
},
}
for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
rd := strings.NewReader(tc.line)
gotCreate, gotDestroy, err := parse(rd)
if err != nil {
t.Fatalf("\ngot: %v\nwant: no error", err)
}
if !stringEqual(gotCreate, tc.wantCreate) {
t.Errorf("\ngotCreate: %v\nwantCreate: %v", gotCreate, tc.wantCreate)
}
if !stringEqual(gotDestroy, tc.wantDestroy) {
t.Errorf("\ngotDestroy: %v\nwantDestroy: %v", gotDestroy, tc.wantDestroy)
}
})
}
}
func TestParseFailure(t *testing.T) {
testCases := []struct {
description string
line string
wantError error
}{
{
"vaporized is not an expected action",
" # aws_instance.bar will be vaporized",
errors.New(`line " # aws_instance.bar will be vaporized", unexpected action "vaporized"`),
},
}
for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
rd := strings.NewReader(tc.line)
_, _, err := parse(rd)
if err == nil {
t.Fatalf("\ngot: no error\nwant: %v", tc.wantError)
}
if err.Error() != tc.wantError.Error() {
t.Fatalf("\ngot: %v\nwant: %v", err, tc.wantError)
}
})
}
}
func stringEqual(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i, v := range a {
if v != b[i] {
return false
}
}
return true
}