forked from gruntwork-io/terragrunt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremote_state_test.go
305 lines (272 loc) · 9.14 KB
/
remote_state_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
package remote_test
import (
"strings"
"testing"
"github.com/gruntwork-io/terragrunt/options"
"github.com/gruntwork-io/terragrunt/remote"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
/**
* Test for s3, also tests that the terragrunt-specific options are not passed on to terraform
*/
func TestToTerraformInitArgs(t *testing.T) {
t.Parallel()
remoteState := remote.RemoteState{
Backend: "s3",
Config: map[string]interface{}{
"encrypt": true,
"bucket": "my-bucket",
"key": "terraform.tfstate",
"region": "us-east-1",
"s3_bucket_tags": map[string]interface{}{
"team": "team name",
"name": "Terraform state storage",
"service": "Terraform"},
"dynamodb_table_tags": map[string]interface{}{
"team": "team name",
"name": "Terraform lock table",
"service": "Terraform"},
"accesslogging_bucket_tags": map[string]interface{}{
"team": "team name",
"name": "Terraform access log storage",
"service": "Terraform"},
"skip_bucket_versioning": true,
"shared_credentials_file": "my-file",
"force_path_style": true,
},
}
args := remoteState.ToTerraformInitArgs()
// must not contain s3_bucket_tags or dynamodb_table_tags or accesslogging_bucket_tags or skip_bucket_versioning
assertTerraformInitArgsEqual(t, args, "-backend-config=encrypt=true -backend-config=bucket=my-bucket -backend-config=key=terraform.tfstate -backend-config=region=us-east-1 -backend-config=force_path_style=true -backend-config=shared_credentials_file=my-file")
}
func TestToTerraformInitArgsForGCS(t *testing.T) {
t.Parallel()
remoteState := remote.RemoteState{
Backend: "gcs",
Config: map[string]interface{}{
"project": "my-project-123456",
"location": "US",
"bucket": "my-bucket",
"prefix": "terraform.tfstate",
"gcs_bucket_labels": map[string]interface{}{
"team": "team name",
"name": "Terraform state storage",
"service": "Terraform"},
"skip_bucket_versioning": true,
"credentials": "my-file",
"access_token": "xxxxxxxx",
},
}
args := remoteState.ToTerraformInitArgs()
// must not contain project, location gcs_bucket_labels or skip_bucket_versioning
assertTerraformInitArgsEqual(t, args, "-backend-config=bucket=my-bucket -backend-config=prefix=terraform.tfstate -backend-config=credentials=my-file -backend-config=access_token=xxxxxxxx")
}
func TestToTerraformInitArgsUnknownBackend(t *testing.T) {
t.Parallel()
remoteState := remote.RemoteState{
Backend: "s4",
Config: map[string]interface{}{
"encrypt": true,
"bucket": "my-bucket",
"key": "terraform.tfstate",
"region": "us-east-1"},
}
args := remoteState.ToTerraformInitArgs()
// no Backend initializer available, but command line args should still be passed on
assertTerraformInitArgsEqual(t, args, "-backend-config=encrypt=true -backend-config=bucket=my-bucket -backend-config=key=terraform.tfstate -backend-config=region=us-east-1")
}
func TestToTerraformInitArgsInitDisabled(t *testing.T) {
t.Parallel()
remoteState := remote.RemoteState{
Backend: "s3",
DisableInit: true,
Config: map[string]interface{}{
"encrypt": true,
"bucket": "my-bucket",
"key": "terraform.tfstate",
"region": "us-east-1"},
}
args := remoteState.ToTerraformInitArgs()
assertTerraformInitArgsEqual(t, args, "-backend=false")
}
func TestToTerraformInitArgsNoBackendConfigs(t *testing.T) {
t.Parallel()
remoteStates := []remote.RemoteState{
{Backend: "s3"},
{Backend: "gcs"},
}
for _, state := range remoteStates {
args := state.ToTerraformInitArgs()
assert.Empty(t, args)
}
}
func TestDiffersFrom(t *testing.T) {
t.Parallel()
terragruntOptions, err := options.NewTerragruntOptionsForTest("remote_state_test")
require.NoError(t, err, "Unexpected error creating NewTerragruntOptionsForTest: %v", err)
testCases := []struct {
name string
existingBackend remote.TerraformBackend
stateFromConfig remote.RemoteState
shouldOverride bool
}{
{"both empty", remote.TerraformBackend{}, remote.RemoteState{}, false},
{"same backend type value", remote.TerraformBackend{Type: "s3"}, remote.RemoteState{Backend: "s3"}, false},
{"different backend type values", remote.TerraformBackend{Type: "s3"}, remote.RemoteState{Backend: "atlas"}, true},
{
"identical S3 configs",
remote.TerraformBackend{
Type: "s3",
Config: map[string]interface{}{"bucket": "foo", "key": "bar", "region": "us-east-1"},
},
remote.RemoteState{
Backend: "s3",
Config: map[string]interface{}{"bucket": "foo", "key": "bar", "region": "us-east-1"},
},
false,
}, {
"identical GCS configs",
remote.TerraformBackend{
Type: "gcs",
Config: map[string]interface{}{"project": "foo-123456", "location": "europe-west3", "bucket": "foo", "prefix": "bar"},
},
remote.RemoteState{
Backend: "gcs",
Config: map[string]interface{}{"project": "foo-123456", "location": "europe-west3", "bucket": "foo", "prefix": "bar"},
},
false,
}, {
"different s3 bucket values",
remote.TerraformBackend{
Type: "s3",
Config: map[string]interface{}{"bucket": "foo", "key": "bar", "region": "us-east-1"},
},
remote.RemoteState{
Backend: "s3",
Config: map[string]interface{}{"bucket": "different", "key": "bar", "region": "us-east-1"},
},
true,
}, {
"different gcs bucket values",
remote.TerraformBackend{
Type: "gcs",
Config: map[string]interface{}{"project": "foo-123456", "location": "europe-west3", "bucket": "foo", "prefix": "bar"},
},
remote.RemoteState{
Backend: "gcs",
Config: map[string]interface{}{"project": "foo-123456", "location": "europe-west3", "bucket": "different", "prefix": "bar"},
},
true,
}, {
"different s3 key values",
remote.TerraformBackend{
Type: "s3",
Config: map[string]interface{}{"bucket": "foo", "key": "bar", "region": "us-east-1"},
},
remote.RemoteState{
Backend: "s3",
Config: map[string]interface{}{"bucket": "foo", "key": "different", "region": "us-east-1"},
},
true,
}, {
"different gcs prefix values",
remote.TerraformBackend{
Type: "gcs",
Config: map[string]interface{}{"project": "foo-123456", "location": "europe-west3", "bucket": "foo", "prefix": "bar"},
},
remote.RemoteState{
Backend: "gcs",
Config: map[string]interface{}{"project": "foo-123456", "location": "europe-west3", "bucket": "foo", "prefix": "different"},
},
true,
}, {
"different s3 region values",
remote.TerraformBackend{
Type: "s3",
Config: map[string]interface{}{"bucket": "foo", "key": "bar", "region": "us-east-1"},
},
remote.RemoteState{
Backend: "s3",
Config: map[string]interface{}{"bucket": "foo", "key": "bar", "region": "different"},
},
true,
}, {
"different gcs location values",
remote.TerraformBackend{
Type: "gcs",
Config: map[string]interface{}{"project": "foo-123456", "location": "europe-west3", "bucket": "foo", "prefix": "bar"},
},
remote.RemoteState{
Backend: "gcs",
Config: map[string]interface{}{"project": "foo-123456", "location": "different", "bucket": "foo", "prefix": "bar"},
},
true,
},
{
"different boolean values and boolean conversion",
remote.TerraformBackend{
Type: "s3",
Config: map[string]interface{}{"something": "true"},
},
remote.RemoteState{
Backend: "s3",
Config: map[string]interface{}{"something": false},
},
true,
},
{
"different gcs boolean values and boolean conversion",
remote.TerraformBackend{
Type: "gcs",
Config: map[string]interface{}{"something": "true"},
},
remote.RemoteState{
Backend: "gcs",
Config: map[string]interface{}{"something": false},
},
true,
},
{
"null values ignored",
remote.TerraformBackend{
Type: "s3",
Config: map[string]interface{}{"something": "foo", "set-to-nil-should-be-ignored": nil},
},
remote.RemoteState{
Backend: "s3",
Config: map[string]interface{}{"something": "foo"},
},
false,
},
{
"gcs null values ignored",
remote.TerraformBackend{
Type: "gcs",
Config: map[string]interface{}{"something": "foo", "set-to-nil-should-be-ignored": nil},
},
remote.RemoteState{
Backend: "gcs",
Config: map[string]interface{}{"something": "foo"},
},
false,
},
}
for _, testCase := range testCases {
// Save the testCase in local scope so all the t.Run calls don't end up with the last item in the list
testCase := testCase
t.Run(testCase.name, func(t *testing.T) {
t.Parallel()
shouldOverride := testCase.stateFromConfig.DiffersFrom(&testCase.existingBackend, terragruntOptions)
assert.Equal(t, testCase.shouldOverride, shouldOverride, "Expect differsFrom to return %t but got %t for existingRemoteState %v and remoteStateFromTerragruntConfig %v", testCase.shouldOverride, shouldOverride, testCase.existingBackend, testCase.stateFromConfig)
})
}
}
func assertTerraformInitArgsEqual(t *testing.T, actualArgs []string, expectedArgs string) {
t.Helper()
expected := strings.Split(expectedArgs, " ")
assert.Len(t, actualArgs, len(expected))
for _, expectedArg := range expected {
assert.Contains(t, actualArgs, expectedArg)
}
}