github.com/terraform-modules-krish/terratest@v0.29.0/modules/terraform/plan.go (about) 1 package terraform 2 3 import ( 4 "fmt" 5 6 "github.com/terraform-modules-krish/terratest/modules/testing" 7 "github.com/stretchr/testify/require" 8 ) 9 10 // InitAndPlan runs terraform init and plan with the given options and returns stdout/stderr from the plan command. 11 // This will fail the test if there is an error in the command. 12 func InitAndPlan(t testing.TestingT, options *Options) string { 13 out, err := InitAndPlanE(t, options) 14 require.NoError(t, err) 15 return out 16 } 17 18 // InitAndPlanE runs terraform init and plan with the given options and returns stdout/stderr from the plan command. 19 func InitAndPlanE(t testing.TestingT, options *Options) (string, error) { 20 if _, err := InitE(t, options); err != nil { 21 return "", err 22 } 23 24 if _, err := GetE(t, options); err != nil { 25 return "", err 26 } 27 28 return PlanE(t, options) 29 } 30 31 // Plan runs terraform plan with the given options and returns stdout/stderr. 32 // This will fail the test if there is an error in the command. 33 func Plan(t testing.TestingT, options *Options) string { 34 out, err := PlanE(t, options) 35 require.NoError(t, err) 36 return out 37 } 38 39 // PlanE runs terraform plan with the given options and returns stdout/stderr. 40 func PlanE(t testing.TestingT, options *Options) (string, error) { 41 return RunTerraformCommandE(t, options, FormatArgs(options, "plan", "-input=false", "-lock=false")...) 42 } 43 44 // InitAndPlanWithExitCode runs terraform init and plan with the given options and returns exitcode for the plan command. 45 // This will fail the test if there is an error in the command. 46 func InitAndPlanWithExitCode(t testing.TestingT, options *Options) int { 47 exitCode, err := InitAndPlanWithExitCodeE(t, options) 48 require.NoError(t, err) 49 return exitCode 50 } 51 52 // InitAndPlanWithExitCodeE runs terraform init and plan with the given options and returns exitcode for the plan command. 53 func InitAndPlanWithExitCodeE(t testing.TestingT, options *Options) (int, error) { 54 if _, err := InitE(t, options); err != nil { 55 return DefaultErrorExitCode, err 56 } 57 58 return PlanExitCodeE(t, options) 59 } 60 61 // PlanExitCode runs terraform plan with the given options and returns the detailed exitcode. 62 // This will fail the test if there is an error in the command. 63 func PlanExitCode(t testing.TestingT, options *Options) int { 64 exitCode, err := PlanExitCodeE(t, options) 65 require.NoError(t, err) 66 return exitCode 67 } 68 69 // PlanExitCodeE runs terraform plan with the given options and returns the detailed exitcode. 70 func PlanExitCodeE(t testing.TestingT, options *Options) (int, error) { 71 return GetExitCodeForTerraformCommandE(t, options, FormatArgs(options, "plan", "-input=false", "-detailed-exitcode")...) 72 } 73 74 // TgPlanAllExitCode runs terragrunt plan-all with the given options and returns the detailed exitcode. 75 // This will fail the test if there is an error in the command. 76 func TgPlanAllExitCode(t testing.TestingT, options *Options) int { 77 exitCode, err := TgPlanAllExitCodeE(t, options) 78 require.NoError(t, err) 79 return exitCode 80 } 81 82 // TgPlanAllExitCodeE runs terragrunt plan-all with the given options and returns the detailed exitcode. 83 func TgPlanAllExitCodeE(t testing.TestingT, options *Options) (int, error) { 84 if options.TerraformBinary != "terragrunt" { 85 return 1, fmt.Errorf("terragrunt must be set as TerraformBinary to use this method") 86 } 87 88 return GetExitCodeForTerraformCommandE(t, options, FormatArgs(options, "plan-all", "--input=false", "--lock=true", "--detailed-exitcode")...) 89 }