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

Support parallel testing in terraform testing framework #16807

Closed
wants to merge 8 commits into from
11 changes: 11 additions & 0 deletions helper/resource/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,12 @@ type ImportStateIdFunc func(*terraform.State) (string, error)
// When the destroy plan is executed, the config from the last TestStep
// is used to plan it.
type TestCase struct {
// IsParallel allows a test to run in parallel in Go tesing framework.
// Make sure the test case is independent without any shared resource.
// For subtests, if IsParallel is enabled while defining it, all the subtests
// within the same group will be run in paralllel.
IsParallel bool

// IsUnitTest allows a test to run regardless of the TF_ACC
// environment variable. This should be used with care - only for
// fast tests on local resources (e.g. remote state with a local
Expand Down Expand Up @@ -415,6 +421,10 @@ func LogOutput(t TestT) (logOutput io.Writer, err error) {
// long, we require the verbose flag so users are able to see progress
// output.
func Test(t TestT, c TestCase) {
if c.IsParallel {
t.Parallel()
}

// We only run acceptance tests if an env var is set because they're
// slow and generally require some outside configuration. You can opt out
// of this with OverrideEnvVar on individual TestCases.
Expand Down Expand Up @@ -1002,6 +1012,7 @@ type TestT interface {
Fatal(args ...interface{})
Skip(args ...interface{})
Name() string
Parallel()
}

// This is set to true by unit tests to alter some behavior
Expand Down
15 changes: 15 additions & 0 deletions helper/resource/testing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,15 @@ func TestTest_noEnv(t *testing.T) {
}
}

func TestTest_withParallelCase(t *testing.T) {
mt := new(mockT)
Test(mt, TestCase{IsParallel: true})

if !mt.Paralleled {
t.Fatal("parallel not called")
}
}

func TestTest_preCheck(t *testing.T) {
called := false

Expand Down Expand Up @@ -605,6 +614,7 @@ type mockT struct {
FatalArgs []interface{}
SkipCalled bool
SkipArgs []interface{}
Paralleled bool

f bool
}
Expand All @@ -631,6 +641,11 @@ func (t *mockT) Name() string {
return "MockedName"
}

func (t *mockT) Parallel() {
t.Paralleled = true
return
}

func (t *mockT) failed() bool {
return t.f
}
Expand Down