@@ -9,13 +9,11 @@ import (
9
9
func TestFakePorts (t * testing.T ) {
10
10
ovsif := NewFake ("br0" )
11
11
12
- _ , err := ovsif .AddPort ("tun0" , 1 )
13
- if err == nil {
12
+ if _ , err := ovsif .AddPort ("tun0" , 1 ); err == nil {
14
13
t .Fatalf ("unexpected lack of error adding port on non-existent bridge" )
15
14
}
16
15
17
- err = ovsif .AddBridge ()
18
- if err != nil {
16
+ if err := ovsif .AddBridge (); err != nil {
19
17
t .Fatalf ("unexpected error adding bridge: %v" , err )
20
18
}
21
19
ofport , err := ovsif .AddPort ("tun0" , 17 )
@@ -29,20 +27,105 @@ func TestFakePorts(t *testing.T) {
29
27
if ofport != 17 {
30
28
t .Fatalf ("unexpected ofport %d returned from GetOFPort" , ofport )
31
29
}
32
- err = ovsif .DeletePort ("tun0" )
33
- if err != nil {
30
+ if err = ovsif .DeletePort ("tun0" ); err != nil {
34
31
t .Fatalf ("unexpected error deleting port: %v" , err )
35
32
}
36
- _ , err = ovsif .GetOFPort ("tun0" )
37
- if err == nil {
33
+ if _ , err = ovsif .GetOFPort ("tun0" ); err == nil {
38
34
t .Fatalf ("unexpected lack of error getting non-existent port" )
39
35
}
40
36
}
41
37
38
+ func TestTransaction (t * testing.T ) {
39
+ ovsif := NewFake ("br0" )
40
+ if err := ovsif .AddBridge (); err != nil {
41
+ t .Fatalf ("unexpected error adding bridge: %v" , err )
42
+ }
43
+
44
+ // Empty transaction
45
+ otx := ovsif .NewTransaction ()
46
+ if err := otx .Commit (); err != nil {
47
+ t .Fatalf ("unexpected error: %v" , err )
48
+ }
49
+ if err := checkDump (ovsif , "" , []string {}); err != nil {
50
+ t .Fatalf (err .Error ())
51
+ }
52
+
53
+ // Add flows transaction
54
+ otx .AddFlow ("table=100, priority=100, reg0=1, actions=one" )
55
+ otx .AddFlow ("table=100, priority=200, reg0=2, cookie=1, actions=two" )
56
+ if err := otx .Commit (); err != nil {
57
+ t .Fatalf ("unexpected error: %v" , err )
58
+ }
59
+ expectedFlows := []string {
60
+ " cookie=1, table=100, priority=200, reg0=2, actions=two" ,
61
+ " cookie=0, table=100, priority=100, reg0=1, actions=one" ,
62
+ }
63
+ if err := checkDump (ovsif , "" , expectedFlows ); err != nil {
64
+ t .Fatalf (err .Error ())
65
+ }
66
+
67
+ // Add flows failed transaction, invalid action
68
+ otx .AddFlow ("table=100, priority=300, reg0=3, actions=three" )
69
+ otx .AddFlow ("table=100, priority=400, reg0=2, actions" )
70
+ if err := otx .Commit (); err == nil {
71
+ t .Fatalf ("expected no error but got %v" , err )
72
+ }
73
+ if err := checkDump (ovsif , "" , expectedFlows ); err != nil {
74
+ t .Fatalf (err .Error ())
75
+ }
76
+
77
+ // Delete flows transaction
78
+ otx .DeleteFlows ("table=100, reg0=1" )
79
+ if err := otx .Commit (); err != nil {
80
+ t .Fatalf ("unexpected error: %v" , err )
81
+ }
82
+ expectedFlows = []string {
83
+ " cookie=1, table=100, priority=200, reg0=2, actions=two" ,
84
+ }
85
+ if err := checkDump (ovsif , "" , expectedFlows ); err != nil {
86
+ t .Fatalf (err .Error ())
87
+ }
88
+
89
+ // Delete flows failed transaction, invalid cookie(missing mask)
90
+ otx .DeleteFlows ("table=100, cookie=1" )
91
+ if err := otx .Commit (); err == nil {
92
+ t .Fatalf ("expected no error but got %v" , err )
93
+ }
94
+ if err := checkDump (ovsif , "" , expectedFlows ); err != nil {
95
+ t .Fatalf (err .Error ())
96
+ }
97
+
98
+ // Add and Delete flows transaction
99
+ otx .AddFlow ("table=100, priority=300, reg0=3, actions=three" )
100
+ otx .AddFlow ("table=101, priority=100, reg0=1, actions=one" )
101
+ otx .DeleteFlows ("table=100" )
102
+ otx .AddFlow ("table=101, priority=200, reg0=2, actions=two" )
103
+ if err := otx .Commit (); err != nil {
104
+ t .Fatalf ("unexpected error: %v" , err )
105
+ }
106
+ expectedFlows = []string {
107
+ " cookie=0, table=101, priority=200, reg0=2, actions=two" ,
108
+ " cookie=0, table=101, priority=100, reg0=1, actions=one" ,
109
+ }
110
+ if err := checkDump (ovsif , "" , expectedFlows ); err != nil {
111
+ t .Fatalf (err .Error ())
112
+ }
113
+
114
+ // Add and Delete flows failed transaction, missing action
115
+ otx .AddFlow ("table=101, priority=300, reg0=3, actions=three" )
116
+ otx .DeleteFlows ("table=101" )
117
+ otx .AddFlow ("table=101, priority=400, reg0=4, actions" )
118
+ if err := otx .Commit (); err == nil {
119
+ t .Fatalf ("expected no error but got %v" , err )
120
+ }
121
+ if err := checkDump (ovsif , "" , expectedFlows ); err != nil {
122
+ t .Fatalf (err .Error ())
123
+ }
124
+ }
125
+
42
126
func TestFind (t * testing.T ) {
43
127
ovsif := NewFake ("br0" )
44
- err := ovsif .AddBridge ()
45
- if err != nil {
128
+ if err := ovsif .AddBridge (); err != nil {
46
129
t .Fatalf ("unexpected error adding bridge: %v" , err )
47
130
}
48
131
@@ -114,8 +197,7 @@ func checkDump(ovsif Interface, filter string, cmpFlows []string) error {
114
197
115
198
func TestFakeDumpFlows (t * testing.T ) {
116
199
ovsif := NewFake ("br0" )
117
- err := ovsif .AddBridge ()
118
- if err != nil {
200
+ if err := ovsif .AddBridge (); err != nil {
119
201
t .Fatalf ("unexpected error adding bridge: %v" , err )
120
202
}
121
203
@@ -165,13 +247,12 @@ func TestFakeDumpFlows(t *testing.T) {
165
247
otx .AddFlow ("table=30, priority=300, ip, nw_dst=%s, actions=output:2" , localSubnetGateway )
166
248
otx .AddFlow ("table=35, priority=300, ip, nw_dst=%s, actions=ct(commit,exec(set_field:1->ct_mark),table=70)" , localSubnetGateway )
167
249
168
- err = otx .Commit ()
169
- if err != nil {
250
+ if err := otx .Commit (); err != nil {
170
251
t .Fatalf ("unexpected error from AddFlow: %v" , err )
171
252
}
172
253
173
254
// fake DumpFlows sorts first by table, then by priority (decreasing), then by creation time
174
- err = checkDump (ovsif , "" , []string {
255
+ err : = checkDump (ovsif , "" , []string {
175
256
" cookie=0, table=0, priority=250, in_port=2, ip, nw_dst=224.0.0.0/4, actions=drop" ,
176
257
" cookie=0, table=0, priority=200, in_port=2, ip, actions=goto_table:30" ,
177
258
" cookie=0, table=0, priority=200, in_port=1, ip, nw_src=10.128.0.0/14, nw_dst=10.129.0.0/23, actions=move:NXM_NX_TUN_ID[0..31]->NXM_NX_REG0[],goto_table:10" ,
@@ -270,8 +351,7 @@ func matchActions(flows []string, actions ...string) bool {
270
351
271
352
func TestFlowMatchesMasked (t * testing.T ) {
272
353
ovsif := NewFake ("br0" )
273
- err := ovsif .AddBridge ()
274
- if err != nil {
354
+ if err := ovsif .AddBridge (); err != nil {
275
355
t .Fatalf ("unexpected error adding bridge: %v" , err )
276
356
}
277
357
@@ -280,8 +360,7 @@ func TestFlowMatchesMasked(t *testing.T) {
280
360
otx .AddFlow ("table=100, priority=200, reg0=2, actions=two" )
281
361
otx .AddFlow ("table=100, priority=300, reg0=3, cookie=1, actions=three" )
282
362
otx .AddFlow ("table=100, priority=400, reg0=4, cookie=0xe, actions=four" )
283
- err = otx .Commit ()
284
- if err != nil {
363
+ if err := otx .Commit (); err != nil {
285
364
t .Fatalf ("unexpected error from AddFlow: %v" , err )
286
365
}
287
366
flows , err := ovsif .DumpFlows ("" )
@@ -294,8 +373,7 @@ func TestFlowMatchesMasked(t *testing.T) {
294
373
295
374
otx = ovsif .NewTransaction ()
296
375
otx .DeleteFlows ("table=100, cookie=0/0xFFFF" )
297
- err = otx .Commit ()
298
- if err != nil {
376
+ if err = otx .Commit (); err != nil {
299
377
t .Fatalf ("unexpected error from AddFlow: %v" , err )
300
378
}
301
379
flows , err = ovsif .DumpFlows ("" )
@@ -308,8 +386,7 @@ func TestFlowMatchesMasked(t *testing.T) {
308
386
309
387
otx = ovsif .NewTransaction ()
310
388
otx .DeleteFlows ("table=100, cookie=2/2" )
311
- err = otx .Commit ()
312
- if err != nil {
389
+ if err = otx .Commit (); err != nil {
313
390
t .Fatalf ("unexpected error from AddFlow: %v" , err )
314
391
}
315
392
flows , err = ovsif .DumpFlows ("" )
0 commit comments