Skip to content

Commit

Permalink
Merge pull request #320 from klebediev/feature/terraform-plan
Browse files Browse the repository at this point in the history
modules/terraform:  Rename existing InitAndPlan* functions to InitAndPlanWithExitCode*, add InitAndPlan* and Plan* functions with consistent signatures
  • Loading branch information
yorinasub17 committed Jun 11, 2019
2 parents f3916f7 + f19570a commit 59a9dff
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 22 deletions.
49 changes: 43 additions & 6 deletions modules/terraform/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,59 @@ import (
"github.com/stretchr/testify/require"
)

// InitAndPlan runs terraform init and plan with the given options and return stdout/stderr from the apply command.
func InitAndPlan(t *testing.T, options *Options) int {
exitCode, err := InitAndPlanE(t, options)
// InitAndPlan runs terraform init and plan with the given options and returns stdout/stderr from the plan command.
// This will fail the test if there is an error in the command.
func InitAndPlan(t *testing.T, options *Options) string {
out, err := InitAndPlanE(t, options)
require.NoError(t, err)
return out
}

// InitAndPlanE runs terraform init and plan with the given options and returns stdout/stderr from the plan command.
func InitAndPlanE(t *testing.T, options *Options) (string, error) {
if _, err := InitE(t, options); err != nil {
return "", err
}

if _, err := GetE(t, options); err != nil {
return "", err
}

return PlanE(t, options)
}

// Plan runs terraform plan with the given options and returns stdout/stderr.
// This will fail the test if there is an error in the command.
func Plan(t *testing.T, options *Options) string {
out, err := PlanE(t, options)
require.NoError(t, err)
return out
}

// PlanE runs terraform plan with the given options and returns stdout/stderr.
func PlanE(t *testing.T, options *Options) (string, error) {
return RunTerraformCommandE(t, options, FormatArgs(options, "plan", "-input=false", "-lock=false")...)
}

// InitAndPlanWithExitCode runs terraform init and plan with the given options and returns exitcode for the plan command.
// This will fail the test if there is an error in the command.
func InitAndPlanWithExitCode(t *testing.T, options *Options) int {
exitCode, err := InitAndPlanWithExitCodeE(t, options)
require.NoError(t, err)
return exitCode
}

// InitAndPlanE runs terraform init and plan with the given options and return stdout/stderr from the apply command.
func InitAndPlanE(t *testing.T, options *Options) (int, error) {
// InitAndPlanWithExitCodeE runs terraform init and plan with the given options and returns exitcode for the plan command.
func InitAndPlanWithExitCodeE(t *testing.T, options *Options) (int, error) {
if _, err := InitE(t, options); err != nil {
return DefaultErrorExitCode, err
}

return PlanExitCodeE(t, options)
}

// PlanExitCode runs terraform apply with the given options and returns the detailed exitcode.
// PlanExitCode runs terraform plan with the given options and returns the detailed exitcode.
// This will fail the test if there is an error in the command.
func PlanExitCode(t *testing.T, options *Options) int {
exitCode, err := PlanExitCodeE(t, options)
require.NoError(t, err)
Expand All @@ -36,6 +72,7 @@ func PlanExitCodeE(t *testing.T, options *Options) (int, error) {
}

// TgPlanAllExitCode runs terragrunt plan-all with the given options and returns the detailed exitcode.
// This will fail the test if there is an error in the command.
func TgPlanAllExitCode(t *testing.T, options *Options) int {
exitCode, err := TgPlanAllExitCodeE(t, options)
require.NoError(t, err)
Expand Down
53 changes: 37 additions & 16 deletions modules/terraform/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,67 @@ package terraform
import (
"testing"

"github.com/gruntwork-io/terratest/modules/aws"
"github.com/gruntwork-io/terratest/modules/files"
"github.com/stretchr/testify/require"
)

func TestPlanWithNoChanges(t *testing.T) {
func TestInitAndPlanWithError(t *testing.T) {
t.Parallel()

testFolder, err := files.CopyTerraformFolderToTemp("../../test/fixtures/terraform-with-plan-error", t.Name())
require.NoError(t, err)

options := &Options{
TerraformDir: testFolder,
}

_, err = InitAndPlanE(t, options)
require.Error(t, err)
}

func TestInitAndPlanWithNoError(t *testing.T) {
t.Parallel()

testFolder, err := files.CopyTerraformFolderToTemp("../../test/fixtures/terraform-no-error", t.Name())
require.NoError(t, err)

awsRegion := aws.GetRandomStableRegion(t, nil, nil)
options := &Options{
TerraformDir: testFolder,
}

EnvVars: map[string]string{
"AWS_DEFAULT_REGION": awsRegion,
},
out, err := InitAndPlanE(t, options)
require.NoError(t, err)
require.Contains(t, out, "No changes. Infrastructure is up-to-date.")
}

func TestPlanWithExitCodeWithNoChanges(t *testing.T) {
t.Parallel()
testFolder, err := files.CopyTerraformFolderToTemp("../../test/fixtures/terraform-no-error", t.Name())
require.NoError(t, err)

options := &Options{
TerraformDir: testFolder,
}
exitCode := InitAndPlan(t, options)
exitCode := InitAndPlanWithExitCode(t, options)
require.Equal(t, DefaultSuccessExitCode, exitCode)
}

func TestPlanWithChanges(t *testing.T) {
func TestPlanWithExitCodeWithChanges(t *testing.T) {
t.Parallel()
testFolder, err := files.CopyTerraformFolderToTemp("../../examples/terraform-aws-example", t.Name())
testFolder, err := files.CopyTerraformFolderToTemp("../../test/fixtures/terraform-basic-configuration", t.Name())
require.NoError(t, err)

awsRegion := aws.GetRandomStableRegion(t, nil, nil)
options := &Options{
TerraformDir: testFolder,

EnvVars: map[string]string{
"AWS_DEFAULT_REGION": awsRegion,
Vars: map[string]interface{}{
"cnt": 1,
},
}
exitCode := InitAndPlan(t, options)
exitCode := InitAndPlanWithExitCode(t, options)
require.Equal(t, TerraformPlanChangesPresentExitCode, exitCode)
}

func TestPlanWithFailure(t *testing.T) {
func TestPlanWithExitCodeWithFailure(t *testing.T) {
t.Parallel()

testFolder, err := files.CopyTerraformFolderToTemp("../../test/fixtures/terraform-with-plan-error", t.Name())
Expand All @@ -52,7 +73,7 @@ func TestPlanWithFailure(t *testing.T) {
TerraformDir: testFolder,
}

_, getExitCodeErr := InitAndPlanE(t, options)
_, getExitCodeErr := InitAndPlanWithExitCodeE(t, options)
require.Error(t, getExitCodeErr)
}

Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/terraform-basic-configuration/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
variable cnt {}

resource "null_resource" test {
count = "${var.cnt}"
}

0 comments on commit 59a9dff

Please sign in to comment.