This repository has been archived by the owner on Aug 3, 2020. It is now read-only.
forked from ligato/vpp-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vpp_ctx_mock.go
186 lines (159 loc) · 5.37 KB
/
vpp_ctx_mock.go
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
// Copyright (c) 2017 Cisco and/or its affiliates.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package vppcallmock
import (
"testing"
"git.fd.io/govpp.git/adapter/mock"
govppapi "git.fd.io/govpp.git/api"
govpp "git.fd.io/govpp.git/core"
log "github.com/ligato/cn-infra/logging/logrus"
"github.com/ligato/vpp-agent/plugins/vpp/binapi/vpp1901/vpe"
. "github.com/onsi/gomega"
)
// TestCtx is helping structure for unit testing.
// It wraps VppAdapter which is used instead of real VPP.
type TestCtx struct {
MockVpp *mock.VppAdapter
MockStats *mock.StatsAdapter
conn *govpp.Connection
channel govppapi.Channel
MockChannel *mockedChannel
}
// SetupTestCtx sets up all fields of TestCtx structure at the begining of test
func SetupTestCtx(t *testing.T) *TestCtx {
RegisterTestingT(t)
ctx := &TestCtx{
MockVpp: mock.NewVppAdapter(),
MockStats: mock.NewStatsAdapter(),
}
var err error
ctx.conn, err = govpp.Connect(ctx.MockVpp)
Expect(err).ShouldNot(HaveOccurred())
ctx.channel, err = ctx.conn.NewAPIChannel()
Expect(err).ShouldNot(HaveOccurred())
ctx.MockChannel = &mockedChannel{Channel: ctx.channel}
return ctx
}
// TeardownTestCtx politely close all used resources
func (ctx *TestCtx) TeardownTestCtx() {
ctx.channel.Close()
ctx.conn.Disconnect()
}
// MockedChannel implements ChannelIntf for testing purposes
type mockedChannel struct {
govppapi.Channel
// Last message which passed through method SendRequest
Msg govppapi.Message
// List of all messages which passed through method SendRequest
Msgs []govppapi.Message
RetErrs []error
}
// SendRequest just save input argument to structure field for future check
func (m *mockedChannel) SendRequest(msg govppapi.Message) govppapi.RequestCtx {
m.Msg = msg
m.Msgs = append(m.Msgs, msg)
reqCtx := m.Channel.SendRequest(msg)
var retErr error
if retErrsLen := len(m.RetErrs); retErrsLen > 0 {
retErr = m.RetErrs[retErrsLen-1]
m.RetErrs = m.RetErrs[:retErrsLen-1]
}
return &mockedContext{reqCtx, retErr}
}
// SendMultiRequest just save input argument to structure field for future check
func (m *mockedChannel) SendMultiRequest(msg govppapi.Message) govppapi.MultiRequestCtx {
m.Msg = msg
m.Msgs = append(m.Msgs, msg)
return m.Channel.SendMultiRequest(msg)
}
type mockedContext struct {
requestCtx govppapi.RequestCtx
retErr error
}
// ReceiveReply returns prepared error or nil
func (m *mockedContext) ReceiveReply(msg govppapi.Message) error {
if m.retErr != nil {
return m.retErr
}
return m.requestCtx.ReceiveReply(msg)
}
// HandleReplies represents spec for MockReplyHandler.
type HandleReplies struct {
Name string
Ping bool
Message govppapi.Message
Messages []govppapi.Message
}
// MockReplies sets up reply handler for give HandleReplies.
func (ctx *TestCtx) MockReplies(dataList []*HandleReplies) {
var sendControlPing bool
ctx.MockVpp.MockReplyHandler(func(request mock.MessageDTO) (reply []byte, msgID uint16, prepared bool) {
// Following types are not automatically stored in mock adapter's map and will be sent with empty MsgName
// TODO: initialize mock adapter's map with these
switch request.MsgID {
case 100:
request.MsgName = "control_ping"
case 101:
request.MsgName = "control_ping_reply"
case 200:
request.MsgName = "sw_interface_dump"
case 201:
request.MsgName = "sw_interface_details"
}
if request.MsgName == "" {
log.DefaultLogger().Fatalf("mockHandler received request (ID: %v) with empty MsgName, check if compatibility check is done before using this request", request.MsgID)
}
if sendControlPing {
sendControlPing = false
data := &vpe.ControlPingReply{}
reply, err := ctx.MockVpp.ReplyBytes(request, data)
Expect(err).To(BeNil())
msgID, err := ctx.MockVpp.GetMsgID(data.GetMessageName(), data.GetCrcString())
Expect(err).To(BeNil())
return reply, msgID, true
}
for _, dataMock := range dataList {
if request.MsgName == dataMock.Name {
// Send control ping next iteration if set
sendControlPing = dataMock.Ping
if len(dataMock.Messages) > 0 {
log.DefaultLogger().Infof(" MOCK HANDLER: mocking %d messages", len(dataMock.Messages))
for _, msg := range dataMock.Messages {
ctx.MockVpp.MockReply(msg)
}
return nil, 0, false
}
if dataMock.Message == nil {
return nil, 0, false
}
msgID, err := ctx.MockVpp.GetMsgID(dataMock.Message.GetMessageName(), dataMock.Message.GetCrcString())
Expect(err).To(BeNil())
reply, err := ctx.MockVpp.ReplyBytes(request, dataMock.Message)
Expect(err).To(BeNil())
return reply, msgID, true
}
}
var err error
replyMsg, id, ok := ctx.MockVpp.ReplyFor(request.MsgName)
if ok {
reply, err = ctx.MockVpp.ReplyBytes(request, replyMsg)
Expect(err).To(BeNil())
msgID = id
prepared = true
} else {
log.DefaultLogger().Warnf("NO REPLY FOR %v FOUND", request.MsgName)
}
return reply, msgID, prepared
})
}