Skip to content

Commit e11b923

Browse files
committed
add new e2e test method so we can skip idempotent check since some examples would try to retrieve data from data plane api and would failed due to the network policy
1 parent bd04177 commit e11b923

8 files changed

Lines changed: 63 additions & 11 deletions

File tree

e2etest.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ import (
1616
"github.com/stretchr/testify/require"
1717
)
1818

19+
type TestOptions struct {
20+
TerraformOptions terraform.Options
21+
Assertion func(*testing.T, TerraformOutput)
22+
SkipIdempotentCheck bool
23+
}
24+
1925
var copyLock = &KeyedMutex{}
2026

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

4551
func RunE2ETest(t *testing.T, moduleRootPath, exampleRelativePath string, option terraform.Options, assertion func(*testing.T, TerraformOutput)) {
46-
initAndApplyAndIdempotentTest(t, moduleRootPath, exampleRelativePath, option, assertion, e2eTestExecutor{})
52+
initAndApplyAndIdempotentTest(t, moduleRootPath, exampleRelativePath, option, assertion, true, e2eTestExecutor{})
53+
}
54+
55+
func RunE2ETestWithOption(t *testing.T, moduleRootPath, exampleRelativePath string, testOption TestOptions) {
56+
initAndApplyAndIdempotentTest(t,
57+
moduleRootPath,
58+
exampleRelativePath,
59+
testOption.TerraformOptions,
60+
testOption.Assertion,
61+
testOption.SkipIdempotentCheck,
62+
e2eTestExecutor{})
4763
}
4864

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

7288
initAndApply(t, &option)
73-
if err := initAndPlanAndIdempotentAtEasyMode(t, option); err != nil {
89+
var err error
90+
if !skipCheckIdempotent {
91+
err = initAndPlanAndIdempotentAtEasyMode(t, option)
92+
}
93+
if err != nil {
7494
t.Fatalf(err.Error())
7595
}
7696
if assertion != nil {

e2etest_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import (
77
"testing"
88

99
"github.com/gruntwork-io/terratest/modules/terraform"
10+
"github.com/prashantv/gostub"
1011
"github.com/stretchr/testify/assert"
12+
"github.com/timandy/routine"
1113
)
1214

1315
func TestE2EExampleTest(t *testing.T) {
@@ -20,6 +22,33 @@ func TestE2EExampleTest(t *testing.T) {
2022
})
2123
}
2224

25+
func TestE2EExample_WithoutIdempotent(t *testing.T) {
26+
currentId := routine.Goid()
27+
originStub := initAndPlanAndIdempotentAtEasyMode
28+
stub := gostub.Stub(&initAndPlanAndIdempotentAtEasyMode, func(t *testing.T, opts terraform.Options) error {
29+
// Do not impact other tests.
30+
id := routine.Goid()
31+
if id != currentId {
32+
return originStub(t, opts)
33+
}
34+
assert.FailNow(t, "should not be called")
35+
return nil
36+
})
37+
defer stub.Reset()
38+
RunE2ETestWithOption(t, "./", "example/basic",
39+
TestOptions{
40+
TerraformOptions: terraform.Options{
41+
Upgrade: true,
42+
},
43+
Assertion: func(t *testing.T, output TerraformOutput) {
44+
resId, ok := output["resource_id"].(string)
45+
assert.True(t, ok)
46+
assert.NotEqual(t, "", resId, "expected output `resource_id`")
47+
},
48+
SkipIdempotentCheck: true,
49+
})
50+
}
51+
2352
func TestE2EExampleTest_setEnvWontCausePanicOnParallel(t *testing.T) {
2453
for i := 0; i < runtime.NumCPU(); i++ {
2554
iterator := i

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ require (
1515
github.com/r3labs/diff/v3 v3.0.1
1616
github.com/spf13/afero v1.9.5
1717
github.com/stretchr/testify v1.8.2
18+
github.com/timandy/routine v1.1.1
1819
golang.org/x/mod v0.9.0
1920
golang.org/x/oauth2 v0.6.0
2021
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,8 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
556556
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
557557
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
558558
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
559+
github.com/timandy/routine v1.1.1 h1:6/Z7qLFZj3GrzuRksBFzIG8YGUh8CLhjnnMePBQTrEI=
560+
github.com/timandy/routine v1.1.1/go.mod h1:OZHPOKSvqL/ZvqXFkNZyit0xIVelERptYXdAHH00adQ=
559561
github.com/tmccombs/hcl2json v0.3.3 h1:+DLNYqpWE0CsOQiEZu+OZm5ZBImake3wtITYxQ8uLFQ=
560562
github.com/tmccombs/hcl2json v0.3.3/go.mod h1:Y2chtz2x9bAeRTvSibVRVgbLJhLJXKlUeIvjeVdnm4w=
561563
github.com/ulikunitz/xz v0.5.10 h1:t92gobL9l3HE202wg3rlk19F6X+JOxl9BBrCCMYEYd8=

stream_logger_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ package terraform_module_test_helper
22

33
import (
44
"bytes"
5-
"github.com/prashantv/gostub"
6-
"github.com/stretchr/testify/assert"
7-
"github.com/stretchr/testify/require"
85
"io"
96
"math"
107
"testing"
8+
9+
"github.com/prashantv/gostub"
10+
"github.com/stretchr/testify/assert"
11+
"github.com/stretchr/testify/require"
1112
)
1213

1314
func TestStreamLoggerShouldLogSth(t *testing.T) {
@@ -46,8 +47,8 @@ func TestStreamLoggerClose(t *testing.T) {
4647

4748
type testStream struct {
4849
content byte
49-
length int
50-
finish chan int
50+
length int
51+
finish chan int
5152
}
5253

5354
func newTestStream(content byte, length int) *testStream {

unittest.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ func (u unitTestExecutor) Logger() logger.TestLogger {
1818
}
1919

2020
func RunUnitTest(t *testing.T, moduleRootPath, exampleRelativePath string, option terraform.Options, assertion func(*testing.T, TerraformOutput)) {
21-
initAndApplyAndIdempotentTest(t, moduleRootPath, exampleRelativePath, option, assertion, unitTestExecutor{})
21+
initAndApplyAndIdempotentTest(t, moduleRootPath, exampleRelativePath, option, assertion, false, unitTestExecutor{})
2222
}

upgradetest.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func diffTwoVersions(t *testing.T, opts terraform.Options, originTerraformDir st
143143
return initAndPlanAndIdempotentAtEasyMode(t, opts)
144144
}
145145

146-
func initAndPlanAndIdempotentAtEasyMode(t *testing.T, opts terraform.Options) error {
146+
var initAndPlanAndIdempotentAtEasyMode = func(t *testing.T, opts terraform.Options) error {
147147
opts.PlanFilePath = filepath.Join(opts.TerraformDir, "tf.plan")
148148
opts.Logger = logger.Discard
149149
exitCode := initAndPlanWithExitCode(t, &opts)

version_helper.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
test_structure "github.com/gruntwork-io/terratest/modules/test-structure"
1616
)
1717

18-
1918
var initE = terraform.InitE
2019
var runTerraformCommandE = terraform.RunTerraformCommandE
2120
var recordFileLocks = &KeyedMutex{}

0 commit comments

Comments
 (0)