Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add helper for extracting plan struct without logging and without specifying a plan file #957

Merged
merged 1 commit into from
Jul 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions modules/terraform/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package terraform

import (
"fmt"
"io/ioutil"
"os"

"github.com/gruntwork-io/terratest/modules/logger"
"github.com/gruntwork-io/terratest/modules/testing"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -59,6 +62,22 @@ func InitAndPlanAndShowE(t testing.TestingT, options *Options) (string, error) {
return ShowE(t, options)
}

// InitAndPlanAndShowWithStructNoLog runs InitAndPlanAndShowWithStruct without logging and also by allocating a
// temporary plan file destination that is discarded before returning the struct.
func InitAndPlanAndShowWithStructNoLogTempPlanFile(t testing.TestingT, options *Options) *PlanStruct {
oldLogger := options.Logger
options.Logger = logger.Discard
defer func() { options.Logger = oldLogger }()

tmpFile, err := ioutil.TempFile("", "terratest-plan-file-")
require.NoError(t, err)
require.NoError(t, tmpFile.Close())
defer require.NoError(t, os.Remove(tmpFile.Name()))

options.PlanFilePath = tmpFile.Name()
return InitAndPlanAndShowWithStruct(t, options)
}

// InitAndPlanAndShowWithStruct runs terraform init, then terraform plan, and then terraform show with the given
// options, and parses the json result into a go struct. This will fail the test if there is an error in the command.
func InitAndPlanAndShowWithStruct(t testing.TestingT, options *Options) *PlanStruct {
Expand Down
16 changes: 16 additions & 0 deletions modules/terraform/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,22 @@ func TestInitAndPlanWithPlanFile(t *testing.T) {
assert.FileExists(t, planFilePath, "Plan file was not saved to expected location:", planFilePath)
}

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

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

options := &Options{
TerraformDir: testFolder,
Vars: map[string]interface{}{
"cnt": 1,
},
}
planStruct := InitAndPlanAndShowWithStructNoLogTempPlanFile(t, options)
assert.Equal(t, 1, len(planStruct.ResourceChangesMap))
}

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