Skip to content

Commit 72c98d7

Browse files
authored
Issue #74: Table-driven testing (#167)
1 parent 7e44df5 commit 72c98d7

File tree

1 file changed

+158
-117
lines changed

1 file changed

+158
-117
lines changed

cmd/nettop/main_test.go

+158-117
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main
22

33
import (
44
"bufio"
5-
"errors"
65
"fmt"
76
"os"
87
"path/filepath"
@@ -12,138 +11,177 @@ import (
1211
"github.com/stretchr/testify/require"
1312
)
1413

15-
func TestConnectionsOutput(t *testing.T) {
16-
testsDir := getTestsDir()
17-
dirPath := filepath.Join(testsDir, "onlineboutique", "kubernetes-manifests.yaml")
18-
outFile := filepath.Join(testsDir, "onlineboutique", "output.json")
19-
expectedOutput := filepath.Join(testsDir, "onlineboutique", "expected_output.json")
20-
args := getTestArgs(dirPath, outFile, JSONFormat, false, false, false)
21-
22-
err := detectTopology(args)
23-
require.Nil(t, err)
24-
25-
res, err := compareFiles(expectedOutput, outFile)
26-
require.Nil(t, err)
27-
require.True(t, res)
28-
29-
os.Remove(outFile)
14+
type TestDetails struct {
15+
name string
16+
dirPath [][]string
17+
outputFormat string
18+
synthNetpols bool
19+
quiet bool
20+
verbose bool
21+
expectError bool
22+
expectedOutput []string
3023
}
3124

32-
func TestConnectionsYamlOutput(t *testing.T) {
33-
testsDir := getTestsDir()
34-
dirPath := filepath.Join(testsDir, "onlineboutique", "kubernetes-manifests.yaml")
35-
outFile := filepath.Join(testsDir, "onlineboutique", "output.yaml")
36-
expectedOutput := filepath.Join(testsDir, "onlineboutique", "expected_output.yaml")
37-
args := getTestArgs(dirPath, outFile, YamlFormat, false, false, false)
38-
39-
err := detectTopology(args)
40-
require.Nil(t, err)
41-
42-
res, err := compareFiles(expectedOutput, outFile)
43-
require.Nil(t, err)
44-
require.True(t, res)
45-
46-
os.Remove(outFile)
47-
}
25+
var (
26+
testCaseScenarios = []TestDetails{
27+
{
28+
"ConnectionsOutputJSON",
29+
[][]string{{"onlineboutique", "kubernetes-manifests.yaml"}},
30+
JSONFormat,
31+
false,
32+
false,
33+
false,
34+
false,
35+
[]string{"onlineboutique", "expected_output.json"},
36+
},
37+
{
38+
"ConnectionsOutputYAML",
39+
[][]string{{"onlineboutique", "kubernetes-manifests.yaml"}},
40+
YamlFormat,
41+
false,
42+
false,
43+
false,
44+
false,
45+
[]string{"onlineboutique", "expected_output.yaml"},
46+
},
47+
{
48+
"DirScan",
49+
[][]string{{"onlineboutique"}},
50+
JSONFormat,
51+
false,
52+
true,
53+
false,
54+
false,
55+
[]string{"onlineboutique", "expected_dirscan_output.json"},
56+
},
57+
{
58+
"NetpolsOnlineBoutiqueYAML",
59+
[][]string{{"onlineboutique", "kubernetes-manifests.yaml"}},
60+
YamlFormat,
61+
true,
62+
false,
63+
false,
64+
false,
65+
[]string{"onlineboutique", "expected_netpol_output.yaml"},
66+
},
67+
{
68+
"NetpolsMultiplePaths",
69+
[][]string{{"k8s_wordpress_example", "mysql-deployment.yaml"}, {"k8s_wordpress_example", "wordpress-deployment.yaml"}},
70+
JSONFormat,
71+
true,
72+
false,
73+
false,
74+
false,
75+
[]string{"k8s_wordpress_example", "expected_netpol_output.json"},
76+
},
77+
{
78+
"NetpolsOnlineBoutiqueJson",
79+
[][]string{{"onlineboutique", "kubernetes-manifests.yaml"}},
80+
JSONFormat,
81+
true,
82+
false,
83+
false,
84+
false,
85+
[]string{"onlineboutique", "expected_netpol_output.json"},
86+
},
87+
{
88+
"NetpolsSockshop",
89+
[][]string{{"sockshop", "manifests"}},
90+
JSONFormat,
91+
true,
92+
false,
93+
true,
94+
false,
95+
[]string{"sockshop", "expected_netpol_output.json"},
96+
},
97+
{
98+
"NetpolsK8sWordpress",
99+
[][]string{{"k8s_wordpress_example"}},
100+
JSONFormat,
101+
true,
102+
false,
103+
true,
104+
false,
105+
[]string{"k8s_wordpress_example", "expected_netpol_output.json"},
106+
},
107+
{
108+
"NetpolsK8sGuestbook",
109+
[][]string{{"k8s_guestbook"}},
110+
JSONFormat,
111+
true,
112+
false,
113+
true,
114+
false,
115+
[]string{"k8s_guestbook", "expected_netpol_output.json"},
116+
},
117+
{
118+
"NetpolsBookInfo",
119+
[][]string{{"bookinfo"}},
120+
JSONFormat,
121+
true,
122+
false,
123+
true,
124+
false,
125+
[]string{"bookinfo", "expected_netpol_output.json"},
126+
},
127+
}
48128

49-
func TestDirScan(t *testing.T) {
50-
testsDir := getTestsDir()
51-
dirPath := filepath.Join(testsDir, "onlineboutique")
52-
outFile := filepath.Join(dirPath, "output.json")
53-
expectedOutput := filepath.Join(dirPath, "expected_dirscan_output.json")
54-
args := getTestArgs(dirPath, outFile, JSONFormat, false, true, false)
129+
currentDir, _ = os.Getwd()
130+
testsDir = filepath.Join(currentDir, "..", "..", "tests")
131+
)
55132

56-
err := detectTopology(args)
133+
func (td *TestDetails) runTest(t *testing.T) {
134+
t.Logf("Running test %s", td.name)
135+
outFileName, err := getTempOutputFile()
57136
require.Nil(t, err)
58137

59-
res, err := compareFiles(expectedOutput, outFile)
60-
require.Nil(t, err)
61-
require.True(t, res)
138+
args := getTestArgs(td.dirPath, outFileName, td.outputFormat, td.synthNetpols, td.quiet, td.verbose)
139+
err = detectTopology(args)
62140

63-
os.Remove(outFile)
64-
}
141+
if td.expectError {
142+
require.NotNil(t, err)
143+
} else {
144+
require.Nil(t, err)
145+
if td.expectedOutput != nil {
146+
res, err := compareFiles(pathInTestsDir(td.expectedOutput), outFileName)
147+
require.Nil(t, err)
148+
require.True(t, res)
65149

66-
type TestDetails struct {
67-
dirPath string
68-
outFile string
69-
expectedOutput string
70-
}
71-
72-
func TestNetpolsJsonOutput(t *testing.T) {
73-
testsDir := getTestsDir()
74-
tests := map[string]TestDetails{} // map from test name to test details
75-
tests["onlineboutique"] = TestDetails{dirPath: filepath.Join(testsDir, "onlineboutique", "kubernetes-manifests.yaml"),
76-
outFile: filepath.Join(testsDir, "onlineboutique", "output.json"),
77-
expectedOutput: filepath.Join(testsDir, "onlineboutique", "expected_netpol_output.json")}
78-
tests["sockshop"] = TestDetails{dirPath: filepath.Join(testsDir, "sockshop", "manifests"),
79-
outFile: filepath.Join(testsDir, "sockshop", "output.json"),
80-
expectedOutput: filepath.Join(testsDir, "sockshop", "expected_netpol_output.json")}
81-
tests["wordpress"] = TestDetails{dirPath: filepath.Join(testsDir, "k8s_wordpress_example"),
82-
outFile: filepath.Join(testsDir, "k8s_wordpress_example", "output.json"),
83-
expectedOutput: filepath.Join(testsDir, "k8s_wordpress_example", "expected_netpol_output.json")}
84-
tests["guestbook"] = TestDetails{dirPath: filepath.Join(testsDir, "k8s_guestbook"),
85-
outFile: filepath.Join(testsDir, "k8s_guestbook", "output.json"),
86-
expectedOutput: filepath.Join(testsDir, "k8s_guestbook", "expected_netpol_output.json")}
87-
tests["bookinfo"] = TestDetails{dirPath: filepath.Join(testsDir, "bookinfo"),
88-
outFile: filepath.Join(testsDir, "bookinfo", "output.json"),
89-
expectedOutput: filepath.Join(testsDir, "bookinfo", "expected_netpol_output.json")}
90-
91-
for testName, testDetails := range tests {
92-
args := getTestArgs(testDetails.dirPath, testDetails.outFile, JSONFormat, true, false, true)
93-
err := detectTopology(args)
94-
require.Nilf(t, err, "on test %s", testName)
95-
96-
res, err := compareFiles(testDetails.expectedOutput, testDetails.outFile)
97-
require.Nilf(t, err, "on test %s", testName)
98-
require.Truef(t, res, "on test %s", testName)
99-
os.Remove(testDetails.outFile)
150+
os.Remove(outFileName)
151+
}
100152
}
101153
}
102154

103-
func TestNetpolsYamlOutput(t *testing.T) {
104-
testsDir := getTestsDir()
105-
dirPath := filepath.Join(testsDir, "onlineboutique", "kubernetes-manifests.yaml")
106-
outFile := filepath.Join(testsDir, "onlineboutique", "output.yaml")
107-
expectedOutput := filepath.Join(testsDir, "onlineboutique", "expected_netpol_output.yaml")
108-
args := getTestArgs(dirPath, outFile, YamlFormat, true, false, false)
109-
110-
err := detectTopology(args)
111-
require.Nil(t, err)
112-
113-
res, err := compareFiles(expectedOutput, outFile)
114-
require.Nil(t, err)
115-
require.True(t, res)
116-
117-
os.Remove(outFile)
155+
func TestAll(t *testing.T) {
156+
for testIdx := range testCaseScenarios {
157+
tc := &testCaseScenarios[testIdx] // rebind tc into this lexical scope to support reentrancy
158+
t.Run(tc.name, func(t *testing.T) {
159+
t.Parallel()
160+
tc.runTest(t)
161+
})
162+
}
118163
}
119164

120-
func TestNetpolsMultiplePaths(t *testing.T) {
121-
testsDir := getTestsDir()
122-
dirPath1 := filepath.Join(testsDir, "k8s_wordpress_example", "mysql-deployment.yaml")
123-
dirPath2 := filepath.Join(testsDir, "k8s_wordpress_example", "wordpress-deployment.yaml")
124-
outFile := filepath.Join(testsDir, "k8s_wordpress_example", "netpols.yaml")
125-
expectedOutput := filepath.Join(testsDir, "k8s_wordpress_example", "expected_netpol_output.json")
126-
args := getTestArgs(dirPath1, outFile, JSONFormat, true, false, false)
127-
args.DirPaths = append(args.DirPaths, dirPath2)
128-
129-
err := detectTopology(args)
130-
require.Nil(t, err)
131-
132-
res, err := compareFiles(expectedOutput, outFile)
133-
require.Nil(t, err)
134-
require.True(t, res)
135-
136-
os.Remove(outFile)
165+
func getTempOutputFile() (string, error) {
166+
outFile, err := os.CreateTemp(os.TempDir(), "cta_temp")
167+
if err != nil {
168+
return "", err
169+
}
170+
outFileName := outFile.Name()
171+
err = outFile.Close()
172+
return outFileName, err
137173
}
138174

139-
func getTestsDir() string {
140-
currentDir, _ := os.Getwd()
141-
return filepath.Join(currentDir, "..", "..", "tests")
175+
func pathInTestsDir(pathElements []string) string {
176+
return filepath.Join(testsDir, filepath.Join(pathElements...))
142177
}
143178

144-
func getTestArgs(dirPath, outFile, outFormat string, netpols, quiet, verbose bool) InArgs {
179+
func getTestArgs(dirPaths [][]string, outFile, outFormat string, netpols, quiet, verbose bool) InArgs {
145180
args := InArgs{}
146-
args.DirPaths = []string{dirPath}
181+
args.DirPaths = []string{}
182+
for idx := range dirPaths {
183+
args.DirPaths = append(args.DirPaths, pathInTestsDir(dirPaths[idx]))
184+
}
147185
args.OutputFile = &outFile
148186
args.OutputFormat = &outFormat
149187
args.SynthNetpols = &netpols
@@ -169,9 +207,12 @@ func readLines(path string) ([]string, error) {
169207

170208
func compareFiles(expectedFile, actualFile string) (bool, error) {
171209
expectedLines, err1 := readLines(expectedFile)
210+
if err1 != nil {
211+
return false, fmt.Errorf("error reading lines from file %v", err1)
212+
}
172213
actualLines, err2 := readLines(actualFile)
173-
if err1 != nil || err2 != nil {
174-
return false, errors.New("error reading lines from file")
214+
if err2 != nil {
215+
return false, fmt.Errorf("error reading lines from file %v", err2)
175216
}
176217
if len(expectedLines) != len(actualLines) {
177218
fmt.Printf("Files line count is different: expected(%s): %d, actual(%s): %d",

0 commit comments

Comments
 (0)