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