1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
package main
import (
"math/rand"
"testing"
"time"
"git.fd.io/govpp.git/adapter"
"git.fd.io/govpp.git/api"
"git.fd.io/govpp.git/examples/bin_api/interfaces"
"git.fd.io/govpp.git/examples/bin_api/vpe"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
)
var (
vppDetails = vpe.ShowVersionReply{
Program: []byte("vpe"),
Version: []byte("18.10"),
}
testSwIfIndex = uint32(0)
testInterface = func() *vppInterface {
return &vppInterface{
SwInterfaceDetails: interfaces.SwInterfaceDetails{SwIfIndex: testSwIfIndex}, // TODO
Stats: interfaceStats{}, // TODO
}
}
testInterfaces = func() []*vppInterface {
return []*vppInterface{
testInterface(),
}
}
r = rand.New(rand.NewSource(time.Now().UnixNano()))
testCombinedStats = interfaceStats{
TxBytes: r.Uint64(),
TxPackets: r.Uint64(),
RxBytes: r.Uint64(),
RxPackets: r.Uint64(),
}
testCombinedStatsDump = []*adapter.StatEntry{
{
Name: "/if/tx",
Type: adapter.CombinedCounterVector,
Data: adapter.CombinedCounterStat{
[]adapter.CombinedCounter{
{
Bytes: adapter.Counter(testCombinedStats.TxBytes),
Packets: adapter.Counter(testCombinedStats.TxPackets),
},
},
},
},
{
Name: "/if/rx",
Type: adapter.CombinedCounterVector,
Data: adapter.CombinedCounterStat{
[]adapter.CombinedCounter{
{
Bytes: adapter.Counter(testCombinedStats.RxBytes),
Packets: adapter.Counter(testCombinedStats.RxPackets),
},
},
},
},
}
testSimpleStats = interfaceStats{
TxErrors: r.Uint64(),
RxErrors: r.Uint64(),
Drops: r.Uint64(),
Punts: r.Uint64(),
}
testSimpleStatsDump = []*adapter.StatEntry{
{
Name: "/if/tx-error",
Type: adapter.SimpleCounterVector,
Data: adapter.SimpleCounterStat{
[]adapter.Counter{adapter.Counter(testSimpleStats.TxErrors)},
},
},
{
Name: "/if/rx-error",
Type: adapter.SimpleCounterVector,
Data: adapter.SimpleCounterStat{
[]adapter.Counter{adapter.Counter(testSimpleStats.RxErrors)},
},
},
{
Name: "/if/drops",
Type: adapter.SimpleCounterVector,
Data: adapter.SimpleCounterStat{
[]adapter.Counter{adapter.Counter(testSimpleStats.Drops)},
},
},
{
Name: "/if/punt",
Type: adapter.SimpleCounterVector,
Data: adapter.SimpleCounterStat{
[]adapter.Counter{adapter.Counter(testSimpleStats.Punts)},
},
},
}
)
type showDetailsContext struct {
details vpe.ShowVersionReply
}
func (ctx *showDetailsContext) ReceiveReply(msg api.Message) (err error) {
*(msg.(*vpe.ShowVersionReply)) = vppDetails
return nil
}
type interfaceDumpContext struct {
interfaces []interfaces.SwInterfaceDetails
currentIndex int
}
func (ctx *interfaceDumpContext) ReceiveReply(msg api.Message) (lastReplyReceived bool, err error) {
stop := ctx.currentIndex >= len(ctx.interfaces)
if !stop {
*(msg.(*interfaces.SwInterfaceDetails)) = ctx.interfaces[ctx.currentIndex]
ctx.currentIndex++
}
return stop, nil
}
func TestVppIfStats_GetVppVersion(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
mockChannel := NewMockChannel(mockCtrl)
mockChannel.EXPECT().SendRequest(&vpe.ShowVersion{}).Return(&showDetailsContext{details: vppDetails})
v := vppConnector{api: mockChannel}
err := v.getVppVersion()
assert.NoError(t, err, "GetVppVersion should not return an error")
assert.Equal(t, vppDetails, v.VppDetails, "VPP details should be saved")
}
func TestVppIfStats_GetInterfaces(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
testContext := interfaceDumpContext{interfaces: []interfaces.SwInterfaceDetails{testInterface().SwInterfaceDetails}}
mockChannel := NewMockChannel(mockCtrl)
mockChannel.EXPECT().SendMultiRequest(&interfaces.SwInterfaceDump{}).Return(&testContext)
v := vppConnector{api: mockChannel}
err := v.getInterfaces()
assert.NoError(t, err, "GetInterfaces should not return an error")
assert.Len(t, v.Interfaces, len(testContext.interfaces), "All dumped interfaces should be saved")
if len(testContext.interfaces) > 0 {
assert.Equal(t, testContext.interfaces[0], v.Interfaces[testInterface().SwIfIndex].SwInterfaceDetails,
"All dumped interface info should be saved")
}
}
func TestVppIfStats_GetStatsForAllInterfacesNoStats(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
mockStatsAPI := NewMockStatsAPI(mockCtrl)
mockStatsAPI.EXPECT().DumpStats("/if").Return([]*adapter.StatEntry{}, nil)
v := vppConnector{stats: mockStatsAPI, Interfaces: testInterfaces()}
err := v.getStatsForAllInterfaces()
assert.NoError(t, err, "GetStatsForAllInterfaces should not return an error")
assert.Equal(t, interfaceStats{}, v.Interfaces[testSwIfIndex].Stats, "Stats should be empty")
}
func testStats(t *testing.T, statsDump *[]*adapter.StatEntry, expectedStats *interfaceStats) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
mockStatsAPI := NewMockStatsAPI(mockCtrl)
mockStatsAPI.EXPECT().DumpStats("/if").Return(*statsDump, nil)
v := vppConnector{stats: mockStatsAPI, Interfaces: testInterfaces()}
err := v.getStatsForAllInterfaces()
assert.NoError(t, err, "GetStatsForAllInterfaces should not return an error")
assert.Equal(t, *expectedStats, v.Interfaces[testSwIfIndex].Stats, "Collected and saved stats should match")
}
func TestVppIfStats_GetStatsForAllInterfacesCombinedStats(t *testing.T) {
testStats(t, &testCombinedStatsDump, &testCombinedStats)
}
func TestVppIfStats_GetStatsForAllInterfacesSimpleStats(t *testing.T) {
testStats(t, &testSimpleStatsDump, &testSimpleStats)
}
|