Example #1
0
File: grpc.go Project: IRCody/snap
func newGrpcClient(addr string, port int, timeout time.Duration, typ plugin.PluginType) (*grpcClient, error) {
	conn, err := rpcutil.GetClientConnection(addr, port)
	if err != nil {
		return nil, err
	}
	p := &grpcClient{
		timeout: timeout,
		conn:    conn,
	}

	switch typ {
	case plugin.CollectorPluginType:
		p.collector = rpc.NewCollectorClient(conn)
		p.plugin = p.collector
	case plugin.ProcessorPluginType:
		p.processor = rpc.NewProcessorClient(conn)
		p.plugin = p.processor
	case plugin.PublisherPluginType:
		p.publisher = rpc.NewPublisherClient(conn)
		p.plugin = p.publisher
	default:
		return nil, errors.New(fmt.Sprintf("Invalid plugin type provided %v", typ))
	}

	return p, nil
}
Example #2
0
func TestGRPCCollectorProxy(t *testing.T) {
	port, _, err := startCollectorServer()
	Convey("Start gRPC ", t, func() {
		So(err, ShouldBeNil)
	})

	conn, err := rpcutil.GetClientConnection("127.0.0.1", port)
	Convey("create grpc collector", t, func() {
		So(err, ShouldBeNil)
	})

	client := rpc.NewCollectorClient(conn)
	Convey("create client", t, func() {
		So(err, ShouldBeNil)
		So(client, ShouldNotBeNil)
	})

	getCollectArg := &rpc.CollectMetricsArg{
		Metrics: []*common.Metric{
			{
				LastAdvertisedTime: common.ToTime(time.Now()),
				Namespace: []*common.NamespaceElement{
					{
						Value: "foo",
					},
					{
						Name:  "something",
						Value: "*",
					},
					{
						Value: "bar",
					},
				},
			},
		},
	}
	getCollectRes, err := client.CollectMetrics(context.Background(), getCollectArg)
	Convey("calls CollectMetrics", t, func() {
		So(err, ShouldBeNil)
		So(getCollectRes, ShouldNotBeNil)
		So(len(getCollectRes.Metrics), ShouldEqual, 1)
		So(getCollectRes.Metrics[0].Namespace[1].Value, ShouldEqual, "test")
		So(getCollectRes.Metrics[0].Data.(*common.Metric_StringData).StringData, ShouldEqual, "data")
	})

	getMetricTypesArg := &rpc.GetMetricTypesArg{}
	getMetricTypes, err := client.GetMetricTypes(context.Background(), getMetricTypesArg)
	Convey("calls GetMetricTypes", t, func() {
		So(err, ShouldBeNil)
		So(getMetricTypes, ShouldNotBeNil)
		So(len(getMetricTypes.Metrics), ShouldEqual, 2)
	})

}
Example #3
0
func TestGRPCPluginProxy(t *testing.T) {
	port, mockSession, err := startCollectorServer()
	Convey("Start gRPC ", t, func() {
		So(err, ShouldBeNil)
	})

	conn, err := rpcutil.GetClientConnection("127.0.0.1", port)
	Convey("create grpc collector", t, func() {
		So(err, ShouldBeNil)
	})
	// We use collector client here but all the clients contain "plugin" functions
	// and they are all used from the same base.
	client := rpc.NewCollectorClient(conn)
	pingRes, err := client.Ping(context.Background(), &common.Empty{})
	Convey("call ping", t, func() {
		So(pingRes, ShouldResemble, &rpc.PingReply{})
		So(err, ShouldBeNil)
	})

	killRes, err := client.Kill(context.Background(), &rpc.KillRequest{Reason: "testing"})
	Convey("calls kill", t, func() {
		So(killRes, ShouldResemble, &rpc.KillReply{})
		So(err, ShouldBeNil)
		So(<-mockSession.KillChan(), ShouldEqual, 0)
	})

	getConfigPolicyRes, err := client.GetConfigPolicy(context.Background(), &common.Empty{})
	Convey("calls GetConfigPolicy", t, func() {
		So(err, ShouldBeNil)
		So(getConfigPolicyRes, ShouldNotBeNil)
		// string policy
		So(len(getConfigPolicyRes.StringPolicy), ShouldEqual, 1)
		So(len(getConfigPolicyRes.StringPolicy["one.two.potato"].Rules), ShouldEqual, 2)
		// bool policy
		So(len(getConfigPolicyRes.BoolPolicy["one.two.potato"].Rules), ShouldEqual, 2)
		So(
			getConfigPolicyRes.BoolPolicy["one.two.potato"].Rules["bool_rule_default_true"].Default,
			ShouldEqual,
			true,
		)
		// integer policy
		So(len(getConfigPolicyRes.IntegerPolicy["one.two.potato"].Rules), ShouldEqual, 1)
		So(
			getConfigPolicyRes.IntegerPolicy["one.two.potato"].Rules["integer_rule"].Default,
			ShouldEqual,
			1234,
		)
		So(
			getConfigPolicyRes.IntegerPolicy["one.two.potato"].Rules["integer_rule"].Maximum,
			ShouldEqual,
			9999,
		)
		So(
			getConfigPolicyRes.IntegerPolicy["one.two.potato"].Rules["integer_rule"].Minimum,
			ShouldEqual,
			1000,
		)
		// float policy
		So(len(getConfigPolicyRes.FloatPolicy["one.two.potato"].Rules), ShouldEqual, 1)
		So(
			getConfigPolicyRes.FloatPolicy["one.two.potato"].Rules["float_rule"].Default,
			ShouldEqual,
			0.1234,
		)
		So(
			getConfigPolicyRes.FloatPolicy["one.two.potato"].Rules["float_rule"].Maximum,
			ShouldEqual,
			.9999,
		)
		So(
			getConfigPolicyRes.FloatPolicy["one.two.potato"].Rules["float_rule"].Minimum,
			ShouldEqual,
			0.001,
		)
	})
}