Skip to content

Commit d196d28

Browse files
committed
Refactor cloud table test runs
As the cloud e2e tests evolved some common patters became apparent. This standardizes and consolidates the patterns into a common test runner that takes the table tests and runs them in parallel. Some tests also needed to be converted to utilize table tests.
1 parent c647b41 commit d196d28

11 files changed

+574
-1827
lines changed

internal/cloud/e2e/apply_auto_approve_test.go

+2-79
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,9 @@ package main
22

33
import (
44
"context"
5-
"io/ioutil"
6-
"os"
75
"testing"
86

9-
expect "github.com/Netflix/go-expect"
107
tfe "github.com/hashicorp/go-tfe"
11-
"github.com/hashicorp/terraform/internal/e2e"
128
tfversion "github.com/hashicorp/terraform/version"
139
)
1410

@@ -19,10 +15,7 @@ func Test_terraform_apply_autoApprove(t *testing.T) {
1915

2016
ctx := context.Background()
2117

22-
cases := map[string]struct {
23-
operations []operationSets
24-
validations func(t *testing.T, orgName string)
25-
}{
18+
cases := testCases{
2619
"workspace manual apply, terraform apply without auto-approve, expect prompt": {
2720
operations: []operationSets{
2821
{
@@ -180,76 +173,6 @@ func Test_terraform_apply_autoApprove(t *testing.T) {
180173
},
181174
},
182175
}
183-
for name, tc := range cases {
184-
tc := tc // rebind tc into this lexical scope
185-
t.Run(name, func(subtest *testing.T) {
186-
subtest.Parallel()
187-
organization, cleanup := createOrganization(t)
188-
defer cleanup()
189-
exp, err := expect.NewConsole(defaultOpts()...)
190-
if err != nil {
191-
subtest.Fatal(err)
192-
}
193-
defer exp.Close()
194176

195-
tmpDir, err := ioutil.TempDir("", "terraform-test")
196-
if err != nil {
197-
subtest.Fatal(err)
198-
}
199-
defer os.RemoveAll(tmpDir)
200-
201-
tf := e2e.NewBinary(terraformBin, tmpDir)
202-
tf.AddEnv(cliConfigFileEnv)
203-
defer tf.Close()
204-
205-
for _, op := range tc.operations {
206-
op.prep(t, organization.Name, tf.WorkDir())
207-
for _, tfCmd := range op.commands {
208-
cmd := tf.Cmd(tfCmd.command...)
209-
cmd.Stdin = exp.Tty()
210-
cmd.Stdout = exp.Tty()
211-
cmd.Stderr = exp.Tty()
212-
213-
err = cmd.Start()
214-
if err != nil {
215-
subtest.Fatal(err)
216-
}
217-
218-
if tfCmd.expectedCmdOutput != "" {
219-
got, err := exp.ExpectString(tfCmd.expectedCmdOutput)
220-
if err != nil {
221-
subtest.Fatalf("error while waiting for output\nwant: %s\nerror: %s\noutput\n%s", tfCmd.expectedCmdOutput, err, got)
222-
}
223-
}
224-
225-
lenInput := len(tfCmd.userInput)
226-
lenInputOutput := len(tfCmd.postInputOutput)
227-
if lenInput > 0 {
228-
for i := 0; i < lenInput; i++ {
229-
input := tfCmd.userInput[i]
230-
exp.SendLine(input)
231-
// use the index to find the corresponding
232-
// output that matches the input.
233-
if lenInputOutput-1 >= i {
234-
output := tfCmd.postInputOutput[i]
235-
_, err := exp.ExpectString(output)
236-
if err != nil {
237-
subtest.Fatal(err)
238-
}
239-
}
240-
}
241-
}
242-
243-
err = cmd.Wait()
244-
if err != nil && !tfCmd.expectError {
245-
subtest.Fatal(err)
246-
}
247-
}
248-
}
249-
250-
if tc.validations != nil {
251-
tc.validations(t, organization.Name)
252-
}
253-
})
254-
}
177+
testRunner(t, cases, 1)
255178
}
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,15 @@
11
package main
22

33
import (
4-
"io/ioutil"
5-
"os"
64
"testing"
7-
8-
expect "github.com/Netflix/go-expect"
9-
"github.com/hashicorp/terraform/internal/e2e"
105
)
116

127
func Test_backend_apply_before_init(t *testing.T) {
138
t.Parallel()
149
skipIfMissingEnvVar(t)
1510
skipWithoutRemoteTerraformVersion(t)
1611

17-
cases := map[string]struct {
18-
operations []operationSets
19-
}{
12+
cases := testCases{
2013
"terraform apply with cloud block - blank state": {
2114
operations: []operationSets{
2215
{
@@ -71,72 +64,5 @@ func Test_backend_apply_before_init(t *testing.T) {
7164
},
7265
}
7366

74-
for name, tc := range cases {
75-
tc := tc // rebind tc into this lexical scope
76-
t.Run(name, func(subtest *testing.T) {
77-
subtest.Parallel()
78-
organization, cleanup := createOrganization(t)
79-
defer cleanup()
80-
exp, err := expect.NewConsole(defaultOpts()...)
81-
if err != nil {
82-
subtest.Fatal(err)
83-
}
84-
defer exp.Close()
85-
86-
tmpDir, err := ioutil.TempDir("", "terraform-test")
87-
if err != nil {
88-
subtest.Fatal(err)
89-
}
90-
defer os.RemoveAll(tmpDir)
91-
92-
tf := e2e.NewBinary(terraformBin, tmpDir)
93-
tf.AddEnv(cliConfigFileEnv)
94-
defer tf.Close()
95-
96-
for _, op := range tc.operations {
97-
op.prep(t, organization.Name, tf.WorkDir())
98-
for _, tfCmd := range op.commands {
99-
cmd := tf.Cmd(tfCmd.command...)
100-
cmd.Stdin = exp.Tty()
101-
cmd.Stdout = exp.Tty()
102-
cmd.Stderr = exp.Tty()
103-
104-
err = cmd.Start()
105-
if err != nil {
106-
subtest.Fatal(err)
107-
}
108-
109-
if tfCmd.expectedCmdOutput != "" {
110-
got, err := exp.ExpectString(tfCmd.expectedCmdOutput)
111-
if err != nil {
112-
subtest.Fatalf("error while waiting for output\nwant: %s\nerror: %s\noutput\n%s", tfCmd.expectedCmdOutput, err, got)
113-
}
114-
}
115-
116-
lenInput := len(tfCmd.userInput)
117-
lenInputOutput := len(tfCmd.postInputOutput)
118-
if lenInput > 0 {
119-
for i := 0; i < lenInput; i++ {
120-
input := tfCmd.userInput[i]
121-
exp.SendLine(input)
122-
// use the index to find the corresponding
123-
// output that matches the input.
124-
if lenInputOutput-1 >= i {
125-
output := tfCmd.postInputOutput[i]
126-
_, err := exp.ExpectString(output)
127-
if err != nil {
128-
subtest.Fatal(err)
129-
}
130-
}
131-
}
132-
}
133-
134-
err = cmd.Wait()
135-
if err != nil && !tfCmd.expectError {
136-
subtest.Fatal(err)
137-
}
138-
}
139-
}
140-
})
141-
}
67+
testRunner(t, cases, 1)
14268
}

internal/cloud/e2e/helper_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ import (
1515
)
1616

1717
const (
18-
expectConsoleTimeout = 60 * time.Second * 3
18+
// We need to give the console enough time to hear back.
19+
// 1 minute was too short in some cases, so this gives it ample time.
20+
expectConsoleTimeout = 3 * time.Minute
1921
)
2022

2123
type tfCommand struct {
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,15 @@
11
package main
22

33
import (
4-
"io/ioutil"
5-
"os"
64
"testing"
7-
8-
expect "github.com/Netflix/go-expect"
9-
"github.com/hashicorp/terraform/internal/e2e"
105
)
116

127
func Test_init_with_empty_tags(t *testing.T) {
138
t.Parallel()
149
skipIfMissingEnvVar(t)
1510
skipWithoutRemoteTerraformVersion(t)
1611

17-
cases := map[string]struct {
18-
operations []operationSets
19-
}{
12+
cases := testCases{
2013
"terraform init with cloud block - no tagged workspaces exist yet": {
2114
operations: []operationSets{
2215
{
@@ -38,71 +31,5 @@ func Test_init_with_empty_tags(t *testing.T) {
3831
},
3932
}
4033

41-
for name, tc := range cases {
42-
tc := tc // rebind tc into this lexical scope
43-
t.Run(name, func(subtest *testing.T) {
44-
subtest.Parallel()
45-
organization, cleanup := createOrganization(t)
46-
defer cleanup()
47-
exp, err := expect.NewConsole(defaultOpts()...)
48-
if err != nil {
49-
subtest.Fatal(err)
50-
}
51-
defer exp.Close()
52-
53-
tmpDir, err := ioutil.TempDir("", "terraform-test")
54-
if err != nil {
55-
subtest.Fatal(err)
56-
}
57-
defer os.RemoveAll(tmpDir)
58-
59-
tf := e2e.NewBinary(terraformBin, tmpDir)
60-
tf.AddEnv(cliConfigFileEnv)
61-
defer tf.Close()
62-
63-
for _, op := range tc.operations {
64-
op.prep(t, organization.Name, tf.WorkDir())
65-
for _, tfCmd := range op.commands {
66-
cmd := tf.Cmd(tfCmd.command...)
67-
cmd.Stdin = exp.Tty()
68-
cmd.Stdout = exp.Tty()
69-
cmd.Stderr = exp.Tty()
70-
71-
err = cmd.Start()
72-
if err != nil {
73-
subtest.Fatal(err)
74-
}
75-
76-
if tfCmd.expectedCmdOutput != "" {
77-
got, err := exp.ExpectString(tfCmd.expectedCmdOutput)
78-
if err != nil {
79-
subtest.Fatalf("error while waiting for output\nwant: %s\nerror: %s\noutput\n%s", tfCmd.expectedCmdOutput, err, got)
80-
}
81-
}
82-
83-
lenInput := len(tfCmd.userInput)
84-
lenInputOutput := len(tfCmd.postInputOutput)
85-
if lenInput > 0 {
86-
for i := 0; i < lenInput; i++ {
87-
input := tfCmd.userInput[i]
88-
exp.SendLine(input)
89-
// use the index to find the corresponding
90-
// output that matches the input.
91-
if lenInputOutput-1 >= i {
92-
output := tfCmd.postInputOutput[i]
93-
_, err := exp.ExpectString(output)
94-
if err != nil {
95-
subtest.Fatal(err)
96-
}
97-
}
98-
}
99-
}
100-
err = cmd.Wait()
101-
if err != nil && !tfCmd.expectError {
102-
subtest.Fatal(err)
103-
}
104-
}
105-
}
106-
})
107-
}
34+
testRunner(t, cases, 1)
10835
}

0 commit comments

Comments
 (0)