-
Notifications
You must be signed in to change notification settings - Fork 6
/
xport_grpc_test.go
284 lines (232 loc) · 6.69 KB
/
xport_grpc_test.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
//
// February 2016, cisco
//
// Copyright (c) 2016 by cisco Systdialin, Inc.
// All rights reserved.
//
//
package main
import (
"fmt"
"math/rand"
"net"
"testing"
"time"
telem "github.com/cisco/bigmuddy-network-telemetry-proto/proto_go"
dialin "github.com/cisco/bigmuddy-network-telemetry-proto/proto_go/mdt_grpc_dialin"
"github.com/golang/protobuf/proto"
log "github.com/sirupsen/logrus"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)
type grpcTestUserPasswordCollector struct {
}
const (
grpcServerPort = ":56789"
GRPCTESTMSGS = 10000
GRPCCLIENTS = 1
)
var dummyUsername = "root"
var dummyPassword = "lab"
func (c *grpcTestUserPasswordCollector) handleConfig(
nc nodeConfig, name string, server string) error {
return nil
}
func (c *grpcTestUserPasswordCollector) getUP() (
string, string, error) {
return dummyUsername, dummyPassword, nil
}
func grpcTestUPCollectorFactory() userPasswordCollector {
return &grpcTestUserPasswordCollector{}
}
func TestGRPCRun(t *testing.T) {
//
// Setup username and password for test
grpcUPCollector := grpcTestUPCollectorFactory()
name := "xrv9k-grpc-server"
server := grpcServerPort
_, _, err := grpcUPCollector.getUP()
if err != nil {
t.Error("failed to collect username/password")
}
cleanSubs := extractSubscriptions(" counters, datarates, 132, nemo, sub1, sub2, sub3, sub4")
ctrlChan := make(chan *ctrlMsg)
dataChan := make(chan dataMsg, 10)
var dataChans = make([]chan<- dataMsg, 0)
dataChans = append(dataChans, dataChan)
tls := false
tls_pem := ""
tls_key := ""
tls_servername := ""
block := &grpcBlock{
name: name,
server: &grpcRemoteServer{
server: server,
subscriptions: cleanSubs,
auth: grpcUPCollector,
reqID: rand.Int63(),
},
encap: ENCODING_GPB,
encodingRequest: ENCODING_GPB_KV,
logData: true,
ctrlChan: ctrlChan,
dataChans: dataChans,
childrenDone: make(chan struct{}),
tls: tls,
tls_pem: tls_pem,
tls_key: tls_key,
tls_servername: tls_servername,
}
//
// We will fail to connect. Test retry
log.Debug("GRPC TEST RETRY: Transport errors expected here:")
log.Debug("GRPC TEST RETRY: ==============================================")
block.server.linkServerBlock(block)
go block.run()
time.Sleep(1 * time.Second)
//
// Now run local server and consume all
go runTestGRPCServer(t)
var i int
for i = 0; i < GRPCTESTMSGS*GRPCCLIENTS*len(cleanSubs); i++ {
<-dataChan
}
log.Debug(" END ==============================================")
log.Debug("GRPCTEST: exited datachannel after events\n", i)
respChan := make(chan *ctrlMsg)
request := &ctrlMsg{
id: SHUTDOWN,
respChan: respChan,
}
//
// and shutdown in the face of retry.
ctrlChan <- request
// Wait for ACK
ack := <-respChan
if ack.id != ACK {
t.Error("failed to recieve acknowledgement indicating shutdown complete")
}
//
// Now run local server and consume half then cancel
ctrlChan2 := make(chan *ctrlMsg)
dataChan2 := make(chan dataMsg, 10)
var dataChans2 = make([]chan<- dataMsg, 0)
dataChans2 = append(dataChans2, dataChan2)
block2 := &grpcBlock{
name: "client2TestForCancel",
server: &grpcRemoteServer{
server: server,
subscriptions: cleanSubs,
auth: grpcUPCollector,
reqID: rand.Int63(),
},
encap: ENCODING_GPB,
encodingRequest: ENCODING_GPB_KV,
logData: true,
ctrlChan: ctrlChan2,
dataChans: dataChans2,
childrenDone: make(chan struct{}),
tls: tls,
tls_pem: tls_pem,
tls_servername: tls_servername,
}
block2.server.linkServerBlock(block2)
go block2.run()
for i = 0; i < (GRPCTESTMSGS/2)*GRPCCLIENTS*len(cleanSubs); i++ {
<-dataChan2
}
log.Debug("GRPCTEST: exited datachannel after %d events"+
" (partial, as expect), test cancel\n", i)
respChan = make(chan *ctrlMsg)
request = &ctrlMsg{
id: SHUTDOWN,
respChan: respChan,
}
//
// and shutdown in the face of retry.
ctrlChan2 <- request
// Wait for ACK
ack = <-respChan
if ack.id != ACK {
t.Error("failed to recieve acknowledgement indicating shutdown complete")
}
}
type gRPCConfigOperServer struct{}
func (s *gRPCConfigOperServer) GetConfig(*dialin.ConfigGetArgs,
dialin.GRPCConfigOper_GetConfigServer) error {
return nil
}
func (s *gRPCConfigOperServer) MergeConfig(context.Context, *dialin.ConfigArgs) (*dialin.ConfigReply, error) {
return nil, nil
}
func (s *gRPCConfigOperServer) DeleteConfig(context.Context, *dialin.ConfigArgs) (*dialin.ConfigReply, error) {
return nil, nil
}
func (s *gRPCConfigOperServer) ReplaceConfig(context.Context, *dialin.ConfigArgs) (*dialin.ConfigReply, error) {
return nil, nil
}
func (s *gRPCConfigOperServer) CliConfig(context.Context, *dialin.CliConfigArgs) (*dialin.CliConfigReply, error) {
return nil, nil
}
func (s *gRPCConfigOperServer) CommitReplace(context.Context, *dialin.CommitReplaceArgs) (*dialin.CommitReplaceReply, error) {
return nil, nil
}
func (s *gRPCConfigOperServer) CommitConfig(context.Context, *dialin.CommitArgs) (*dialin.CommitReply, error) {
return nil, nil
}
func (s *gRPCConfigOperServer) ConfigDiscardChanges(context.Context, *dialin.DiscardChangesArgs) (*dialin.DiscardChangesReply, error) {
return nil, nil
}
func (s *gRPCConfigOperServer) GetOper(*dialin.GetOperArgs, dialin.GRPCConfigOper_GetOperServer) error {
return nil
}
//
// Create subs handler.
func (s *gRPCConfigOperServer) CreateSubs(
reqArgs *dialin.CreateSubsArgs,
stream dialin.GRPCConfigOper_CreateSubsServer) error {
ctx := stream.Context()
md, _ := metadata.FromIncomingContext(ctx)
if md["username"][0] != dummyUsername {
return fmt.Errorf("Bad username")
}
if md["password"][0] != dummyPassword {
return fmt.Errorf("Bad password")
}
var collectionId uint64
collectionId = 100
basePath := "RootOper.ABC.DEF"
var msg telem.Telemetry
if reqArgs.Encode == GRPC_ENCODE_GPBKV {
msg = telem.Telemetry{
CollectionId: collectionId,
EncodingPath: basePath,
CollectionStartTime: uint64(time.Now().Unix()),
CollectionEndTime: uint64(time.Now().Unix()),
}
} else {
return fmt.Errorf("Requesting unexpected encoding")
}
msgstream, _ := proto.Marshal(&msg)
for i := 0; i < GRPCTESTMSGS; i++ {
reply := dialin.CreateSubsReply{
ResReqId: reqArgs.ReqId,
Data: msgstream,
Errors: "",
}
if err := stream.Send(&reply); err != nil {
return err
}
}
return nil
}
func runTestGRPCServer(t *testing.T) {
lis, err := net.Listen("tcp", grpcServerPort)
if err != nil {
t.Logf("failed to listen: %v", err)
}
s := grpc.NewServer()
dialin.RegisterGRPCConfigOperServer(s, new(gRPCConfigOperServer))
go s.Serve(lis)
}