@@ -2,6 +2,7 @@ package ovs
2
2
3
3
import (
4
4
"fmt"
5
+ "io/ioutil"
5
6
"strings"
6
7
"testing"
7
8
@@ -29,8 +30,8 @@ func missingSetup() *fakeexec.FakeExec {
29
30
}
30
31
}
31
32
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 {
34
35
CombinedOutputScript : []fakeexec.FakeCombinedOutputAction {
35
36
func () ([]byte , error ) { return []byte (output ), err },
36
37
},
@@ -41,8 +42,10 @@ func addTestResult(t *testing.T, fexec *fakeexec.FakeExec, command string, outpu
41
42
if execCommand != command {
42
43
t .Fatalf ("Unexpected command: wanted %q got %q" , command , execCommand )
43
44
}
44
- return fakeexec .InitFakeCmd (& fcmd , cmd , args ... )
45
+ return fakeexec .InitFakeCmd (fcmd , cmd , args ... )
45
46
})
47
+
48
+ return fcmd
46
49
}
47
50
48
51
func ensureTestResults (t * testing.T , fexec * fakeexec.FakeExec ) {
@@ -51,6 +54,22 @@ func ensureTestResults(t *testing.T, fexec *fakeexec.FakeExec) {
51
54
}
52
55
}
53
56
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
+
54
73
func TestTransactionSuccess (t * testing.T ) {
55
74
fexec := normalSetup ()
56
75
@@ -65,16 +84,22 @@ func TestTransactionSuccess(t *testing.T) {
65
84
t .Fatalf ("Unexpected error from command: %v" , err )
66
85
}
67
86
ensureTestResults (t , fexec )
87
+ ensureInputFlows (t , nil , []string {})
68
88
69
89
// 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 )
71
91
otx = ovsif .NewTransaction ()
72
92
otx .AddFlow ("flow1" )
73
93
otx .AddFlow ("flow2" )
74
94
if err = otx .Commit (); err != nil {
75
95
t .Fatalf ("Unexpected error from command: %v" , err )
76
96
}
77
97
ensureTestResults (t , fexec )
98
+ expectedInputFlows := []string {
99
+ "flow add flow1" ,
100
+ "flow add flow2" ,
101
+ }
102
+ ensureInputFlows (t , fakeCmd , expectedInputFlows )
78
103
79
104
// Test reuse transaction object
80
105
if err = otx .Commit (); err != nil {
@@ -83,14 +108,19 @@ func TestTransactionSuccess(t *testing.T) {
83
108
ensureTestResults (t , fexec )
84
109
85
110
// 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" ))
87
112
otx = ovsif .NewTransaction ()
88
113
otx .AddFlow ("flow1" )
89
- otx .AddFlow ("flow2" )
114
+ otx .DeleteFlows ("flow2" )
90
115
if err = otx .Commit (); err == nil {
91
116
t .Fatalf ("Failed to get expected error" )
92
117
}
93
118
ensureTestResults (t , fexec )
119
+ expectedInputFlows = []string {
120
+ "flow add flow1" ,
121
+ "flow delete flow2" ,
122
+ }
123
+ ensureInputFlows (t , fakeCmd , expectedInputFlows )
94
124
}
95
125
96
126
func TestDumpFlows (t * testing.T ) {
0 commit comments