Skip to content

Commit 55ff6b1

Browse files
committed
Add unit tests for pkg/ovs/openflow
Signed-off-by: Hongliang Liu <lhongliang@vmware.com>
1 parent fbfa97d commit 55ff6b1

8 files changed

Lines changed: 4713 additions & 24 deletions

File tree

pkg/ovs/openflow/ofctrl_action_test.go

Lines changed: 2133 additions & 0 deletions
Large diffs are not rendered by default.

pkg/ovs/openflow/ofctrl_builder_test.go

Lines changed: 1943 additions & 21 deletions
Large diffs are not rendered by default.
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
// Copyright 2023 Antrea Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package openflow
16+
17+
import (
18+
"net"
19+
"testing"
20+
21+
"antrea.io/libOpenflow/openflow15"
22+
"antrea.io/libOpenflow/util"
23+
"antrea.io/ofnet/ofctrl"
24+
"github.com/stretchr/testify/assert"
25+
)
26+
27+
func getGroupMod(t *testing.T, g Group) *openflow15.GroupMod {
28+
msgs, err := g.GetBundleMessages(AddMessage)
29+
assert.NoError(t, err)
30+
assert.Equal(t, 1, len(msgs))
31+
return msgs[0].GetMessage().(*openflow15.GroupMod)
32+
}
33+
34+
func TestBucketBuilder(t *testing.T) {
35+
t.Run("Weight", func(t *testing.T) {
36+
weight := uint16(100)
37+
g := &ofGroup{ofctrl: &ofctrl.Group{}}
38+
group := g.Bucket().Weight(weight).Done()
39+
groupMod := getGroupMod(t, group)
40+
assert.Equal(t, 1, len(groupMod.Buckets))
41+
assert.Equal(t, 1, len(groupMod.Buckets[0].Properties))
42+
assert.Equal(t, weight, groupMod.Buckets[0].Properties[0].(*openflow15.GroupBucketPropWeight).Weight)
43+
assert.Contains(t, GroupModToString(groupMod), "weight:100")
44+
})
45+
t.Run("LoadToRegField", func(t *testing.T) {
46+
testCases := []struct {
47+
regField *RegField
48+
value uint32
49+
expected *openflow15.ActionSetField
50+
expectedActionStr string
51+
}{
52+
{
53+
regField: NewRegField(1, 0, 31),
54+
value: uint32(0xffff_ffff),
55+
expected: &openflow15.ActionSetField{
56+
Field: openflow15.MatchField{
57+
Class: openflow15.OXM_CLASS_NXM_1,
58+
Field: openflow15.NXM_NX_REG1,
59+
Value: &openflow15.Uint32Message{Data: uint32(0xffff_ffff)},
60+
Mask: &openflow15.Uint32Message{Data: uint32(0xffff_ffff)},
61+
},
62+
},
63+
expectedActionStr: "set_field:0xffffffff->reg1",
64+
},
65+
{
66+
regField: NewRegField(1, 4, 15),
67+
value: uint32(0xf),
68+
expected: &openflow15.ActionSetField{
69+
Field: openflow15.MatchField{
70+
Class: openflow15.OXM_CLASS_NXM_1,
71+
Field: openflow15.NXM_NX_REG1,
72+
Value: &openflow15.Uint32Message{Data: uint32(0xf0)},
73+
Mask: &openflow15.Uint32Message{Data: uint32(0xfff0)},
74+
},
75+
},
76+
expectedActionStr: "set_field:0xf0/0xfff0->reg1",
77+
},
78+
}
79+
for _, tc := range testCases {
80+
g := &ofGroup{ofctrl: &ofctrl.Group{}}
81+
group := g.Bucket().LoadToRegField(tc.regField, tc.value).Done()
82+
groupMod := getGroupMod(t, group)
83+
assert.Equal(t, 1, len(groupMod.Buckets))
84+
checkActionSetFields(t, []*openflow15.ActionSetField{tc.expected}, groupMod.Buckets[0].Actions)
85+
assert.Contains(t, GroupModToString(groupMod), tc.expectedActionStr)
86+
}
87+
})
88+
t.Run("LoadXXReg", func(t *testing.T) {
89+
regID := 0
90+
data := []byte{0x11, 0x22, 0x33, 0x44}
91+
expected := &openflow15.ActionSetField{
92+
Field: openflow15.MatchField{
93+
Class: openflow15.OXM_CLASS_NXM_1,
94+
Field: openflow15.NXM_NX_XXREG0,
95+
Value: util.NewBuffer(data),
96+
},
97+
}
98+
expectedActionStr := "set_field:0x11223344->xxreg0"
99+
100+
g := &ofGroup{ofctrl: &ofctrl.Group{}}
101+
group := g.Bucket().LoadXXReg(regID, data).Done()
102+
groupMod := getGroupMod(t, group)
103+
assert.Equal(t, 1, len(groupMod.Buckets))
104+
checkActionSetFields(t, []*openflow15.ActionSetField{expected}, groupMod.Buckets[0].Actions)
105+
assert.Contains(t, GroupModToString(groupMod), expectedActionStr)
106+
})
107+
t.Run("SetTunnelDst", func(t *testing.T) {
108+
testCases := []struct {
109+
dstIP net.IP
110+
expected *openflow15.ActionSetField
111+
expectedActionStr string
112+
}{
113+
{
114+
dstIP: net.ParseIP("1.1.1.1"),
115+
expected: &openflow15.ActionSetField{
116+
Field: openflow15.MatchField{
117+
Class: openflow15.OXM_CLASS_NXM_1,
118+
Field: openflow15.NXM_NX_TUN_IPV4_DST,
119+
Value: &openflow15.TunnelIpv4DstField{
120+
TunnelIpv4Dst: net.ParseIP("1.1.1.1"),
121+
},
122+
},
123+
},
124+
expectedActionStr: "set_field:1.1.1.1->tun_dst",
125+
},
126+
{
127+
dstIP: net.ParseIP("fec0::1111"),
128+
expected: &openflow15.ActionSetField{
129+
Field: openflow15.MatchField{
130+
Class: openflow15.OXM_CLASS_NXM_1,
131+
Field: openflow15.NXM_NX_TUN_IPV6_DST,
132+
Value: &openflow15.Ipv6DstField{
133+
Ipv6Dst: net.ParseIP("fec0::1111"),
134+
},
135+
},
136+
},
137+
expectedActionStr: "set_field:fec0::1111->tun_ipv6_dst",
138+
},
139+
}
140+
for _, tc := range testCases {
141+
g := &ofGroup{ofctrl: &ofctrl.Group{}}
142+
group := g.Bucket().SetTunnelDst(tc.dstIP).Done()
143+
groupMod := getGroupMod(t, group)
144+
assert.Equal(t, 1, len(groupMod.Buckets))
145+
checkActionSetFields(t, []*openflow15.ActionSetField{tc.expected}, groupMod.Buckets[0].Actions)
146+
assert.Contains(t, GroupModToString(groupMod), tc.expectedActionStr)
147+
}
148+
})
149+
t.Run("ResubmitToTable", func(t *testing.T) {
150+
tableID := uint8(100)
151+
g := &ofGroup{ofctrl: &ofctrl.Group{}}
152+
group := g.Bucket().ResubmitToTable(tableID).Done()
153+
groupMod := getGroupMod(t, group)
154+
assert.Equal(t, 1, len(groupMod.Buckets))
155+
assert.Equal(t, 1, len(groupMod.Buckets[0].Actions))
156+
assert.IsType(t, &openflow15.NXActionResubmitTable{}, groupMod.Buckets[0].Actions[0])
157+
158+
action := groupMod.Buckets[0].Actions[0].(*openflow15.NXActionResubmitTable)
159+
assert.Equal(t, uint16(openflow15.OFPP_IN_PORT), action.InPort)
160+
assert.Equal(t, tableID, action.TableID)
161+
assert.Contains(t, GroupModToString(groupMod), "resubmit:100")
162+
})
163+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
// Copyright 2023 Antrea Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package openflow
16+
17+
import (
18+
"testing"
19+
20+
"antrea.io/libOpenflow/openflow15"
21+
"antrea.io/libOpenflow/util"
22+
"antrea.io/ofnet/ofctrl"
23+
"github.com/stretchr/testify/assert"
24+
)
25+
26+
func getMeterMod(t *testing.T, m Meter) *openflow15.MeterMod {
27+
msgs, err := m.GetBundleMessages(AddMessage)
28+
assert.NoError(t, err)
29+
assert.Equal(t, 1, len(msgs))
30+
return msgs[0].GetMessage().(*openflow15.MeterMod)
31+
}
32+
33+
func TestMeterBandBuilder(t *testing.T) {
34+
m := &ofMeter{ofctrl: &ofctrl.Meter{}}
35+
36+
t.Run("MeterType", func(t *testing.T) {
37+
testCases := []struct {
38+
meterType ofctrl.MeterType
39+
expectedBandType util.Message
40+
}{
41+
{ofctrl.MeterDrop, &openflow15.MeterBandDrop{}},
42+
{ofctrl.MeterDSCPRemark, &openflow15.MeterBandDSCP{}},
43+
{ofctrl.MeterExperimenter, &openflow15.MeterBandExperimenter{}},
44+
}
45+
for _, tc := range testCases {
46+
m.ResetMeterBands()
47+
meter := m.MeterBand().MeterType(tc.meterType).Done()
48+
meterMod := getMeterMod(t, meter)
49+
assert.Equal(t, 1, len(meterMod.MeterBands))
50+
assert.IsType(t, tc.expectedBandType, meterMod.MeterBands[0])
51+
}
52+
})
53+
t.Run("Rate", func(t *testing.T) {
54+
testCases := []struct {
55+
rate uint32
56+
}{
57+
{100},
58+
{200},
59+
}
60+
for _, tc := range testCases {
61+
m.ResetMeterBands()
62+
meter := m.MeterBand().MeterType(ofctrl.MeterDSCPRemark).Rate(tc.rate).Done()
63+
meterMod := getMeterMod(t, meter)
64+
assert.Equal(t, 1, len(meterMod.MeterBands))
65+
assert.Equal(t, tc.rate, meterMod.MeterBands[0].(*openflow15.MeterBandDSCP).MeterBandHeader.Rate)
66+
}
67+
})
68+
t.Run("Burst", func(t *testing.T) {
69+
testCases := []struct {
70+
burst uint32
71+
}{
72+
{100},
73+
{200},
74+
}
75+
for _, tc := range testCases {
76+
m.ResetMeterBands()
77+
meter := m.MeterBand().MeterType(ofctrl.MeterDSCPRemark).Burst(tc.burst).Done()
78+
meterMod := getMeterMod(t, meter)
79+
assert.Equal(t, 1, len(meterMod.MeterBands))
80+
assert.Equal(t, tc.burst, meterMod.MeterBands[0].(*openflow15.MeterBandDSCP).MeterBandHeader.BurstSize)
81+
}
82+
})
83+
t.Run("PrecLevel", func(t *testing.T) {
84+
testCases := []struct {
85+
precLevel uint8
86+
}{
87+
{100},
88+
{200},
89+
}
90+
for _, tc := range testCases {
91+
m.ResetMeterBands()
92+
meter := m.MeterBand().MeterType(ofctrl.MeterDSCPRemark).PrecLevel(tc.precLevel).Done()
93+
meterMod := getMeterMod(t, meter)
94+
assert.Equal(t, 1, len(meterMod.MeterBands))
95+
assert.Equal(t, tc.precLevel, meterMod.MeterBands[0].(*openflow15.MeterBandDSCP).PrecLevel)
96+
}
97+
})
98+
t.Run("Experimenter", func(t *testing.T) {
99+
testCases := []struct {
100+
experimenter uint32
101+
}{
102+
{100},
103+
{200},
104+
}
105+
for _, tc := range testCases {
106+
m.ResetMeterBands()
107+
meter := m.MeterBand().MeterType(ofctrl.MeterExperimenter).Experimenter(tc.experimenter).Done()
108+
meterMod := getMeterMod(t, meter)
109+
assert.Equal(t, 1, len(meterMod.MeterBands))
110+
assert.Equal(t, tc.experimenter, meterMod.MeterBands[0].(*openflow15.MeterBandExperimenter).Experimenter)
111+
}
112+
})
113+
}

pkg/ovs/openflow/ofctrl_packetout.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ func (b *ofPacketOutBuilder) SetEthPacket(packet *protocol.Ethernet) PacketOutBu
330330
return b
331331
}
332332

333-
// AddSetIPToSAction sets the IP_TOS field in the packet-out message. The action clears the two ECN bits as 0,
333+
// AddSetIPTOSAction sets the IP_TOS field in the packet-out message. The action clears the two ECN bits as 0,
334334
// and only 2-7 bits of the DSCP field in IP header is set.
335335
func (b *ofPacketOutBuilder) AddSetIPTOSAction(data uint8) PacketOutBuilder {
336336
field, _ := openflow15.FindFieldHeaderByName(NxmFieldIPToS, true)

0 commit comments

Comments
 (0)