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

feat: add AssertTgPlanAllExitCode #1323

Merged
merged 1 commit into from
Jul 26, 2023
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 @@ -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
49 changes: 49 additions & 0 deletions modules/terraform/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,52 @@ 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)
require.NoError(t, errExitCode)

AssertTgPlanAllExitCode(t, getExitCode, false)
}