This repository has been archived by the owner on Nov 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 294
/
Copy pathcontrol_grpc_server_test.go
337 lines (308 loc) · 10.8 KB
/
control_grpc_server_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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// +build legacy
/*
http://www.apache.org/licenses/LICENSE-2.0.txt
Copyright 2015 Intel Corporation
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 control
import (
"net"
"testing"
"github.com/intelsdi-x/gomit"
"golang.org/x/net/context"
log "github.com/sirupsen/logrus"
"github.com/intelsdi-x/snap/control/fixtures"
"github.com/intelsdi-x/snap/core"
"github.com/intelsdi-x/snap/core/control_event"
"github.com/intelsdi-x/snap/core/ctypes"
"github.com/intelsdi-x/snap/grpc/common"
"github.com/intelsdi-x/snap/grpc/controlproxy/rpc"
"github.com/intelsdi-x/snap/pkg/rpcutil"
"github.com/intelsdi-x/snap/plugin/helper"
. "github.com/smartystreets/goconvey/convey"
)
type listenPluginLoadEvent struct {
plugin *mockPluginEvent
done chan struct{}
}
func newFoo() *listenPluginLoadEvent {
return &listenPluginLoadEvent{
done: make(chan struct{}),
}
}
func (l *listenPluginLoadEvent) HandleGomitEvent(e gomit.Event) {
switch v := e.Body.(type) {
case *control_event.LoadPluginEvent:
l.done <- struct{}{}
default:
controlLogger.WithFields(log.Fields{
"event:": v.Namespace(),
"_block": "HandleGomit",
}).Info("Unhandled Event")
}
}
// This test is meant to cover the grpc implementation of the subset of control
// features that scheduler uses. It is not intended to test the control features
// themselves, only that we are correctly passing data over grpc and correctly
// passing success/errors.
func TestGRPCServerScheduler(t *testing.T) {
l, _ := net.Listen("tcp", ":0")
l.Close()
cfg := GetDefaultConfig()
cfg.ListenPort = l.Addr().(*net.TCPAddr).Port
c := New(cfg)
err := c.Start()
lpe := newFoo()
c.eventManager.RegisterHandler("Control.PluginLoaded", lpe)
Convey("Starting control_proxy server/client", t, func() {
Convey("So err should be nil", func() {
So(err, ShouldBeNil)
})
})
// Load 3 plugins
// collector -- mock
// processor -- passthru
// publisher -- file
mock, err := core.NewRequestedPlugin(fixtures.PluginPathMock1, GetDefaultConfig().TempDirPath, nil)
if err != nil {
log.Fatal(err)
}
_, serr := c.Load(mock)
Convey("Loading mock collector", t, func() {
Convey("should not error", func() {
So(serr, ShouldBeNil)
})
})
<-lpe.done
passthru, err := core.NewRequestedPlugin(helper.PluginFilePath("snap-plugin-processor-passthru"), GetDefaultConfig().TempDirPath, nil)
if err != nil {
log.Fatal(err)
}
catalogedPassthru, serr := c.Load(passthru)
Convey("Loading passthru processor", t, func() {
Convey("should not error", func() {
So(serr, ShouldBeNil)
})
})
subscribedPassThruPlugin := subscribedPlugin{
name: catalogedPassthru.Name(),
version: catalogedPassthru.Version(),
typeName: catalogedPassthru.TypeName(),
}
<-lpe.done
filepub, err := core.NewRequestedPlugin(helper.PluginFilePath("snap-plugin-publisher-mock-file"), GetDefaultConfig().TempDirPath, nil)
if err != nil {
log.Fatal(err)
}
catalogedFile, serr := c.Load(filepub)
Convey("Loading file publisher", t, func() {
Convey("should not error", func() {
So(serr, ShouldBeNil)
})
})
subscribedFilePlugin := subscribedPlugin{
name: catalogedFile.Name(),
version: catalogedFile.Version(),
typeName: catalogedFile.TypeName(),
}
<-lpe.done
conn, err := rpcutil.GetClientConnection(context.Background(), c.Config.ListenAddr, c.Config.ListenPort)
Convey("Creating an rpc connection", t, func() {
Convey("Should not error", func() {
So(err, ShouldBeNil)
})
})
client := rpc.NewMetricManagerClient(conn)
Convey("Creating an RPC client to control RPC server", t, func() {
Convey("And a client should exist", func() {
So(client, ShouldNotBeNil)
})
})
// Verify that validate deps is properly passing through errors
Convey("Validating Deps", t, func() {
Convey("Should Fail if given invalid info", func() {
req := &rpc.ValidateDepsRequest{
Metrics: common.NewMetrics([]core.Metric{fixtures.InvalidMetric}),
Plugins: common.ToSubPluginsMsg([]core.SubscribedPlugin{}),
}
reply, err := client.ValidateDeps(context.Background(), req)
// we don't expect rpc errors
So(err, ShouldBeNil)
So(len(reply.Errors), ShouldNotEqual, 0)
})
Convey("with valid metrics", func() {
req := &rpc.ValidateDepsRequest{
Metrics: common.NewMetrics([]core.Metric{fixtures.ValidMetric}),
Plugins: common.ToSubPluginsMsg([]core.SubscribedPlugin{}),
}
reply, err := client.ValidateDeps(context.Background(), req)
// we don't expect rpc errors
So(err, ShouldBeNil)
So(len(reply.Errors), ShouldNotEqual, 0)
})
})
//Subscribe Deps: valid/invalid
Convey("SubscribeDeps", t, func() {
Convey("Should Error with invalid inputs", func() {
req := &rpc.SubscribeDepsRequest{
Requested: []*common.Metric{&common.Metric{Namespace: common.ToNamespace(fixtures.InvalidMetric.Namespace()), Version: int64(fixtures.InvalidMetric.Version())}},
Plugins: common.ToSubPluginsMsg([]core.SubscribedPlugin{subscribedFilePlugin, subscribedPassThruPlugin}),
TaskId: "my-snowflake-id",
}
reply, err := client.SubscribeDeps(context.Background(), req)
// we don't expect rpc errors
So(err, ShouldBeNil)
So(len(reply.Errors), ShouldNotEqual, 0)
So(reply.Errors[0].ErrorString, ShouldResemble, "Metric not found: /this/is/invalid (version: 1000)")
})
Convey("Should not error with valid inputs", func() {
req := &rpc.SubscribeDepsRequest{
Requested: []*common.Metric{&common.Metric{Namespace: common.ToNamespace(fixtures.ValidMetric.Namespace()), Version: int64(fixtures.ValidMetric.Version())}},
Plugins: common.ToSubPluginsMsg([]core.SubscribedPlugin{}),
TaskId: "my-snowflake-valid",
}
reply, err := client.SubscribeDeps(context.Background(), req)
// we don't expect rpc errors
So(err, ShouldBeNil)
So(len(reply.Errors), ShouldEqual, 0)
})
})
//our returned metrics
var mts []core.Metric
//collect
Convey("CollectMetrics", t, func() {
req := &rpc.SubscribeDepsRequest{
Requested: []*common.Metric{&common.Metric{Namespace: common.ToNamespace(fixtures.ValidMetric.Namespace()), Version: int64(fixtures.ValidMetric.Version())}},
Plugins: common.ToSubPluginsMsg([]core.SubscribedPlugin{
subscribedPassThruPlugin,
subscribedFilePlugin,
},
),
TaskId: "my-snowflake-id",
}
_, err := client.SubscribeDeps(context.Background(), req)
So(err, ShouldBeNil)
Convey("should error with invalid inputs", func() {
req := &rpc.CollectMetricsRequest{
TaskID: "my-fake-snowflake-id",
}
reply, err := client.CollectMetrics(context.Background(), req)
So(err, ShouldBeNil)
So(len(reply.Errors), ShouldNotEqual, 0)
})
Convey("should not error with valid inputs", func() {
req := &rpc.CollectMetricsRequest{
TaskID: "my-snowflake-valid",
}
reply, err := client.CollectMetrics(context.Background(), req)
So(err, ShouldBeNil)
So(len(reply.Errors), ShouldEqual, 0)
So(reply.Metrics[0].Namespace, ShouldResemble, common.ToNamespace(fixtures.ValidMetric.Namespace()))
// Used in a later test as metrics to be passed to processor
mts = common.ToCoreMetrics(reply.Metrics)
})
})
//process
Convey("ProcessMetrics", t, func() {
req := &rpc.SubscribeDepsRequest{
Requested: []*common.Metric{&common.Metric{Namespace: common.ToNamespace(fixtures.ValidMetric.Namespace()), Version: int64(fixtures.ValidMetric.Version())}},
Plugins: common.ToSubPluginsMsg([]core.SubscribedPlugin{
subscribedPassThruPlugin,
subscribedFilePlugin,
},
),
TaskId: "my-snowflake-id",
}
_, err := client.SubscribeDeps(context.Background(), req)
So(err, ShouldBeNil)
Convey("should error with invalid inputs", func() {
req := &rpc.PubProcMetricsRequest{
Metrics: common.NewMetrics([]core.Metric{fixtures.ValidMetric}),
PluginName: "passthru-invalid",
PluginVersion: 1,
TaskId: "my-snowflake-id",
Config: common.ToConfigMap(map[string]ctypes.ConfigValue{}),
}
reply, err := client.ProcessMetrics(context.Background(), req)
// we don't expect rpc errors
So(err, ShouldBeNil)
So(len(reply.Errors), ShouldNotEqual, 0)
// content to pass to publisher
})
Convey("should not error with valid inputs", func() {
req := &rpc.PubProcMetricsRequest{
Metrics: common.NewMetrics(mts),
PluginName: "passthru",
PluginVersion: 1,
TaskId: "my-snowflake-id",
Config: common.ToConfigMap(map[string]ctypes.ConfigValue{}),
}
reply, err := client.ProcessMetrics(context.Background(), req)
// we don't expect rpc errors
So(err, ShouldBeNil)
So(len(reply.Errors), ShouldEqual, 0)
})
})
//publishmetrics
Convey("PublishMetrics", t, func() {
req := &rpc.SubscribeDepsRequest{
Requested: []*common.Metric{&common.Metric{Namespace: common.ToNamespace(fixtures.ValidMetric.Namespace()), Version: int64(fixtures.ValidMetric.Version())}},
Plugins: common.ToSubPluginsMsg([]core.SubscribedPlugin{
subscribedPassThruPlugin,
subscribedFilePlugin,
},
),
TaskId: "my-snowflake-id",
}
_, err := client.SubscribeDeps(context.Background(), req)
So(err, ShouldBeNil)
Convey("Should error with invalid inputs", func() {
req := &rpc.PubProcMetricsRequest{
Metrics: common.NewMetrics([]core.Metric{fixtures.ValidMetric}),
PluginName: "mock-file-invalid",
PluginVersion: 3,
TaskId: "my-snowflake-id",
Config: common.ToConfigMap(map[string]ctypes.ConfigValue{}),
}
reply, err := client.PublishMetrics(context.Background(), req)
// we don't expect rpc errors
So(err, ShouldBeNil)
So(len(reply.Errors), ShouldNotEqual, 0)
})
// Publish only returns no errors on success
Convey("should not error with valid inputs", func() {
config := make(map[string]ctypes.ConfigValue)
config["file"] = ctypes.ConfigValueStr{Value: "/tmp/grpcservertest.snap"}
req := &rpc.PubProcMetricsRequest{
Metrics: common.NewMetrics([]core.Metric{fixtures.ValidMetric}),
PluginName: "mock-file",
PluginVersion: 3,
TaskId: "my-snowflake-id",
Config: common.ToConfigMap(config),
}
reply, err := client.PublishMetrics(context.Background(), req)
// we don't expect rpc errors
So(err, ShouldBeNil)
So(len(reply.Errors), ShouldEqual, 0)
})
})
}
func equal(a []string, b []string) bool {
if len(a) != len(b) {
return false
}
for idx := range a {
if a[idx] != b[idx] {
return false
}
}
return true
}