Skip to content

Commit

Permalink
feat: add AssertTgPlanAllExitCode
Browse files Browse the repository at this point in the history
this adds an assertion to use with TgPlanAllE.

since run-all plan with `-detailed-exitcode` can return a `0` or `2` on successful plans,
this assertions checks for either acceptable value and asserts if true or false
depending on the assertion under test, designated by the `assertTrue` input arg.

this has been passed around as a helper function for simple regression testing using plan all
across many modules, seemed like time to share it with the community.
  • Loading branch information
bt-macole committed Jul 22, 2023
1 parent 64b4cee commit 625f3ae
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
19 changes: 19 additions & 0 deletions modules/terraform/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/gruntwork-io/terratest/modules/logger"
"github.com/gruntwork-io/terratest/modules/testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -144,6 +145,24 @@ func TgPlanAllExitCodeE(t testing.TestingT, options *Options) (int, error) {
"--lock=true", "--detailed-exitcode")...)
}

// AssertTgPlanAllExitCode asserts the succuess (or failure) of a terragrunt run-all plan.
// On success, terragrunt will exit 0 on a plan that has previously been applied (has state)
// and exit with 2 for plans that have never been applied when ran with `-detailed-exitcode`.
func AssertTgPlanAllExitCode(t testing.TestingT, exitCode int, assertTrue bool) {

validExitCodes := map[int]bool{
0: true,
2: true,
}

_, hasKey := validExitCodes[exitCode]
if assertTrue {
assert.True(t, hasKey)
} else {
assert.False(t, hasKey)
}
}

// Custom errors

var (
Expand Down
50 changes: 50 additions & 0 deletions modules/terraform/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,53 @@ func TestTgPlanAllWithError(t *testing.T) {

require.Equal(t, DefaultErrorExitCode, getExitCode)
}

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

testFolder, err := files.CopyTerragruntFolderToTemp("../../test/fixtures/terragrunt/terragrunt-multi-plan", t.Name())
require.NoError(t, err)

options := &Options{
TerraformDir: testFolder,
TerraformBinary: "terragrunt",
}

getExitCode, errExitCode := TgPlanAllExitCodeE(t, options)
if errExitCode != nil {
t.Fatal(errExitCode)
}

// since there is no state file we expect `2` to be the success exit code
assert.Equal(t, 2, getExitCode)
AssertTgPlanAllExitCode(t, getExitCode, true)

TgApplyAll(t, options)

getExitCode, errExitCode = TgPlanAllExitCodeE(t, options)
if errExitCode != nil {
t.Fatal(errExitCode)
}

// since there is a state file we expect `0` to be the success exit code
assert.Equal(t, 0, getExitCode)
AssertTgPlanAllExitCode(t, getExitCode, true)
}

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

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

options := &Options{
TerraformDir: testFolder,
TerraformBinary: "terragrunt",
}

getExitCode, errExitCode := TgPlanAllExitCodeE(t, options)
// GetExitCodeForRunCommandError was unable to determine the exit code correctly
require.NoError(t, errExitCode)

AssertTgPlanAllExitCode(t, getExitCode, false)
}

0 comments on commit 625f3ae

Please sign in to comment.