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 ctx.mockVpp.MockReply(&tap.SwInterfaceTapDetails{
201 SwIfIndex: uint32(i),
202 DevName: []byte("dev-name-test"),
205 ctx.mockVpp.MockReply(&vpe.ControlPingReply{})
207 reqCtx := ctx.ch.SendMultiRequest(&tap.SwInterfaceTapDump{})
210 msg := &tap.SwInterfaceTapDetails{}
211 stop, err := reqCtx.ReceiveReply(msg)
213 break // break out of the loop
215 Expect(err).ShouldNot(HaveOccurred())
218 Expect(cnt).To(BeEquivalentTo(10))
221 func TestMultiRequestReplySwInterfaceMemifDump(t *testing.T) {
223 defer ctx.teardownTest()
226 for i := 1; i <= 10; i++ {
227 ctx.mockVpp.MockReply(&memif.MemifDetails{
228 SwIfIndex: uint32(i),
231 ctx.mockVpp.MockReply(&vpe.ControlPingReply{})
233 reqCtx := ctx.ch.SendMultiRequest(&memif.MemifDump{})
236 msg := &memif.MemifDetails{}
237 stop, err := reqCtx.ReceiveReply(msg)
239 break // break out of the loop
241 Expect(err).ShouldNot(HaveOccurred())
244 Expect(cnt).To(BeEquivalentTo(10))
247 func TestNotifications(t *testing.T) {
249 defer ctx.teardownTest()
251 // subscribe for notification
252 notifChan := make(chan api.Message, 1)
253 subs, err := ctx.ch.SubscribeNotification(notifChan, interfaces.NewSwInterfaceSetFlags)
254 Expect(err).ShouldNot(HaveOccurred())
256 // mock the notification and force its delivery
257 ctx.mockVpp.MockReply(&interfaces.SwInterfaceSetFlags{
261 ctx.mockVpp.SendMsg(0, []byte(""))
263 // receive the notification
264 var notif *interfaces.SwInterfaceSetFlags
265 Eventually(func() *interfaces.SwInterfaceSetFlags {
267 case n := <-notifChan:
268 notif = n.(*interfaces.SwInterfaceSetFlags)
273 }).ShouldNot(BeNil())
275 // verify the received notifications
276 Expect(notif.SwIfIndex).To(BeEquivalentTo(3), "Incorrect SwIfIndex value for SwInterfaceSetFlags")
277 Expect(notif.AdminUpDown).To(BeEquivalentTo(1), "Incorrect AdminUpDown value for SwInterfaceSetFlags")
279 ctx.ch.UnsubscribeNotification(subs)
282 func TestNotificationEvent(t *testing.T) {
284 defer ctx.teardownTest()
286 // subscribe for notification
287 notifChan := make(chan api.Message, 1)
288 subs, err := ctx.ch.SubscribeNotification(notifChan, interfaces.NewSwInterfaceEvent)
289 Expect(err).ShouldNot(HaveOccurred())
291 // mock the notification and force its delivery
292 ctx.mockVpp.MockReply(&interfaces.SwInterfaceEvent{
296 ctx.mockVpp.SendMsg(0, []byte(""))
298 // receive the notification
299 var notif *interfaces.SwInterfaceEvent
300 Eventually(func() *interfaces.SwInterfaceEvent {
302 case n := <-notifChan:
303 notif = n.(*interfaces.SwInterfaceEvent)
308 }).ShouldNot(BeNil())
310 // verify the received notifications
311 Expect(notif.SwIfIndex).To(BeEquivalentTo(2), "Incorrect SwIfIndex value for SwInterfaceSetFlags")
312 Expect(notif.LinkUpDown).To(BeEquivalentTo(1), "Incorrect LinkUpDown value for SwInterfaceSetFlags")
314 ctx.ch.UnsubscribeNotification(subs)
317 func TestCheckMessageCompatibility(t *testing.T) {
319 defer ctx.teardownTest()
321 err := ctx.ch.CheckMessageCompatibility(&interfaces.SwInterfaceSetFlags{})
322 Expect(err).ShouldNot(HaveOccurred())
325 func TestSetReplyTimeout(t *testing.T) {
327 defer ctx.teardownTest()
329 ctx.ch.SetReplyTimeout(time.Millisecond)
331 // first one request should work
332 ctx.mockVpp.MockReply(&vpe.ControlPingReply{})
333 err := ctx.ch.SendRequest(&vpe.ControlPing{}).ReceiveReply(&vpe.ControlPingReply{})
334 Expect(err).ShouldNot(HaveOccurred())
336 // no other reply ready - expect timeout
337 err = ctx.ch.SendRequest(&vpe.ControlPing{}).ReceiveReply(&vpe.ControlPingReply{})
338 Expect(err).Should(HaveOccurred())
339 Expect(err.Error()).To(ContainSubstring("timeout"))
342 func TestSetReplyTimeoutMultiRequest(t *testing.T) {
344 defer ctx.teardownTest()
346 ctx.ch.SetReplyTimeout(time.Millisecond)
348 for i := 1; i <= 3; i++ {
349 ctx.mockVpp.MockReply(&interfaces.SwInterfaceDetails{
350 SwIfIndex: uint32(i),
351 InterfaceName: []byte("if-name-test"),
354 ctx.mockVpp.MockReply(&vpe.ControlPingReply{})
357 sendMultiRequest := func() error {
358 reqCtx := ctx.ch.SendMultiRequest(&interfaces.SwInterfaceDump{})
360 msg := &interfaces.SwInterfaceDetails{}
361 stop, err := reqCtx.ReceiveReply(msg)
363 break // break out of the loop
373 // first one request should work
374 err := sendMultiRequest()
375 Expect(err).ShouldNot(HaveOccurred())
377 // no other reply ready - expect timeout
378 err = sendMultiRequest()
379 Expect(err).Should(HaveOccurred())
380 Expect(err.Error()).To(ContainSubstring("timeout"))
382 Expect(cnt).To(BeEquivalentTo(3))
385 func TestReceiveReplyNegative(t *testing.T) {
387 defer ctx.teardownTest()
390 reqCtx1 := &api.RequestCtx{}
391 err := reqCtx1.ReceiveReply(&vpe.ControlPingReply{})
392 Expect(err).Should(HaveOccurred())
393 Expect(err.Error()).To(ContainSubstring("invalid request context"))
396 reqCtx2 := &api.MultiRequestCtx{}
397 _, err = reqCtx2.ReceiveReply(&vpe.ControlPingReply{})
398 Expect(err).Should(HaveOccurred())
399 Expect(err.Error()).To(ContainSubstring("invalid request context"))
402 reqCtx3 := &api.RequestCtx{}
403 err = reqCtx3.ReceiveReply(nil)
404 Expect(err).Should(HaveOccurred())
405 Expect(err.Error()).To(ContainSubstring("invalid request context"))
408 func TestMultiRequestDouble(t *testing.T) {
410 defer ctx.teardownTest()
413 for i := 1; i <= 3; i++ {
414 ctx.mockVpp.MockReply(&interfaces.SwInterfaceDetails{
415 SwIfIndex: uint32(i),
416 InterfaceName: []byte("if-name-test"),
419 ctx.mockVpp.MockReply(&vpe.ControlPingReply{})
420 for i := 1; i <= 3; i++ {
421 ctx.mockVpp.MockReply(&interfaces.SwInterfaceDetails{
422 SwIfIndex: uint32(i),
423 InterfaceName: []byte("if-name-test"),
426 ctx.mockVpp.MockReply(&vpe.ControlPingReply{})
429 var sendMultiRequest = func() error {
430 reqCtx := ctx.ch.SendMultiRequest(&interfaces.SwInterfaceDump{})
432 msg := &interfaces.SwInterfaceDetails{}
433 stop, err := reqCtx.ReceiveReply(msg)
435 break // break out of the loop
445 err := sendMultiRequest()
446 Expect(err).ShouldNot(HaveOccurred())
448 err = sendMultiRequest()
449 Expect(err).ShouldNot(HaveOccurred())
451 Expect(cnt).To(BeEquivalentTo(6))
454 func TestReceiveReplyAfterTimeout(t *testing.T) {
456 defer ctx.teardownTest()
458 ctx.ch.SetReplyTimeout(time.Millisecond)
460 // first one request should work
461 ctx.mockVpp.MockReply(&vpe.ControlPingReply{})
462 err := ctx.ch.SendRequest(&vpe.ControlPing{}).ReceiveReply(&vpe.ControlPingReply{})
463 Expect(err).ShouldNot(HaveOccurred())
465 err = ctx.ch.SendRequest(&vpe.ControlPing{}).ReceiveReply(&vpe.ControlPingReply{})
466 Expect(err).Should(HaveOccurred())
467 Expect(err.Error()).To(ContainSubstring("timeout"))
469 // simulating late reply
470 ctx.mockVpp.MockReply(&vpe.ControlPingReply{})
472 // normal reply for next request
473 ctx.mockVpp.MockReply(&tap.TapConnectReply{})
475 req := &tap.TapConnect{
476 TapName: []byte("test-tap-name"),
479 reply := &tap.TapConnectReply{}
482 err = ctx.ch.SendRequest(req).ReceiveReply(reply)
483 Expect(err).ShouldNot(HaveOccurred())
487 TODO: fix mock adapter
488 This test will fail because mock adapter will stop sending replies
489 when it encounters control_ping_reply from multi request,
490 thus never sending reply for next request
492 func TestReceiveReplyAfterTimeoutMultiRequest(t *testing.T) {
494 defer ctx.teardownTest()
496 ctx.ch.SetReplyTimeout(time.Millisecond * 100)
498 // first one request should work
499 ctx.mockVpp.MockReply(&vpe.ControlPingReply{})
500 err := ctx.ch.SendRequest(&vpe.ControlPing{}).ReceiveReply(&vpe.ControlPingReply{})
501 Expect(err).ShouldNot(HaveOccurred())
504 var sendMultiRequest = func() error {
505 reqCtx := ctx.ch.SendMultiRequest(&interfaces.SwInterfaceDump{})
507 msg := &interfaces.SwInterfaceDetails{}
508 stop, err := reqCtx.ReceiveReply(msg)
510 break // break out of the loop
520 err = sendMultiRequest()
521 Expect(err).Should(HaveOccurred())
522 Expect(err.Error()).To(ContainSubstring("timeout"))
523 Expect(cnt).To(BeEquivalentTo(0))
525 // simulating late replies
526 for i := 1; i <= 3; i++ {
527 ctx.mockVpp.MockReply(&interfaces.SwInterfaceDetails{
528 SwIfIndex: uint32(i),
529 InterfaceName: []byte("if-name-test"),
532 ctx.mockVpp.MockReply(&vpe.ControlPingReply{})
534 // normal reply for next request
535 ctx.mockVpp.MockReply(&tap.TapConnectReply{})
537 req := &tap.TapConnect{
538 TapName: []byte("test-tap-name"),
541 reply := &tap.TapConnectReply{}
544 err = ctx.ch.SendRequest(req).ReceiveReply(reply)
545 Expect(err).ShouldNot(HaveOccurred())