forked from jmccann/drone-terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin_test.go
301 lines (274 loc) · 7.36 KB
/
plugin_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
package main
import (
"os"
"os/exec"
"testing"
. "github.com/franela/goblin"
)
func TestPlugin(t *testing.T) {
g := Goblin(t)
g.Describe("CopyTfEnv", func() {
g.It("Should create copies of TF_VAR_ to lowercase", func() {
// Set some initial TF_VAR_ that are uppercase
os.Setenv("TF_VAR_SOMETHING", "some value")
os.Setenv("TF_VAR_SOMETHING_ELSE", "some other value")
os.Setenv("TF_VAR_BASE64", "dGVzdA==")
CopyTfEnv()
// Make sure new env vars exist with proper values
g.Assert(os.Getenv("TF_VAR_something")).Equal("some value")
g.Assert(os.Getenv("TF_VAR_something_else")).Equal("some other value")
g.Assert(os.Getenv("TF_VAR_base64")).Equal("dGVzdA==")
})
})
g.Describe("credsSet", func() {
tests := []struct {
name string
args map[string]string
want bool
}{
{
"Should return true when all credentials were set",
map[string]string{"AWS_ACCESS_KEY_ID": "x", "AWS_SECRET_ACCESS_KEY": "x", "AWS_SESSION_TOKEN": "x"},
true,
},
{
"Should return false when access key id is missing",
map[string]string{"AWS_SECRET_ACCESS_KEY": "x", "AWS_SESSION_TOKEN": "x"},
false,
},
{
"Should return false when secret access key is missing",
map[string]string{"AWS_ACCESS_KEY_ID": "x", "AWS_SESSION_TOKEN": "x"},
false,
},
{
"Should return false when session token is missing",
map[string]string{"AWS_ACCESS_KEY_ID": "x", "AWS_SECRET_ACCESS_KEY": "x"},
false,
},
}
for _, tt := range tests {
g.It(tt.name, func() {
for k, v := range tt.args {
os.Setenv(k, v)
}
g.Assert(credsSet()).Equal(tt.want)
})
}
})
g.Describe("tfApply", func() {
g.It("Should return correct apply commands given the arguments", func() {
type args struct {
config Config
}
tests := []struct {
name string
args args
want *exec.Cmd
}{
{
"default",
args{config: Config{}},
exec.Command("terraform", "apply", "plan.tfout"),
},
{
"with parallelism",
args{config: Config{Parallelism: 5}},
exec.Command("terraform", "apply", "-parallelism=5", "plan.tfout"),
},
{
"with targets",
args{config: Config{Targets: []string{"target1", "target2"}}},
exec.Command("terraform", "apply", "--target", "target1", "--target", "target2", "plan.tfout"),
},
}
for _, tt := range tests {
g.Assert(tfApply(tt.args.config).Args).Equal(tt.want.Args)
}
})
})
g.Describe("tfDestroy", func() {
g.It("Should return correct destroy commands given the arguments", func() {
type args struct {
config Config
}
tests := []struct {
name string
args args
want *exec.Cmd
}{
{
"default",
args{config: Config{}},
exec.Command("terraform", "destroy", "-auto-approve"),
},
{
"with parallelism",
args{config: Config{Parallelism: 5}},
exec.Command("terraform", "destroy", "-parallelism=5", "-auto-approve"),
},
{
"with targets",
args{config: Config{Targets: []string{"target1", "target2"}}},
exec.Command("terraform", "destroy", "-target=target1", "-target=target2", "-auto-approve"),
},
{
"with vars",
args{config: Config{Vars: map[string]string{"username": "someuser", "password": "1pass"}}},
exec.Command("terraform", "destroy", "-var", "username=someuser", "-var", "password=1pass", "-auto-approve"),
},
{
"with var-files",
args{config: Config{VarFiles: []string{"common.tfvars", "prod.tfvars"}}},
exec.Command("terraform", "destroy", "-var-file=common.tfvars", "-var-file=prod.tfvars", "-auto-approve"),
},
}
for _, tt := range tests {
g.Assert(tfDestroy(tt.args.config)).Equal(tt.want)
}
})
})
g.Describe("tfValidate", func() {
g.It("Should return correct validate command", func() {
tests := []struct {
name string
want *exec.Cmd
}{
{
"default",
exec.Command("terraform", "validate"),
},
}
for _, tt := range tests {
g.Assert(tfValidate()).Equal(tt.want)
}
})
})
g.Describe("tfPlan", func() {
g.It("Should return correct plan commands given the arguments", func() {
type args struct {
config Config
}
tests := []struct {
name string
args args
destroy bool
want *exec.Cmd
}{
{
"default",
args{config: Config{}},
false,
exec.Command("terraform", "plan", "-out=plan.tfout"),
},
{
"destroy",
args{config: Config{}},
true,
exec.Command("terraform", "plan", "-destroy"),
},
{
"with vars",
args{config: Config{Vars: map[string]string{"username": "someuser", "password": "1pass"}}},
false,
exec.Command("terraform", "plan", "-out=plan.tfout", "-var", "username=someuser", "-var", "password=1pass"),
},
{
"with var-files",
args{config: Config{VarFiles: []string{"common.tfvars", "prod.tfvars"}}},
false,
exec.Command("terraform", "plan", "-out=plan.tfout", "-var-file=common.tfvars", "-var-file=prod.tfvars"),
},
}
for _, tt := range tests {
g.Assert(tfPlan(tt.args.config, tt.destroy)).Equal(tt.want)
}
})
})
g.Describe("tfFmt", func() {
g.It("Should return correct fmt commands given the arguments", func() {
type args struct {
config Config
}
affirmative := true
negative := false
tests := []struct {
name string
args args
want *exec.Cmd
}{
{
"default",
args{config: Config{}},
exec.Command("terraform", "fmt"),
},
{
"with list",
args{config: Config{FmtOptions: FmtOptions{List: &affirmative}}},
exec.Command("terraform", "fmt", "-list=true"),
},
{
"with write",
args{config: Config{FmtOptions: FmtOptions{Write: &affirmative}}},
exec.Command("terraform", "fmt", "-write=true"),
},
{
"with diff",
args{config: Config{FmtOptions: FmtOptions{Diff: &affirmative}}},
exec.Command("terraform", "fmt", "-diff=true"),
},
{
"with check",
args{config: Config{FmtOptions: FmtOptions{Check: &affirmative}}},
exec.Command("terraform", "fmt", "-check=true"),
},
{
"with combination",
args{config: Config{FmtOptions: FmtOptions{
List: &negative,
Write: &negative,
Diff: &affirmative,
Check: &affirmative,
}}},
exec.Command("terraform", "fmt", "-list=false", "-write=false", "-diff=true", "-check=true"),
},
}
for _, tt := range tests {
g.Assert(tfFmt(tt.args.config)).Equal(tt.want)
}
})
})
g.Describe("tfDataDir", func() {
g.It("Should override the terraform data dir environment variable when provided", func() {
type args struct {
config Config
}
tests := []struct {
name string
args args
want *exec.Cmd
}{
{
"with TerraformDataDir",
args{config: Config{TerraformDataDir: ".overriden_terraform_dir"}},
exec.Command("terraform", "apply", ".overriden_terraform_dir.plan.tfout"),
},
{
"with TerraformDataDir value as .terraform",
args{config: Config{TerraformDataDir: ".terraform"}},
exec.Command("terraform", "apply", "plan.tfout"),
},
{
"without TerraformDataDir",
args{config: Config{}},
exec.Command("terraform", "apply", "plan.tfout"),
},
}
for _, tt := range tests {
os.Setenv("TF_DATA_DIR", tt.args.config.TerraformDataDir)
applied := tfApply(tt.args.config)
g.Assert(applied).Equal(tt.want)
}
})
})
}