Skip to content

Commit 49a1bf3

Browse files
committed
simplify e2e tests
1 parent e075ed1 commit 49a1bf3

File tree

3 files changed

+107
-127
lines changed

3 files changed

+107
-127
lines changed

test/e2e/adv2new_test.go

Lines changed: 2 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,11 @@
11
package e2e_test
22

33
import (
4-
"os"
54
"testing"
65

76
"github.com/mongodb-labs/atlas-cli-plugin-terraform/test/e2e"
8-
"github.com/spf13/afero"
9-
"github.com/stretchr/testify/assert"
10-
"github.com/stretchr/testify/require"
117
)
128

13-
func TestAdv2NewParams(t *testing.T) {
14-
cwd, err := os.Getwd()
15-
require.NoError(t, err)
16-
var (
17-
prefix = cwd + "/testdata/"
18-
fileIn = prefix + "adv2new.in.tf"
19-
fileOut = prefix + "adv2new.out.tf"
20-
fileExpected = prefix + "adv2new.expected.tf"
21-
fileUnexisting = prefix + "adv2new.unexisting.tf"
22-
fs = afero.NewOsFs()
23-
)
24-
tests := map[string]struct {
25-
expectedErrContains string
26-
assertFunc func(t *testing.T)
27-
args []string
28-
}{
29-
"no params": {
30-
expectedErrContains: "required flag(s) \"file\", \"output\" not set",
31-
},
32-
"no input file": {
33-
args: []string{"--output", fileOut},
34-
expectedErrContains: "required flag(s) \"file\" not set",
35-
},
36-
"no output file": {
37-
args: []string{"--file", fileIn},
38-
expectedErrContains: "required flag(s) \"output\" not set",
39-
},
40-
"unexisting input file": {
41-
args: []string{"--file", fileUnexisting, "--output", fileOut},
42-
expectedErrContains: "file must exist: " + fileUnexisting,
43-
},
44-
"existing output file without replaceOutput flag": {
45-
args: []string{"--file", fileIn, "--output", fileExpected},
46-
expectedErrContains: "file must not exist: " + fileExpected,
47-
},
48-
"basic use": {
49-
args: []string{"--file", fileIn, "--output", fileOut},
50-
assertFunc: func(t *testing.T) { t.Helper(); e2e.CompareFiles(t, fs, fileOut, fileExpected) },
51-
},
52-
}
53-
for name, tc := range tests {
54-
t.Run(name, func(t *testing.T) {
55-
resp, err := e2e.RunAdv2New(tc.args...)
56-
assert.Equal(t, tc.expectedErrContains == "", err == nil)
57-
if err == nil {
58-
assert.Empty(t, resp)
59-
if tc.assertFunc != nil {
60-
tc.assertFunc(t)
61-
}
62-
} else {
63-
assert.Contains(t, resp, tc.expectedErrContains)
64-
}
65-
_ = fs.Remove(fileOut) // Ensure the output file does not exist in case it was generated in some test case
66-
})
67-
}
9+
func TestAdv2New(t *testing.T) {
10+
e2e.RunTests(t, "adv2new", nil)
6811
}

test/e2e/clu2adv_test.go

Lines changed: 7 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,19 @@
11
package e2e_test
22

33
import (
4-
"os"
54
"testing"
65

76
"github.com/mongodb-labs/atlas-cli-plugin-terraform/test/e2e"
8-
"github.com/spf13/afero"
9-
"github.com/stretchr/testify/assert"
10-
"github.com/stretchr/testify/require"
117
)
128

13-
func TestClu2AdvParams(t *testing.T) {
14-
cwd, err := os.Getwd()
15-
require.NoError(t, err)
16-
var (
17-
prefix = cwd + "/testdata/"
18-
fileIn = prefix + "clu2adv.in.tf"
19-
fileOut = prefix + "clu2adv.out.tf"
20-
fileExpected = prefix + "clu2adv.expected.tf"
21-
fileExpectedMoved = prefix + "clu2adv.expected_moved.tf"
22-
fileUnexisting = prefix + "clu2adv.unexisting.tf"
23-
fs = afero.NewOsFs()
24-
)
25-
tests := map[string]struct {
26-
expectedErrContains string
27-
assertFunc func(t *testing.T)
28-
args []string
29-
}{
30-
"no params": {
31-
expectedErrContains: "required flag(s) \"file\", \"output\" not set",
32-
},
33-
"no input file": {
34-
args: []string{"--output", fileOut},
35-
expectedErrContains: "required flag(s) \"file\" not set",
36-
},
37-
"no output file": {
38-
args: []string{"--file", fileIn},
39-
expectedErrContains: "required flag(s) \"output\" not set",
40-
},
41-
"unexisting input file": {
42-
args: []string{"--file", fileUnexisting, "--output", fileOut},
43-
expectedErrContains: "file must exist: " + fileUnexisting,
44-
},
45-
"existing output file without replaceOutput flag": {
46-
args: []string{"--file", fileIn, "--output", fileExpected},
47-
expectedErrContains: "file must not exist: " + fileExpected,
48-
},
49-
"basic use": {
50-
args: []string{"--file", fileIn, "--output", fileOut},
51-
assertFunc: func(t *testing.T) { t.Helper(); e2e.CompareFiles(t, fs, fileOut, fileExpected) },
52-
},
9+
func TestClu2Adv(t *testing.T) {
10+
files := e2e.GetTestFiles(t, "clu2adv")
11+
fileExpectedMoved := files.GetCustomFilePath("expected_moved.tf")
12+
extraTests := map[string]e2e.TestCase{
5313
"include moved": {
54-
args: []string{"--file", fileIn, "--output", fileOut, "--includeMoved"},
55-
assertFunc: func(t *testing.T) { t.Helper(); e2e.CompareFiles(t, fs, fileOut, fileExpectedMoved) },
14+
Args: []string{"--file", files.FileIn, "--output", files.FileOut, "--includeMoved"},
15+
Assert: func(t *testing.T) { t.Helper(); e2e.CompareFiles(t, files.Fs, files.FileOut, fileExpectedMoved) },
5616
},
5717
}
58-
for name, tc := range tests {
59-
t.Run(name, func(t *testing.T) {
60-
resp, err := e2e.RunClu2Adv(tc.args...)
61-
assert.Equal(t, tc.expectedErrContains == "", err == nil)
62-
if err == nil {
63-
assert.Empty(t, resp)
64-
if tc.assertFunc != nil {
65-
tc.assertFunc(t)
66-
}
67-
} else {
68-
assert.Contains(t, resp, tc.expectedErrContains)
69-
}
70-
_ = fs.Remove(fileOut) // Ensure the output file does not exist in case it was generated in some test case
71-
})
72-
}
18+
e2e.RunTests(t, "clu2adv", extraTests)
7319
}

test/e2e/e2e_helper.go

Lines changed: 98 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package e2e
22

33
import (
44
"context"
5+
"os"
56
"os/exec"
67
"testing"
78

@@ -17,13 +18,8 @@ func RunTF(args ...string) (string, error) {
1718
return string(resp), err
1819
}
1920

20-
func RunClu2Adv(args ...string) (string, error) {
21-
args = append([]string{"clu2adv"}, args...)
22-
return RunTF(args...)
23-
}
24-
25-
func RunAdv2New(args ...string) (string, error) {
26-
args = append([]string{"adv2new"}, args...)
21+
func RunTFCommand(command string, args ...string) (string, error) {
22+
args = append([]string{command}, args...)
2723
return RunTF(args...)
2824
}
2925

@@ -35,3 +31,98 @@ func CompareFiles(t *testing.T, fs afero.Fs, file1, file2 string) {
3531
require.NoError(t, err2)
3632
assert.Equal(t, string(data1), string(data2))
3733
}
34+
35+
type TestFiles struct {
36+
Fs afero.Fs
37+
Prefix string
38+
FileIn string
39+
FileOut string
40+
FileExpected string
41+
FileUnexisting string
42+
CmdName string
43+
}
44+
45+
// GetTestFiles creates a TestFiles struct with standard file paths for the given command name
46+
func GetTestFiles(t *testing.T, cmdName string) *TestFiles {
47+
t.Helper()
48+
cwd, err := os.Getwd()
49+
require.NoError(t, err)
50+
51+
prefix := cwd + "/testdata/"
52+
files := &TestFiles{
53+
Fs: afero.NewOsFs(),
54+
CmdName: cmdName,
55+
Prefix: prefix,
56+
}
57+
files.FileIn = files.GetCustomFilePath("in.tf")
58+
files.FileOut = files.GetCustomFilePath("out.tf")
59+
files.FileExpected = files.GetCustomFilePath("expected.tf")
60+
files.FileUnexisting = files.GetCustomFilePath("unexisting.tf")
61+
return files
62+
}
63+
64+
// GetCustomFilePath returns a file path using the testdata prefix
65+
func (tf *TestFiles) GetCustomFilePath(suffix string) string {
66+
return tf.Prefix + tf.CmdName + "." + suffix
67+
}
68+
69+
type TestCase struct {
70+
ExpectedErrContains string
71+
Assert func(t *testing.T)
72+
Args []string
73+
}
74+
75+
// RunTests runs common parameter validation tests for both commands. Specific tests can be provided in extraTests.
76+
func RunTests(t *testing.T, cmdName string, extraTests map[string]TestCase) {
77+
t.Helper()
78+
files := GetTestFiles(t, cmdName)
79+
commonTests := map[string]TestCase{
80+
"no params": {
81+
ExpectedErrContains: "required flag(s) \"file\", \"output\" not set",
82+
},
83+
"no input file": {
84+
Args: []string{"--output", files.FileOut},
85+
ExpectedErrContains: "required flag(s) \"file\" not set",
86+
},
87+
"no output file": {
88+
Args: []string{"--file", files.FileIn},
89+
ExpectedErrContains: "required flag(s) \"output\" not set",
90+
},
91+
"unexisting input file": {
92+
Args: []string{"--file", files.FileUnexisting, "--output", files.FileOut},
93+
ExpectedErrContains: "file must exist: " + files.FileUnexisting,
94+
},
95+
"existing output file without replaceOutput flag": {
96+
Args: []string{"--file", files.FileIn, "--output", files.FileExpected},
97+
ExpectedErrContains: "file must not exist: " + files.FileExpected,
98+
},
99+
"basic use": {
100+
Args: []string{"--file", files.FileIn, "--output", files.FileOut},
101+
Assert: func(t *testing.T) { t.Helper(); CompareFiles(t, files.Fs, files.FileOut, files.FileExpected) },
102+
},
103+
}
104+
105+
allTests := make(map[string]TestCase)
106+
for name, test := range commonTests {
107+
allTests[name] = test
108+
}
109+
for name, test := range extraTests {
110+
allTests[name] = test
111+
}
112+
113+
for name, tc := range allTests {
114+
t.Run(name, func(t *testing.T) {
115+
resp, err := RunTFCommand(cmdName, tc.Args...)
116+
assert.Equal(t, tc.ExpectedErrContains == "", err == nil)
117+
if err == nil {
118+
assert.Empty(t, resp)
119+
if tc.Assert != nil {
120+
tc.Assert(t)
121+
}
122+
} else {
123+
assert.Contains(t, resp, tc.ExpectedErrContains)
124+
}
125+
_ = files.Fs.Remove(files.FileOut) // Ensure output file does not exist in case it was generated in some test case
126+
})
127+
}
128+
}

0 commit comments

Comments
 (0)