github.com/mponton/terratest@v0.44.0/modules/test-structure/test_structure_test.go (about) 1 package test_structure 2 3 import ( 4 "os" 5 "path/filepath" 6 "testing" 7 8 "github.com/mponton/terratest/modules/collections" 9 "github.com/stretchr/testify/assert" 10 "github.com/stretchr/testify/require" 11 ) 12 13 func TestCopyToTempFolder(t *testing.T) { 14 tempFolder := CopyTerraformFolderToTemp(t, "../../", "examples") 15 t.Log(tempFolder) 16 } 17 18 func TestCopySubtestToTempFolder(t *testing.T) { 19 t.Run("Subtest", func(t *testing.T) { 20 tempFolder := CopyTerraformFolderToTemp(t, "../../", "examples") 21 t.Log(tempFolder) 22 }) 23 } 24 25 // TestValidateAllTerraformModulesSucceedsOnValidTerraform points at a simple text fixture Terraform module that is 26 // known to be valid 27 func TestValidateAllTerraformModulesSucceedsOnValidTerraform(t *testing.T) { 28 cwd, err := os.Getwd() 29 require.NoError(t, err) 30 31 // Use the test fixtures directory as the RootDir for ValidationOptions 32 projectRootDir := filepath.Join(cwd, "../../test/fixtures") 33 34 opts, optsErr := NewValidationOptions(projectRootDir, []string{"terraform-validation-valid"}, []string{}) 35 require.NoError(t, optsErr) 36 37 ValidateAllTerraformModules(t, opts) 38 } 39 40 func TestNewValidationOptionsRejectsEmptyRootDir(t *testing.T) { 41 _, err := NewValidationOptions("", []string{}, []string{}) 42 require.Error(t, err) 43 44 _, terragruntErr := NewTerragruntValidationOptions("", nil, nil) 45 require.Error(t, terragruntErr) 46 } 47 48 func TestFindTerraformModulePathsInRootEExamples(t *testing.T) { 49 cwd, cwdErr := os.Getwd() 50 require.NoError(t, cwdErr) 51 52 opts, optsErr := NewValidationOptions(filepath.Join(cwd, "../../"), []string{}, []string{}) 53 require.NoError(t, optsErr) 54 55 subDirs, err := FindTerraformModulePathsInRootE(opts) 56 require.NoError(t, err) 57 // There are many valid Terraform modules in the root/examples directory of the Terratest project, so we should get back many results 58 require.Greater(t, len(subDirs), 0) 59 } 60 61 // This test calls ValidateAllTerraformModules on the Terratest root directory 62 func TestValidateAllTerraformModulesOnTerratest(t *testing.T) { 63 cwd, err := os.Getwd() 64 require.NoError(t, err) 65 66 projectRootDir := filepath.Join(cwd, "../..") 67 68 opts, optsErr := NewValidationOptions(projectRootDir, []string{}, []string{ 69 "test/fixtures/terraform-with-plan-error", 70 "test/fixtures/terragrunt/terragrunt-with-plan-error", 71 "examples/terraform-backend-example", 72 }) 73 require.NoError(t, optsErr) 74 75 ValidateAllTerraformModules(t, opts) 76 } 77 78 // Verify ExcludeDirs is working properly, by explicitly passing a list of two test fixture modules to exclude 79 // and ensuring at the end that they do not appear in the returned slice of sub directories to validate 80 // Then, re-run the function with no exclusions and ensure the excluded paths ARE returned in the result set when no 81 // exclusions are passed 82 func TestFindTerraformModulePathsInRootEWithResultsExclusion(t *testing.T) { 83 84 cwd, cwdErr := os.Getwd() 85 require.NoError(t, cwdErr) 86 87 projectRootDir := filepath.Join(cwd, "../..") 88 89 // First, call the FindTerraformModulePathsInRootE method with several exclusions 90 exclusions := []string{ 91 filepath.Join("test", "fixtures", "terraform-output"), 92 filepath.Join("test", "fixtures", "terraform-output-map"), 93 } 94 95 opts, optsErr := NewValidationOptions(projectRootDir, []string{}, exclusions) 96 require.NoError(t, optsErr) 97 98 subDirs, err := FindTerraformModulePathsInRootE(opts) 99 require.NoError(t, err) 100 require.Greater(t, len(subDirs), 0) 101 // Ensure none of the excluded paths were returned by FindTerraformModulePathsInRootE 102 for _, exclusion := range exclusions { 103 assert.False(t, collections.ListContains(subDirs, filepath.Join(projectRootDir, exclusion))) 104 } 105 106 // Next, call the same function but this time without exclusions and ensure that the excluded paths 107 // exist in the non-excluded result set 108 optsWithoutExclusions, optswoErr := NewValidationOptions(projectRootDir, []string{}, []string{}) 109 require.NoError(t, optswoErr) 110 111 subDirsWithoutExclusions, woExErr := FindTerraformModulePathsInRootE(optsWithoutExclusions) 112 require.NoError(t, woExErr) 113 require.Greater(t, len(subDirsWithoutExclusions), 0) 114 for _, exclusion := range exclusions { 115 assert.True(t, collections.ListContains(subDirsWithoutExclusions, filepath.Join(projectRootDir, exclusion))) 116 } 117 } 118 119 func TestValidateAllTerragruntModulesWithUnusedInputsInRelaxedMode(t *testing.T) { 120 121 cwd, err := os.Getwd() 122 require.NoError(t, err) 123 124 projectRootDir := filepath.Join(cwd, "../..") 125 126 opts, optsErr := NewTerragruntValidationOptions(projectRootDir, []string{"examples/terragrunt-example", "examples/terragrunt-second-example"}, []string{}) 127 require.NoError(t, optsErr) 128 129 ValidateAllTerraformModules(t, opts) 130 } 131 132 // This test calls ValidateAllTerraformModules on the Terratest root directory, looking for 133 // Terragrunt directories to validate 134 func TestValidateTerragruntModulesOnTerratestByInclusion(t *testing.T) { 135 cwd, err := os.Getwd() 136 require.NoError(t, err) 137 138 projectRootDir := filepath.Join(cwd, "../../") 139 140 exclusions := []string{} 141 142 opts, optsErr := NewTerragruntValidationOptions(projectRootDir, []string{"test/fixtures/terragrunt/terragrunt-output"}, exclusions) 143 require.NoError(t, optsErr) 144 145 ValidateAllTerraformModules(t, opts) 146 }