forked from gruntwork-io/terratest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterraform_basic_example_regression_test.go
181 lines (144 loc) · 5.34 KB
/
terraform_basic_example_regression_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
package test
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/gruntwork-io/terratest/modules/random"
"github.com/gruntwork-io/terratest/modules/terraform"
test_structure "github.com/gruntwork-io/terratest/modules/test-structure"
)
// The tests in this folder are not example usage of Terratest. Instead, this is a regression test to ensure the
// formatting rules work with an actual Terraform call when using more complex structures.
func TestTerraformFormatNestedOneLevelList(t *testing.T) {
t.Parallel()
testList := [][]string{
[]string{random.UniqueId()},
}
options := GetTerraformOptionsForFormatTests(t)
options.Vars["example_list"] = testList
defer terraform.Destroy(t, options)
terraform.InitAndApply(t, options)
outputMap := OutputJsonMap(t, options, "example_list")
actualExampleList := outputMap["value"]
AssertEqualJson(t, actualExampleList, testList)
}
func TestTerraformFormatNestedTwoLevelList(t *testing.T) {
t.Parallel()
testList := [][][]string{
[][]string{[]string{random.UniqueId()}},
}
options := GetTerraformOptionsForFormatTests(t)
options.Vars["example_list"] = testList
defer terraform.Destroy(t, options)
terraform.InitAndApply(t, options)
outputMap := OutputJsonMap(t, options, "example_list")
actualExampleList := outputMap["value"]
AssertEqualJson(t, actualExampleList, testList)
}
func TestTerraformFormatNestedMultipleItems(t *testing.T) {
t.Parallel()
testList := [][]string{
[]string{random.UniqueId(), random.UniqueId()},
[]string{random.UniqueId(), random.UniqueId(), random.UniqueId()},
}
options := GetTerraformOptionsForFormatTests(t)
options.Vars["example_list"] = testList
defer terraform.Destroy(t, options)
terraform.InitAndApply(t, options)
outputMap := OutputJsonMap(t, options, "example_list")
actualExampleList := outputMap["value"]
AssertEqualJson(t, actualExampleList, testList)
}
func TestTerraformFormatNestedOneLevelMap(t *testing.T) {
t.Parallel()
testMap := map[string]map[string]string{
"test": map[string]string{
"foo": random.UniqueId(),
},
}
options := GetTerraformOptionsForFormatTests(t)
options.Vars["example_map"] = testMap
defer terraform.Destroy(t, options)
terraform.InitAndApply(t, options)
outputMap := OutputJsonMap(t, options, "example_map")
actualExampleMap := outputMap["value"]
AssertEqualJson(t, actualExampleMap, testMap)
}
func TestTerraformFormatNestedTwoLevelMap(t *testing.T) {
t.Parallel()
testMap := map[string]map[string]map[string]string{
"test": map[string]map[string]string{
"foo": map[string]string{
"bar": random.UniqueId(),
},
},
}
options := GetTerraformOptionsForFormatTests(t)
options.Vars["example_map"] = testMap
defer terraform.Destroy(t, options)
terraform.InitAndApply(t, options)
outputMap := OutputJsonMap(t, options, "example_map")
actualExampleMap := outputMap["value"]
AssertEqualJson(t, actualExampleMap, testMap)
}
func TestTerraformFormatNestedMultipleItemsMap(t *testing.T) {
t.Parallel()
testMap := map[string]map[string]string{
"test": map[string]string{
"foo": random.UniqueId(),
"bar": random.UniqueId(),
},
"other": map[string]string{
"baz": random.UniqueId(),
"boo": random.UniqueId(),
},
}
options := GetTerraformOptionsForFormatTests(t)
options.Vars["example_map"] = testMap
defer terraform.Destroy(t, options)
terraform.InitAndApply(t, options)
outputMap := OutputJsonMap(t, options, "example_map")
actualExampleMap := outputMap["value"]
AssertEqualJson(t, actualExampleMap, testMap)
}
func TestTerraformFormatNestedListMap(t *testing.T) {
t.Parallel()
testMap := map[string][]string{
"test": []string{random.UniqueId(), random.UniqueId()},
}
options := GetTerraformOptionsForFormatTests(t)
options.Vars["example_map"] = testMap
defer terraform.Destroy(t, options)
terraform.InitAndApply(t, options)
outputMap := OutputJsonMap(t, options, "example_map")
actualExampleMap := outputMap["value"]
AssertEqualJson(t, actualExampleMap, testMap)
}
func GetTerraformOptionsForFormatTests(t *testing.T) *terraform.Options {
exampleFolder := test_structure.CopyTerraformFolderToTemp(t, "../", "examples/terraform-basic-example")
terraformOptions := &terraform.Options{
TerraformDir: exampleFolder,
Vars: map[string]interface{}{},
NoColor: true,
}
return terraformOptions
}
// To avoid conversion errors with nested data structures, we work directly off the json output when comparing the data.
// This is because both OutputList and OutputMap assume the data is single level deep.
func OutputJsonMap(t *testing.T, options *terraform.Options, key string) map[string]interface{} {
out, err := terraform.RunTerraformCommandE(t, options, "output", "-no-color", "-json", key)
require.NoError(t, err)
outputMap := map[string]interface{}{}
require.NoError(t, json.Unmarshal([]byte(out), &outputMap))
return outputMap
}
// The value of the output nested in the outputMap returned by OutputJsonMap uses the interface{} type for nested
// structures. This can't be compared to actual types like [][]string{}, so we instead compare the json versions.
func AssertEqualJson(t *testing.T, actual interface{}, expected interface{}) {
actualJson, err := json.Marshal(actual)
require.NoError(t, err)
expectedJson, err := json.Marshal(expected)
require.NoError(t, err)
assert.Equal(t, actualJson, expectedJson)
}