4 "git.fd.io/govpp.git/adapter"
5 "git.fd.io/govpp.git/api"
6 "git.fd.io/govpp.git/examples/bin_api/interfaces"
7 "git.fd.io/govpp.git/examples/bin_api/vpe"
8 "github.com/golang/mock/gomock"
9 "github.com/stretchr/testify/assert"
16 vppDetails = vpe.ShowVersionReply{
17 Program: []byte("vpe"),
18 Version: []byte("18.10"),
21 testSwIfIndex = uint32(0)
22 testInterface = func() *vppInterface {
24 SwInterfaceDetails: interfaces.SwInterfaceDetails{SwIfIndex: testSwIfIndex}, // TODO
25 Stats: interfaceStats{}, // TODO
28 testInterfaces = func() *map[uint32]*vppInterface {
29 return &map[uint32]*vppInterface{
30 testSwIfIndex: testInterface(),
34 r = rand.New(rand.NewSource(time.Now().UnixNano()))
35 testCombinedStats = interfaceStats{
37 TxPackets: r.Uint64(),
39 RxPackets: r.Uint64(),
41 testCombinedStatsDump = []*adapter.StatEntry{
44 Type: adapter.CombinedCounterVector,
45 Data: adapter.CombinedCounterStat{
46 []adapter.CombinedCounter{
48 Bytes: adapter.Counter(testCombinedStats.TxBytes),
49 Packets: adapter.Counter(testCombinedStats.TxPackets),
56 Type: adapter.CombinedCounterVector,
57 Data: adapter.CombinedCounterStat{
58 []adapter.CombinedCounter{
60 Bytes: adapter.Counter(testCombinedStats.RxBytes),
61 Packets: adapter.Counter(testCombinedStats.RxPackets),
68 testSimpleStats = interfaceStats{
74 testSimpleStatsDump = []*adapter.StatEntry{
77 Type: adapter.SimpleCounterVector,
78 Data: adapter.SimpleCounterStat{
79 []adapter.Counter{adapter.Counter(testSimpleStats.TxErrors)},
84 Type: adapter.SimpleCounterVector,
85 Data: adapter.SimpleCounterStat{
86 []adapter.Counter{adapter.Counter(testSimpleStats.RxErrors)},
91 Type: adapter.SimpleCounterVector,
92 Data: adapter.SimpleCounterStat{
93 []adapter.Counter{adapter.Counter(testSimpleStats.Drops)},
98 Type: adapter.SimpleCounterVector,
99 Data: adapter.SimpleCounterStat{
100 []adapter.Counter{adapter.Counter(testSimpleStats.Punts)},
106 type showDetailsContext struct {
107 details vpe.ShowVersionReply
110 func (ctx *showDetailsContext) ReceiveReply(msg api.Message) (err error) {
111 *(msg.(*vpe.ShowVersionReply)) = vppDetails
115 type interfaceDumpContext struct {
116 interfaces []interfaces.SwInterfaceDetails
120 func (ctx *interfaceDumpContext) ReceiveReply(msg api.Message) (lastReplyReceived bool, err error) {
121 stop := ctx.currentIndex >= len(ctx.interfaces)
123 *(msg.(*interfaces.SwInterfaceDetails)) = ctx.interfaces[ctx.currentIndex]
129 func TestVppIfStats_GetVppVersion(t *testing.T) {
130 mockCtrl := gomock.NewController(t)
131 defer mockCtrl.Finish()
133 mockChannel := NewMockChannel(mockCtrl)
134 mockChannel.EXPECT().SendRequest(&vpe.ShowVersion{}).Return(&showDetailsContext{details: vppDetails})
136 v := vppConnector{api: mockChannel}
137 err := v.getVppVersion()
138 assert.NoError(t, err, "GetVppVersion should not return an error")
139 assert.Equal(t, vppDetails, v.VppDetails, "VPP details should be saved")
142 func TestVppIfStats_GetInterfaces(t *testing.T) {
143 mockCtrl := gomock.NewController(t)
144 defer mockCtrl.Finish()
146 testContext := interfaceDumpContext{interfaces: []interfaces.SwInterfaceDetails{testInterface().SwInterfaceDetails}}
147 mockChannel := NewMockChannel(mockCtrl)
148 mockChannel.EXPECT().SendMultiRequest(&interfaces.SwInterfaceDump{}).Return(&testContext)
150 v := vppConnector{api: mockChannel}
151 err := v.getInterfaces()
152 assert.NoError(t, err, "GetInterfaces should not return an error")
153 assert.Len(t, v.Interfaces, len(testContext.interfaces), "All dumped interfaces should be saved")
154 if len(testContext.interfaces) > 0 {
155 assert.Equal(t, testContext.interfaces[0], v.Interfaces[testInterface().SwIfIndex].SwInterfaceDetails,
156 "All dumped interface info should be saved")
160 func TestVppIfStats_GetStatsForAllInterfacesNoStats(t *testing.T) {
161 mockCtrl := gomock.NewController(t)
162 defer mockCtrl.Finish()
164 mockStatsAPI := NewMockStatsAPI(mockCtrl)
165 mockStatsAPI.EXPECT().DumpStats("/if").Return([]*adapter.StatEntry{}, nil)
167 v := vppConnector{stats: mockStatsAPI, Interfaces: *testInterfaces()}
168 err := v.getStatsForAllInterfaces()
169 assert.NoError(t, err, "GetStatsForAllInterfaces should not return an error")
170 assert.Equal(t, interfaceStats{}, v.Interfaces[testSwIfIndex].Stats, "Stats should be empty")
173 func testStats(t *testing.T, statsDump *[]*adapter.StatEntry, expectedStats *interfaceStats) {
174 mockCtrl := gomock.NewController(t)
175 defer mockCtrl.Finish()
177 mockStatsAPI := NewMockStatsAPI(mockCtrl)
178 mockStatsAPI.EXPECT().DumpStats("/if").Return(*statsDump, nil)
180 v := vppConnector{stats: mockStatsAPI, Interfaces: *testInterfaces()}
181 err := v.getStatsForAllInterfaces()
182 assert.NoError(t, err, "GetStatsForAllInterfaces should not return an error")
183 assert.Equal(t, *expectedStats, v.Interfaces[testSwIfIndex].Stats, "Collected and saved stats should match")
186 func TestVppIfStats_GetStatsForAllInterfacesCombinedStats(t *testing.T) {
187 testStats(t, &testCombinedStatsDump, &testCombinedStats)
190 func TestVppIfStats_GetStatsForAllInterfacesSimpleStats(t *testing.T) {
191 testStats(t, &testSimpleStatsDump, &testSimpleStats)