github.com/terraform-modules-krish/terratest@v0.29.0/modules/terraform/output_test.go (about) 1 package terraform 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/terraform-modules-krish/terratest/modules/files" 8 "github.com/stretchr/testify/require" 9 ) 10 11 func TestOutputList(t *testing.T) { 12 t.Parallel() 13 14 testFolder, err := files.CopyTerraformFolderToTemp("../../test/fixtures/terraform-output-list", t.Name()) 15 require.NoError(t, err) 16 17 options := &Options{ 18 TerraformDir: testFolder, 19 } 20 21 InitAndApply(t, options) 22 out := OutputList(t, options, "giant_steps") 23 24 expectedLen := 4 25 expectedItem := "John Coltrane" 26 expectedArray := []string{"John Coltrane", "Tommy Flanagan", "Paul Chambers", "Art Taylor"} 27 28 require.Len(t, out, expectedLen, "Output should contain %d items", expectedLen) 29 require.Contains(t, out, expectedItem, "Output should contain %q item", expectedItem) 30 require.Equal(t, out[0], expectedItem, "First item should be %q, got %q", expectedItem, out[0]) 31 require.Equal(t, out, expectedArray, "Array %q should match %q", expectedArray, out) 32 } 33 34 func TestOutputNotListError(t *testing.T) { 35 t.Parallel() 36 37 testFolder, err := files.CopyTerraformFolderToTemp("../../test/fixtures/terraform-output-list", t.Name()) 38 if err != nil { 39 t.Fatal(err) 40 } 41 42 options := &Options{ 43 TerraformDir: testFolder, 44 } 45 46 InitAndApply(t, options) 47 _, err = OutputListE(t, options, "not_a_list") 48 49 require.Error(t, err) 50 } 51 52 func TestOutputMap(t *testing.T) { 53 t.Parallel() 54 55 testFolder, err := files.CopyTerraformFolderToTemp("../../test/fixtures/terraform-output-map", t.Name()) 56 require.NoError(t, err) 57 58 options := &Options{ 59 TerraformDir: testFolder, 60 } 61 62 InitAndApply(t, options) 63 out := OutputMap(t, options, "mogwai") 64 65 t.Log(out) 66 67 expectedLen := 4 68 expectedMap := map[string]string{ 69 "guitar_1": "Stuart Braithwaite", 70 "guitar_2": "Barry Burns", 71 "bass": "Dominic Aitchison", 72 "drums": "Martin Bulloch", 73 } 74 75 require.Len(t, out, expectedLen, "Output should contain %d items", expectedLen) 76 require.Equal(t, expectedMap, out, "Map %q should match %q", expectedMap, out) 77 } 78 79 func TestOutputNotMapError(t *testing.T) { 80 t.Parallel() 81 82 testFolder, err := files.CopyTerraformFolderToTemp("../../test/fixtures/terraform-output-map", t.Name()) 83 require.NoError(t, err) 84 85 options := &Options{ 86 TerraformDir: testFolder, 87 } 88 89 InitAndApply(t, options) 90 _, err = OutputMapE(t, options, "not_a_map") 91 92 require.Error(t, err) 93 } 94 95 func TestOutputMapOfObjects(t *testing.T) { 96 t.Parallel() 97 98 testFolder, err := files.CopyTerraformFolderToTemp("../../test/fixtures/terraform-output-mapofobjects", t.Name()) 99 require.NoError(t, err) 100 101 options := &Options{ 102 TerraformDir: testFolder, 103 } 104 105 InitAndApply(t, options) 106 out := OutputMapOfObjects(t, options, "map_of_objects") 107 108 nestedMap1 := map[string]interface{}{ 109 "four": 4, 110 "five": "five", 111 } 112 113 nestedList1 := []map[string]interface{}{ 114 map[string]interface{}{ 115 "six": 6, 116 "seven": "seven", 117 }, 118 } 119 120 expectedMap1 := map[string]interface{}{ 121 "somebool": true, 122 "somefloat": 1.1, 123 "one": 1, 124 "two": "two", 125 "three": "three", 126 "nest": nestedMap1, 127 "nest_list": nestedList1, 128 } 129 130 require.Equal(t, expectedMap1, out) 131 } 132 133 func TestOutputNotMapOfObjectsError(t *testing.T) { 134 t.Parallel() 135 136 testFolder, err := files.CopyTerraformFolderToTemp("../../test/fixtures/terraform-output-mapofobjects", t.Name()) 137 require.NoError(t, err) 138 139 options := &Options{ 140 TerraformDir: testFolder, 141 } 142 143 InitAndApply(t, options) 144 _, err = OutputMapOfObjectsE(t, options, "not_map_of_objects") 145 146 require.Error(t, err) 147 } 148 149 func TestOutputListOfObjects(t *testing.T) { 150 t.Parallel() 151 152 testFolder, err := files.CopyTerraformFolderToTemp("../../test/fixtures/terraform-output-listofobjects", t.Name()) 153 require.NoError(t, err) 154 155 options := &Options{ 156 TerraformDir: testFolder, 157 } 158 159 InitAndApply(t, options) 160 out := OutputListOfObjects(t, options, "list_of_maps") 161 162 expectedLen := 2 163 nestedMap1 := map[string]interface{}{ 164 "four": 4, 165 "five": "five", 166 } 167 nestedList1 := []map[string]interface{}{ 168 map[string]interface{}{ 169 "four": 4, 170 "five": "five", 171 }, 172 } 173 expectedMap1 := map[string]interface{}{ 174 "one": 1, 175 "two": "two", 176 "three": "three", 177 "more": nestedMap1, 178 } 179 180 expectedMap2 := map[string]interface{}{ 181 "one": "one", 182 "two": 2, 183 "three": 3, 184 "more": nestedList1, 185 } 186 187 require.Len(t, out, expectedLen, "Output should contain %d items", expectedLen) 188 require.Equal(t, out[0], expectedMap1, "First map should be %q, got %q", expectedMap1, out[0]) 189 require.Equal(t, out[1], expectedMap2, "First map should be %q, got %q", expectedMap2, out[1]) 190 } 191 192 func TestOutputNotListOfObjectsError(t *testing.T) { 193 t.Parallel() 194 195 testFolder, err := files.CopyTerraformFolderToTemp("../../test/fixtures/terraform-output-listofobjects", t.Name()) 196 require.NoError(t, err) 197 198 options := &Options{ 199 TerraformDir: testFolder, 200 } 201 202 InitAndApply(t, options) 203 _, err = OutputListOfObjectsE(t, options, "not_list_of_maps") 204 205 require.Error(t, err) 206 } 207 208 func TestOutputsForKeys(t *testing.T) { 209 t.Parallel() 210 211 testFolder, err := files.CopyTerraformFolderToTemp("../../test/fixtures/terraform-output-all", t.Name()) 212 require.NoError(t, err) 213 214 options := &Options{ 215 TerraformDir: testFolder, 216 } 217 218 keys := []string{"our_star", "stars", "magnitudes"} 219 220 InitAndApply(t, options) 221 out := OutputForKeys(t, options, keys) 222 223 expectedLen := 3 224 require.Len(t, out, expectedLen, "Output should contain %d items", expectedLen) 225 226 //String value 227 expectedString := "Sun" 228 str, ok := out["our_star"].(string) 229 require.True(t, ok, fmt.Sprintf("Wrong data type for 'our_star', expected string, got %T", out["our_star"])) 230 require.Equal(t, expectedString, str, "String %q should match %q", expectedString, str) 231 232 //List value 233 expectedListLen := 3 234 outputInterfaceList, ok := out["stars"].([]interface{}) 235 require.True(t, ok, fmt.Sprintf("Wrong data type for 'stars', expected [], got %T", out["stars"])) 236 expectedListItem := "Sirius" 237 require.Len(t, outputInterfaceList, expectedListLen, "Output list should contain %d items", expectedListLen) 238 require.Equal(t, expectedListItem, outputInterfaceList[0].(string), "List Item %q should match %q", 239 expectedListItem, outputInterfaceList[0].(string)) 240 241 //Map value 242 outputInterfaceMap, ok := out["magnitudes"].(map[string]interface{}) 243 require.True(t, ok, fmt.Sprintf("Wrong data type for 'magnitudes', expected map[string], got %T", out["magnitudes"])) 244 expectedMapLen := 3 245 expectedMapItem := -1.46 246 require.Len(t, outputInterfaceMap, expectedMapLen, "Output map should contain %d items", expectedMapLen) 247 require.Equal(t, expectedMapItem, outputInterfaceMap["Sirius"].(float64), "Map Item %q should match %q", 248 expectedMapItem, outputInterfaceMap["Sirius"].(float64)) 249 250 //Key not in the parameter list 251 outputNotPresentMap, ok := out["constellations"].(map[string]interface{}) 252 require.False(t, ok) 253 require.Nil(t, outputNotPresentMap) 254 } 255 256 func TestOutputStruct(t *testing.T) { 257 t.Parallel() 258 259 type TestStruct struct { 260 Somebool bool 261 Somefloat float64 262 Someint int 263 Somestring string 264 Somemap map[string]interface{} 265 Listmaps []map[string]interface{} 266 Liststrings []string 267 } 268 269 testFolder, err := files.CopyTerraformFolderToTemp("../../test/fixtures/terraform-output-struct", t.Name()) 270 if err != nil { 271 t.Fatal(err) 272 } 273 274 options := &Options{ 275 TerraformDir: testFolder, 276 } 277 278 InitAndApply(t, options) 279 280 expectedObject := TestStruct{ 281 Somebool: true, 282 Somefloat: 0.1, 283 Someint: 1, 284 Somestring: "two", 285 Somemap: map[string]interface{}{"three": 3.0, "four": "four"}, 286 Listmaps: []map[string]interface{}{{"five": 5.0, "six": "six"}}, 287 Liststrings: []string{"seven", "eight", "nine"}, 288 } 289 actualObject := TestStruct{} 290 OutputStruct(t, options, "object", &actualObject) 291 292 expectedList := []TestStruct{ 293 { 294 Somebool: true, 295 Somefloat: 0.1, 296 Someint: 1, 297 Somestring: "two", 298 }, 299 { 300 Somebool: false, 301 Somefloat: 0.3, 302 Someint: 4, 303 Somestring: "five", 304 }, 305 } 306 actualList := []TestStruct{} 307 OutputStruct(t, options, "list_of_objects", &actualList) 308 309 require.Equal(t, expectedObject, actualObject, "Object should be %q, got %q", expectedObject, actualObject) 310 require.Equal(t, expectedList, actualList, "List should be %q, got %q", expectedList, actualList) 311 } 312 313 func TestOutputsAll(t *testing.T) { 314 t.Parallel() 315 316 testFolder, err := files.CopyTerraformFolderToTemp("../../test/fixtures/terraform-output-all", t.Name()) 317 if err != nil { 318 t.Fatal(err) 319 } 320 321 options := &Options{ 322 TerraformDir: testFolder, 323 } 324 325 InitAndApply(t, options) 326 out := OutputAll(t, options) 327 328 expectedLen := 4 329 require.Len(t, out, expectedLen, "Output should contain %d items", expectedLen) 330 331 //String Value 332 expectedString := "Sun" 333 str, ok := out["our_star"].(string) 334 require.True(t, ok, fmt.Sprintf("Wrong data type for 'our_star', expected string, got %T", out["our_star"])) 335 require.Equal(t, expectedString, str, "String %q should match %q", expectedString, str) 336 337 //List Value 338 expectedListLen := 3 339 outputInterfaceList, ok := out["stars"].([]interface{}) 340 require.True(t, ok, fmt.Sprintf("Wrong data type for 'stars', expected [], got %T", out["stars"])) 341 expectedListItem := "Betelgeuse" 342 require.Len(t, outputInterfaceList, expectedListLen, "Output list should contain %d items", expectedListLen) 343 require.Equal(t, expectedListItem, outputInterfaceList[2].(string), "List item %q should match %q", 344 expectedListItem, outputInterfaceList[0].(string)) 345 346 //Map Value 347 expectedMapLen := 4 348 outputInterfaceMap, ok := out["constellations"].(map[string]interface{}) 349 require.True(t, ok, fmt.Sprintf("Wrong data type for 'constellations', expected map[string], got %T", out["constellations"])) 350 expectedMapItem := "Aldebaran" 351 require.Len(t, outputInterfaceMap, expectedMapLen, "Output map should contain 4 items") 352 require.Equal(t, expectedMapItem, outputInterfaceMap["Taurus"].(string), "Map item %q should match %q", 353 expectedMapItem, outputInterfaceMap["Taurus"].(string)) 354 } 355 356 func TestOutputsForKeysError(t *testing.T) { 357 t.Parallel() 358 359 testFolder, err := files.CopyTerraformFolderToTemp("../../test/fixtures/terraform-output-map", t.Name()) 360 require.NoError(t, err) 361 362 options := &Options{ 363 TerraformDir: testFolder, 364 } 365 366 InitAndApply(t, options) 367 368 _, err = OutputForKeysE(t, options, []string{"random_key"}) 369 370 require.Error(t, err) 371 }