1 // Copyright (c) 2017 Cisco and/or its affiliates.
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:
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
21 "git.fd.io/govpp.git/adapter/mock"
22 "git.fd.io/govpp.git/api"
23 "git.fd.io/govpp.git/core"
24 "git.fd.io/govpp.git/core/bin_api/vpe"
25 "git.fd.io/govpp.git/examples/bin_api/interfaces"
26 "git.fd.io/govpp.git/examples/bin_api/memif"
27 "git.fd.io/govpp.git/examples/bin_api/tap"
29 . "github.com/onsi/gomega"
33 mockVpp *mock.VppAdapter
38 func setupTest(t *testing.T) *testCtx {
42 mockVpp: &mock.VppAdapter{},
46 ctx.conn, err = core.Connect(ctx.mockVpp)
47 Expect(err).ShouldNot(HaveOccurred())
49 ctx.ch, err = ctx.conn.NewAPIChannel()
50 Expect(err).ShouldNot(HaveOccurred())
55 func (ctx *testCtx) teardownTest() {
60 func TestRequestReplyTapConnect(t *testing.T) {
62 defer ctx.teardownTest()
64 ctx.mockVpp.MockReply(&tap.TapConnectReply{
68 request := &tap.TapConnect{
69 TapName: []byte("test-tap-name"),
72 reply := &tap.TapConnectReply{}
74 err := ctx.ch.SendRequest(request).ReceiveReply(reply)
75 Expect(err).ShouldNot(HaveOccurred())
76 Expect(reply.Retval).To(BeEquivalentTo(10), "Incorrect retval value for TapConnectReply")
77 Expect(reply.SwIfIndex).To(BeEquivalentTo(1), "Incorrect SwIfIndex value for TapConnectReply")
80 func TestRequestReplyTapModify(t *testing.T) {
82 defer ctx.teardownTest()
84 ctx.mockVpp.MockReply(&tap.TapModifyReply{
88 request := &tap.TapModify{
89 TapName: []byte("test-tap-modify"),
93 reply := &tap.TapModifyReply{}
95 err := ctx.ch.SendRequest(request).ReceiveReply(reply)
96 Expect(err).ShouldNot(HaveOccurred())
97 Expect(reply.Retval).To(BeEquivalentTo(15), "Incorrect retval value for TapModifyReply")
98 Expect(reply.SwIfIndex).To(BeEquivalentTo(2), "Incorrect SwIfIndex value for TapModifyReply")
101 func TestRequestReplyTapDelete(t *testing.T) {
103 defer ctx.teardownTest()
105 ctx.mockVpp.MockReply(&tap.TapDeleteReply{
108 request := &tap.TapDelete{
111 reply := &tap.TapDeleteReply{}
113 err := ctx.ch.SendRequest(request).ReceiveReply(reply)
114 Expect(err).ShouldNot(HaveOccurred())
115 Expect(reply.Retval).To(BeEquivalentTo(20), "Incorrect retval value for TapDeleteReply")
118 func TestRequestReplySwInterfaceTapDump(t *testing.T) {
120 defer ctx.teardownTest()
122 byteName := []byte("dev-name-test")
123 ctx.mockVpp.MockReply(&tap.SwInterfaceTapDetails{
127 request := &tap.SwInterfaceTapDump{}
128 reply := &tap.SwInterfaceTapDetails{}
130 err := ctx.ch.SendRequest(request).ReceiveReply(reply)
131 Expect(err).ShouldNot(HaveOccurred())
132 Expect(reply.SwIfIndex).To(BeEquivalentTo(25), "Incorrect SwIfIndex value for SwInterfaceTapDetails")
133 Expect(reply.DevName).ToNot(BeNil(), "Incorrect DevName value for SwInterfaceTapDetails")
136 func TestRequestReplyMemifCreate(t *testing.T) {
138 defer ctx.teardownTest()
140 ctx.mockVpp.MockReply(&memif.MemifCreateReply{
144 request := &memif.MemifCreate{
150 reply := &memif.MemifCreateReply{}
152 err := ctx.ch.SendRequest(request).ReceiveReply(reply)
153 Expect(err).ShouldNot(HaveOccurred())
154 Expect(reply.Retval).To(BeEquivalentTo(22), "Incorrect Retval value for MemifCreate")
155 Expect(reply.SwIfIndex).To(BeEquivalentTo(4), "Incorrect SwIfIndex value for MemifCreate")
158 func TestRequestReplyMemifDelete(t *testing.T) {
160 defer ctx.teardownTest()
162 ctx.mockVpp.MockReply(&memif.MemifDeleteReply{
165 request := &memif.MemifDelete{
168 reply := &memif.MemifDeleteReply{}
170 err := ctx.ch.SendRequest(request).ReceiveReply(reply)
171 Expect(err).ShouldNot(HaveOccurred())
172 Expect(reply.Retval).To(BeEquivalentTo(24), "Incorrect Retval value for MemifDelete")
175 func TestRequestReplyMemifDetails(t *testing.T) {
177 defer ctx.teardownTest()
179 ctx.mockVpp.MockReply(&memif.MemifDetails{
181 IfName: []byte("memif-name"),
184 request := &memif.MemifDump{}
185 reply := &memif.MemifDetails{}
187 err := ctx.ch.SendRequest(request).ReceiveReply(reply)
188 Expect(err).ShouldNot(HaveOccurred())
189 Expect(reply.SwIfIndex).To(BeEquivalentTo(25), "Incorrect SwIfIndex value for MemifDetails")
190 Expect(reply.IfName).ToNot(BeEmpty(), "MemifDetails IfName is empty byte array")
191 Expect(reply.Role).To(BeEquivalentTo(0), "Incorrect Role value for MemifDetails")
194 func TestMultiRequestReplySwInterfaceTapDump(t *testing.T) {
196 defer ctx.teardownTest()
199 for i := 1; i <= 10; i++ {
200 byteName := []byte("dev-name-test")
201 ctx.mockVpp.MockReply(&tap.SwInterfaceTapDetails{
202 SwIfIndex: uint32(i),
206 ctx.mockVpp.MockReply(&vpe.ControlPingReply{})
208 reqCtx := ctx.ch.SendMultiRequest(&tap.SwInterfaceTapDump{})
211 msg := &tap.SwInterfaceTapDetails{}
212 stop, err := reqCtx.ReceiveReply(msg)
214 break // break out of the loop
216 Expect(err).ShouldNot(HaveOccurred())
219 Expect(cnt).To(BeEquivalentTo(10))
222 func TestMultiRequestReplySwInterfaceMemifDump(t *testing.T) {
224 defer ctx.teardownTest()
227 for i := 1; i <= 10; i++ {
228 ctx.mockVpp.MockReply(&memif.MemifDetails{
229 SwIfIndex: uint32(i),
232 ctx.mockVpp.MockReply(&vpe.ControlPingReply{})
234 reqCtx := ctx.ch.SendMultiRequest(&memif.MemifDump{})
237 msg := &memif.MemifDetails{}
238 stop, err := reqCtx.ReceiveReply(msg)
240 break // break out of the loop
242 Expect(err).ShouldNot(HaveOccurred())
245 Expect(cnt).To(BeEquivalentTo(10))
248 func TestNotifications(t *testing.T) {
250 defer ctx.teardownTest()
252 // subscribe for notification
253 notifChan := make(chan api.Message, 1)
254 subs, err := ctx.ch.SubscribeNotification(notifChan, interfaces.NewSwInterfaceSetFlags)
255 Expect(err).ShouldNot(HaveOccurred())
257 // mock the notification and force its delivery
258 ctx.mockVpp.MockReply(&interfaces.SwInterfaceSetFlags{
262 ctx.mockVpp.SendMsg(0, []byte(""))
264 // receive the notification
265 var notif *interfaces.SwInterfaceSetFlags
266 Eventually(func() *interfaces.SwInterfaceSetFlags {
268 case n := <-notifChan:
269 notif = n.(*interfaces.SwInterfaceSetFlags)
274 }).ShouldNot(BeNil())
276 // verify the received notifications
277 Expect(notif.SwIfIndex).To(BeEquivalentTo(3), "Incorrect SwIfIndex value for SwInterfaceSetFlags")
278 Expect(notif.AdminUpDown).To(BeEquivalentTo(1), "Incorrect AdminUpDown value for SwInterfaceSetFlags")
280 ctx.ch.UnsubscribeNotification(subs)
283 func TestNotificationEvent(t *testing.T) {
285 defer ctx.teardownTest()
287 // subscribe for notification
288 notifChan := make(chan api.Message, 1)
289 subs, err := ctx.ch.SubscribeNotification(notifChan, interfaces.NewSwInterfaceEvent)
290 Expect(err).ShouldNot(HaveOccurred())
292 // mock the notification and force its delivery
293 ctx.mockVpp.MockReply(&interfaces.SwInterfaceEvent{
297 ctx.mockVpp.SendMsg(0, []byte(""))
299 // receive the notification
300 var notif *interfaces.SwInterfaceEvent
301 Eventually(func() *interfaces.SwInterfaceEvent {
303 case n := <-notifChan:
304 notif = n.(*interfaces.SwInterfaceEvent)
309 }).ShouldNot(BeNil())
311 // verify the received notifications
312 Expect(notif.SwIfIndex).To(BeEquivalentTo(2), "Incorrect SwIfIndex value for SwInterfaceSetFlags")
313 Expect(notif.LinkUpDown).To(BeEquivalentTo(1), "Incorrect LinkUpDown value for SwInterfaceSetFlags")
315 ctx.ch.UnsubscribeNotification(subs)
318 func TestCheckMessageCompatibility(t *testing.T) {
320 defer ctx.teardownTest()
322 err := ctx.ch.CheckMessageCompatibility(&interfaces.SwInterfaceSetFlags{})
323 Expect(err).ShouldNot(HaveOccurred())
326 func TestSetReplyTimeout(t *testing.T) {
328 defer ctx.teardownTest()
330 ctx.ch.SetReplyTimeout(time.Millisecond)
332 // first one request should work
333 ctx.mockVpp.MockReply(&vpe.ControlPingReply{})
334 err := ctx.ch.SendRequest(&vpe.ControlPing{}).ReceiveReply(&vpe.ControlPingReply{})
335 Expect(err).ShouldNot(HaveOccurred())
337 // no other reply ready - expect timeout
338 err = ctx.ch.SendRequest(&vpe.ControlPing{}).ReceiveReply(&vpe.ControlPingReply{})
339 Expect(err).Should(HaveOccurred())
340 Expect(err.Error()).To(ContainSubstring("timeout"))
343 func TestReceiveReplyNegative(t *testing.T) {
345 defer ctx.teardownTest()
348 reqCtx1 := &api.RequestCtx{}
349 err := reqCtx1.ReceiveReply(&vpe.ControlPingReply{})
350 Expect(err).Should(HaveOccurred())
351 Expect(err.Error()).To(ContainSubstring("invalid request context"))
354 reqCtx2 := &api.MultiRequestCtx{}
355 _, err = reqCtx2.ReceiveReply(&vpe.ControlPingReply{})
356 Expect(err).Should(HaveOccurred())
357 Expect(err.Error()).To(ContainSubstring("invalid request context"))
360 reqCtx3 := &api.RequestCtx{}
361 err = reqCtx3.ReceiveReply(nil)
362 Expect(err).Should(HaveOccurred())
363 Expect(err.Error()).To(ContainSubstring("invalid request context"))