Skip to content

Commit

Permalink
add new e2e test method so we can skip idempotent check since some ex…
Browse files Browse the repository at this point in the history
…amples would try to retrieve data from data plane api and would failed due to the network policy
  • Loading branch information
lonegunmanb committed Mar 22, 2023
1 parent bd04177 commit e11b923
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 11 deletions.
26 changes: 23 additions & 3 deletions e2etest.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ import (
"github.com/stretchr/testify/require"
)

type TestOptions struct {
TerraformOptions terraform.Options
Assertion func(*testing.T, TerraformOutput)
SkipIdempotentCheck bool
}

var copyLock = &KeyedMutex{}

type TerraformOutput = map[string]interface{}
Expand Down Expand Up @@ -43,10 +49,20 @@ func (e2eTestExecutor) Logger() logger.TestLogger {
}

func RunE2ETest(t *testing.T, moduleRootPath, exampleRelativePath string, option terraform.Options, assertion func(*testing.T, TerraformOutput)) {
initAndApplyAndIdempotentTest(t, moduleRootPath, exampleRelativePath, option, assertion, e2eTestExecutor{})
initAndApplyAndIdempotentTest(t, moduleRootPath, exampleRelativePath, option, assertion, true, e2eTestExecutor{})
}

func RunE2ETestWithOption(t *testing.T, moduleRootPath, exampleRelativePath string, testOption TestOptions) {
initAndApplyAndIdempotentTest(t,
moduleRootPath,
exampleRelativePath,
testOption.TerraformOptions,
testOption.Assertion,
testOption.SkipIdempotentCheck,
e2eTestExecutor{})
}

func initAndApplyAndIdempotentTest(t *testing.T, moduleRootPath string, exampleRelativePath string, option terraform.Options, assertion func(*testing.T, TerraformOutput), executor testExecutor) {
func initAndApplyAndIdempotentTest(t *testing.T, moduleRootPath string, exampleRelativePath string, option terraform.Options, assertion func(*testing.T, TerraformOutput), skipCheckIdempotent bool, executor testExecutor) {
tryParallel(t)
defer executor.TearDown(t, moduleRootPath, exampleRelativePath)
testDir := filepath.Join(moduleRootPath, exampleRelativePath)
Expand All @@ -70,7 +86,11 @@ func initAndApplyAndIdempotentTest(t *testing.T, moduleRootPath string, exampleR
defer destroy(t, option)

initAndApply(t, &option)
if err := initAndPlanAndIdempotentAtEasyMode(t, option); err != nil {
var err error
if !skipCheckIdempotent {
err = initAndPlanAndIdempotentAtEasyMode(t, option)
}
if err != nil {
t.Fatalf(err.Error())
}
if assertion != nil {
Expand Down
29 changes: 29 additions & 0 deletions e2etest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
"testing"

"github.com/gruntwork-io/terratest/modules/terraform"
"github.com/prashantv/gostub"
"github.com/stretchr/testify/assert"
"github.com/timandy/routine"
)

func TestE2EExampleTest(t *testing.T) {
Expand All @@ -20,6 +22,33 @@ func TestE2EExampleTest(t *testing.T) {
})
}

func TestE2EExample_WithoutIdempotent(t *testing.T) {
currentId := routine.Goid()
originStub := initAndPlanAndIdempotentAtEasyMode
stub := gostub.Stub(&initAndPlanAndIdempotentAtEasyMode, func(t *testing.T, opts terraform.Options) error {
// Do not impact other tests.
id := routine.Goid()
if id != currentId {
return originStub(t, opts)
}
assert.FailNow(t, "should not be called")
return nil
})
defer stub.Reset()
RunE2ETestWithOption(t, "./", "example/basic",
TestOptions{
TerraformOptions: terraform.Options{
Upgrade: true,
},
Assertion: func(t *testing.T, output TerraformOutput) {
resId, ok := output["resource_id"].(string)
assert.True(t, ok)
assert.NotEqual(t, "", resId, "expected output `resource_id`")
},
SkipIdempotentCheck: true,
})
}

func TestE2EExampleTest_setEnvWontCausePanicOnParallel(t *testing.T) {
for i := 0; i < runtime.NumCPU(); i++ {
iterator := i
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/r3labs/diff/v3 v3.0.1
github.com/spf13/afero v1.9.5
github.com/stretchr/testify v1.8.2
github.com/timandy/routine v1.1.1
golang.org/x/mod v0.9.0
golang.org/x/oauth2 v0.6.0
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,8 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/timandy/routine v1.1.1 h1:6/Z7qLFZj3GrzuRksBFzIG8YGUh8CLhjnnMePBQTrEI=
github.com/timandy/routine v1.1.1/go.mod h1:OZHPOKSvqL/ZvqXFkNZyit0xIVelERptYXdAHH00adQ=
github.com/tmccombs/hcl2json v0.3.3 h1:+DLNYqpWE0CsOQiEZu+OZm5ZBImake3wtITYxQ8uLFQ=
github.com/tmccombs/hcl2json v0.3.3/go.mod h1:Y2chtz2x9bAeRTvSibVRVgbLJhLJXKlUeIvjeVdnm4w=
github.com/ulikunitz/xz v0.5.10 h1:t92gobL9l3HE202wg3rlk19F6X+JOxl9BBrCCMYEYd8=
Expand Down
11 changes: 6 additions & 5 deletions stream_logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package terraform_module_test_helper

import (
"bytes"
"github.com/prashantv/gostub"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"io"
"math"
"testing"

"github.com/prashantv/gostub"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestStreamLoggerShouldLogSth(t *testing.T) {
Expand Down Expand Up @@ -46,8 +47,8 @@ func TestStreamLoggerClose(t *testing.T) {

type testStream struct {
content byte
length int
finish chan int
length int
finish chan int
}

func newTestStream(content byte, length int) *testStream {
Expand Down
2 changes: 1 addition & 1 deletion unittest.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ func (u unitTestExecutor) Logger() logger.TestLogger {
}

func RunUnitTest(t *testing.T, moduleRootPath, exampleRelativePath string, option terraform.Options, assertion func(*testing.T, TerraformOutput)) {
initAndApplyAndIdempotentTest(t, moduleRootPath, exampleRelativePath, option, assertion, unitTestExecutor{})
initAndApplyAndIdempotentTest(t, moduleRootPath, exampleRelativePath, option, assertion, false, unitTestExecutor{})
}
2 changes: 1 addition & 1 deletion upgradetest.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func diffTwoVersions(t *testing.T, opts terraform.Options, originTerraformDir st
return initAndPlanAndIdempotentAtEasyMode(t, opts)
}

func initAndPlanAndIdempotentAtEasyMode(t *testing.T, opts terraform.Options) error {
var initAndPlanAndIdempotentAtEasyMode = func(t *testing.T, opts terraform.Options) error {
opts.PlanFilePath = filepath.Join(opts.TerraformDir, "tf.plan")
opts.Logger = logger.Discard
exitCode := initAndPlanWithExitCode(t, &opts)
Expand Down
1 change: 0 additions & 1 deletion version_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
test_structure "github.com/gruntwork-io/terratest/modules/test-structure"
)


var initE = terraform.InitE
var runTerraformCommandE = terraform.RunTerraformCommandE
var recordFileLocks = &KeyedMutex{}
Expand Down

0 comments on commit e11b923

Please sign in to comment.