Skip to content

Commit 06ac661

Browse files
author
Ravi Sankar Penta
committed
OVS test: Validate stdin values to bundle() call
1 parent 771595d commit 06ac661

File tree

1 file changed

+36
-6
lines changed

1 file changed

+36
-6
lines changed

pkg/util/ovs/ovs_test.go

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

33
import (
44
"fmt"
5+
"io/ioutil"
56
"strings"
67
"testing"
78

@@ -29,8 +30,8 @@ func missingSetup() *fakeexec.FakeExec {
2930
}
3031
}
3132

32-
func addTestResult(t *testing.T, fexec *fakeexec.FakeExec, command string, output string, err error) {
33-
fcmd := fakeexec.FakeCmd{
33+
func addTestResult(t *testing.T, fexec *fakeexec.FakeExec, command string, output string, err error) *fakeexec.FakeCmd {
34+
fcmd := &fakeexec.FakeCmd{
3435
CombinedOutputScript: []fakeexec.FakeCombinedOutputAction{
3536
func() ([]byte, error) { return []byte(output), err },
3637
},
@@ -41,8 +42,10 @@ func addTestResult(t *testing.T, fexec *fakeexec.FakeExec, command string, outpu
4142
if execCommand != command {
4243
t.Fatalf("Unexpected command: wanted %q got %q", command, execCommand)
4344
}
44-
return fakeexec.InitFakeCmd(&fcmd, cmd, args...)
45+
return fakeexec.InitFakeCmd(fcmd, cmd, args...)
4546
})
47+
48+
return fcmd
4649
}
4750

4851
func ensureTestResults(t *testing.T, fexec *fakeexec.FakeExec) {
@@ -51,6 +54,22 @@ func ensureTestResults(t *testing.T, fexec *fakeexec.FakeExec) {
5154
}
5255
}
5356

57+
func ensureInputFlows(t *testing.T, fakeCmd *fakeexec.FakeCmd, flows []string) {
58+
allFlows := strings.Join(flows, "\n")
59+
60+
var fakeCmdFlows string
61+
if fakeCmd != nil {
62+
data, err := ioutil.ReadAll(fakeCmd.Stdin)
63+
if err != nil {
64+
t.Fatalf(err.Error())
65+
}
66+
fakeCmdFlows = string(data)
67+
}
68+
if strings.Compare(allFlows, fakeCmdFlows) != 0 {
69+
t.Fatalf("Expected input flows: %q but got %q", allFlows, fakeCmdFlows)
70+
}
71+
}
72+
5473
func TestTransactionSuccess(t *testing.T) {
5574
fexec := normalSetup()
5675

@@ -65,16 +84,22 @@ func TestTransactionSuccess(t *testing.T) {
6584
t.Fatalf("Unexpected error from command: %v", err)
6685
}
6786
ensureTestResults(t, fexec)
87+
ensureInputFlows(t, nil, []string{})
6888

6989
// Test Successful transaction
70-
addTestResult(t, fexec, "ovs-ofctl -O OpenFlow13 bundle br0 -", "", nil)
90+
fakeCmd := addTestResult(t, fexec, "ovs-ofctl -O OpenFlow13 bundle br0 -", "", nil)
7191
otx = ovsif.NewTransaction()
7292
otx.AddFlow("flow1")
7393
otx.AddFlow("flow2")
7494
if err = otx.Commit(); err != nil {
7595
t.Fatalf("Unexpected error from command: %v", err)
7696
}
7797
ensureTestResults(t, fexec)
98+
expectedInputFlows := []string{
99+
"flow add flow1",
100+
"flow add flow2",
101+
}
102+
ensureInputFlows(t, fakeCmd, expectedInputFlows)
78103

79104
// Test reuse transaction object
80105
if err = otx.Commit(); err != nil {
@@ -83,14 +108,19 @@ func TestTransactionSuccess(t *testing.T) {
83108
ensureTestResults(t, fexec)
84109

85110
// Test Failed transaction
86-
addTestResult(t, fexec, "ovs-ofctl -O OpenFlow13 bundle br0 -", "", fmt.Errorf("Something bad happened"))
111+
fakeCmd = addTestResult(t, fexec, "ovs-ofctl -O OpenFlow13 bundle br0 -", "", fmt.Errorf("Something bad happened"))
87112
otx = ovsif.NewTransaction()
88113
otx.AddFlow("flow1")
89-
otx.AddFlow("flow2")
114+
otx.DeleteFlows("flow2")
90115
if err = otx.Commit(); err == nil {
91116
t.Fatalf("Failed to get expected error")
92117
}
93118
ensureTestResults(t, fexec)
119+
expectedInputFlows = []string{
120+
"flow add flow1",
121+
"flow delete flow2",
122+
}
123+
ensureInputFlows(t, fakeCmd, expectedInputFlows)
94124
}
95125

96126
func TestDumpFlows(t *testing.T) {

0 commit comments

Comments
 (0)