github.com/someshkoli/terratest@v0.41.1/modules/terraform/count_test.go (about) 1 package terraform 2 3 import ( 4 "testing" 5 6 "github.com/gruntwork-io/terratest/modules/files" 7 ttesting "github.com/gruntwork-io/terratest/modules/testing" 8 "github.com/stretchr/testify/assert" 9 "github.com/stretchr/testify/require" 10 ) 11 12 func TestGetResourceCount(t *testing.T) { 13 t.Parallel() 14 testFolder, err := files.CopyTerraformFolderToTemp("../../test/fixtures/terraform-basic-configuration", t.Name()) 15 require.NoError(t, err) 16 17 terraformOptions := &Options{ 18 TerraformDir: testFolder, 19 Vars: map[string]interface{}{ 20 "cnt": 1, 21 }, 22 } 23 24 cnt := GetResourceCount(t, InitAndPlan(t, terraformOptions)) 25 assert.Equal(t, 1, cnt.Add) 26 assert.Equal(t, 0, cnt.Change) 27 assert.Equal(t, 0, cnt.Destroy) 28 } 29 30 func TestGetResourceCountEColor(t *testing.T) { 31 t.Parallel() 32 runTestGetResourceCountE(t, false) 33 } 34 35 func TestGetResourceCountENoColor(t *testing.T) { 36 t.Parallel() 37 runTestGetResourceCountE(t, true) 38 } 39 40 func runTestGetResourceCountE(t *testing.T, noColor bool) { 41 testCases := []struct { 42 Name string 43 tfFuncToRun func(t ttesting.TestingT, options *Options) string 44 cntValue int 45 expectedAdd, expectedChange, expectedDestroy int 46 }{ 47 {"PlanZero", InitAndPlan, 0, 0, 0, 0}, 48 {"ApplyZero", InitAndApply, 0, 0, 0, 0}, 49 {"PlanAddResouce", InitAndPlan, 2, 2, 0, 0}, 50 {"ApplyAddResouce", InitAndApply, 2, 2, 0, 0}, 51 {"PlanNoOp", InitAndApply, 2, 0, 0, 0}, 52 {"ApplyNoOp", InitAndApply, 2, 0, 0, 0}, 53 {"PlanDestroyResource", InitAndPlan, 1, 0, 0, 1}, 54 {"ApplyDestroyResource", InitAndApply, 1, 0, 0, 1}, 55 {"Destroy", Destroy, 1, 0, 0, 1}, 56 {"DestroyNoOp", Destroy, 1, 0, 0, 0}, 57 } 58 59 testFolder, err := files.CopyTerraformFolderToTemp("../../test/fixtures/terraform-basic-configuration", t.Name()) 60 require.NoError(t, err) 61 62 terraformOptions := &Options{ 63 TerraformDir: testFolder, 64 Vars: map[string]interface{}{ 65 "cnt": 0, 66 }, 67 NoColor: noColor, 68 } 69 70 for _, tc := range testCases { 71 t.Run(tc.Name, 72 func(t *testing.T) { 73 terraformOptions.Vars["cnt"] = tc.cntValue 74 cnt, err := GetResourceCountE(t, tc.tfFuncToRun(t, terraformOptions)) 75 assert.NoError(t, err) 76 assert.Equal(t, tc.expectedAdd, cnt.Add) 77 assert.Equal(t, tc.expectedChange, cnt.Change) 78 assert.Equal(t, tc.expectedDestroy, cnt.Destroy) 79 }) 80 } 81 82 t.Run("InvalidInput", 83 func(t *testing.T) { 84 terraformOptions.Vars["cnt"] = "abc" 85 cmdout, _ := PlanE(t, terraformOptions) 86 cnt, err := GetResourceCountE(t, cmdout) 87 assert.EqualError(t, err, getResourceCountErrMessage) 88 assert.Nil(t, cnt) 89 }) 90 91 }