diff --git a/pkg/clients/grpc/client.go b/pkg/clients/grpc/client.go index 1c17639..145c762 100644 --- a/pkg/clients/grpc/client.go +++ b/pkg/clients/grpc/client.go @@ -25,11 +25,11 @@ type Client struct { Tracer trace.Tracer } -func NewClient(config config.NodeConfig, logger zerolog.Logger, tracer trace.Tracer) *Client { +func NewClient(config config.GrpcConfig, logger zerolog.Logger, tracer trace.Tracer) *Client { grpcLogger := logger.With().Str("component", "grpc").Logger() grpcConn, _ := grpc.NewClient( - config.GrpcConfig.Address, + config.Address, grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithTimeout(5*time.Second), ) diff --git a/pkg/clients/grpc/client_test.go b/pkg/clients/grpc/client_test.go index 4922846..702e01e 100644 --- a/pkg/clients/grpc/client_test.go +++ b/pkg/clients/grpc/client_test.go @@ -25,15 +25,13 @@ func TestGrpcClientInit(t *testing.T) { logger := loggerPkg.GetDefaultLogger() tracer := tracing.InitNoopTracer() - config := configPkg.NodeConfig{GrpcConfig: configPkg.GrpcConfig{Address: "localhost:9090"}} + config := configPkg.GrpcConfig{Address: "localhost:9090"} client := NewClient(config, *logger, tracer) assert.NotNil(t, client) } func TestGrpcClientNodeConfigFail(t *testing.T) { t.Parallel() - grpcmock.MockServer() - _, d := grpcmock.MockServerWithBufConn()(t) logger := loggerPkg.GetDefaultLogger() @@ -59,8 +57,6 @@ func TestGrpcClientNodeConfigFail(t *testing.T) { func TestGrpcClientNodeConfigNotImplemented(t *testing.T) { t.Parallel() - grpcmock.MockServer() - _, d := grpcmock.MockServerWithBufConn()(t) logger := loggerPkg.GetDefaultLogger() @@ -86,8 +82,6 @@ func TestGrpcClientNodeConfigNotImplemented(t *testing.T) { func TestGrpcClientNodeConfigOk(t *testing.T) { t.Parallel() - grpcmock.MockServer() - _, d := grpcmock.MockServerWithBufConn( grpcmock.RegisterServiceFromMethods(service.Method{ ServiceName: "cosmos.base.node.v1beta1.Service", @@ -126,8 +120,6 @@ func TestGrpcClientNodeConfigOk(t *testing.T) { func TestGrpcClientNodeInfoFail(t *testing.T) { t.Parallel() - grpcmock.MockServer() - _, d := grpcmock.MockServerWithBufConn()(t) logger := loggerPkg.GetDefaultLogger() @@ -153,8 +145,6 @@ func TestGrpcClientNodeInfoFail(t *testing.T) { func TestGrpcClientNodeInfoOk(t *testing.T) { t.Parallel() - grpcmock.MockServer() - _, d := grpcmock.MockServerWithBufConn( grpcmock.RegisterServiceFromMethods(service.Method{ ServiceName: "cosmos.base.tendermint.v1beta1.Service", diff --git a/pkg/node_handler.go b/pkg/node_handler.go index 17d676c..167e090 100644 --- a/pkg/node_handler.go +++ b/pkg/node_handler.go @@ -55,7 +55,7 @@ func NewNodeHandler( } if config.GrpcConfig.Enabled.Bool { - grpc = grpcPkg.NewClient(config, appLogger, tracer) + grpc = grpcPkg.NewClient(config.GrpcConfig, appLogger, tracer) } gitClient := git.GetClient(config.GitConfig, appLogger, tracer) diff --git a/pkg/queriers/node_info/querier_test.go b/pkg/queriers/node_info/querier_test.go new file mode 100644 index 0000000..a7d96e2 --- /dev/null +++ b/pkg/queriers/node_info/querier_test.go @@ -0,0 +1,202 @@ +package node_stats + +import ( + "context" + grpcPkg "main/pkg/clients/grpc" + configPkg "main/pkg/config" + loggerPkg "main/pkg/logger" + "main/pkg/tracing" + "testing" + + cmtTypes "github.com/cosmos/cosmos-sdk/client/grpc/cmtservice" + "github.com/stretchr/testify/require" + "go.nhat.io/grpcmock" + "go.nhat.io/grpcmock/service" + "google.golang.org/grpc" + "google.golang.org/grpc/credentials/insecure" + + "github.com/stretchr/testify/assert" +) + +func TestNodeInfoQuerierBase(t *testing.T) { + t.Parallel() + + config := configPkg.GrpcConfig{ + Address: "localhost:9090", + } + logger := loggerPkg.GetNopLogger() + tracer := tracing.InitNoopTracer() + client := grpcPkg.NewClient(config, *logger, tracer) + querier := NewQuerier(*logger, client, tracer) + assert.True(t, querier.Enabled()) + assert.Equal(t, "node-info-querier", querier.Name()) +} +func TestNodeInfoQuerierFail(t *testing.T) { + t.Parallel() + + _, d := grpcmock.MockServerWithBufConn()(t) + + logger := loggerPkg.GetNopLogger() + tracer := tracing.InitNoopTracer() + grpcConn, err := grpc.NewClient( + "invalid", + grpc.WithContextDialer(d), + grpc.WithTransportCredentials(insecure.NewCredentials()), + ) + require.NoError(t, err) + client := &grpcPkg.Client{ + Logger: *logger, + Client: grpcConn, + Tracer: tracer, + } + + querier := NewQuerier(*logger, client, tracer) + metrics, queryInfos := querier.Get(context.Background()) + assert.Len(t, queryInfos, 1) + assert.False(t, queryInfos[0].Success) + assert.Empty(t, metrics) +} + +func TestNodeInfoQuerierError(t *testing.T) { + t.Parallel() + + _, d := grpcmock.MockServerWithBufConn()(t) + + logger := loggerPkg.GetNopLogger() + tracer := tracing.InitNoopTracer() + grpcConn, err := grpc.NewClient( + "invalid", + grpc.WithContextDialer(d), + grpc.WithTransportCredentials(insecure.NewCredentials()), + ) + require.NoError(t, err) + client := &grpcPkg.Client{ + Logger: *logger, + Client: grpcConn, + Tracer: tracer, + } + + querier := NewQuerier(*logger, client, tracer) + metrics, queryInfos := querier.Get(context.Background()) + assert.Len(t, queryInfos, 1) + assert.False(t, queryInfos[0].Success) + assert.Empty(t, metrics) +} + +func TestNodeInfoQuerierOk(t *testing.T) { + t.Parallel() + + _, d := grpcmock.MockServerWithBufConn( + grpcmock.RegisterServiceFromMethods(service.Method{ + ServiceName: "cosmos.base.tendermint.v1beta1.Service", + MethodName: "GetNodeInfo", + MethodType: service.TypeUnary, + Input: &cmtTypes.GetNodeInfoRequest{}, + Output: &cmtTypes.GetNodeInfoResponse{}, + }), + func(s *grpcmock.Server) { + s.ExpectUnary("cosmos.base.tendermint.v1beta1.Service/GetNodeInfo"). + Return(&cmtTypes.GetNodeInfoResponse{ + ApplicationVersion: &cmtTypes.VersionInfo{ + Name: "appd", + AppName: "app", + Version: "1.2.3", + CosmosSdkVersion: "0.50.7", + GoVersion: "1.22.3", + BuildTags: "netgo,ledger", + GitCommit: "123abc", + }, + }) + }, + )(t) + + logger := loggerPkg.GetNopLogger() + tracer := tracing.InitNoopTracer() + grpcConn, err := grpc.NewClient( + "localhost:9090", + grpc.WithContextDialer(d), + grpc.WithTransportCredentials(insecure.NewCredentials()), + ) + require.NoError(t, err) + client := &grpcPkg.Client{ + Logger: *logger, + Client: grpcConn, + Tracer: tracer, + } + + querier := NewQuerier(*logger, client, tracer) + metrics, queryInfos := querier.Get(context.Background()) + assert.Len(t, queryInfos, 1) + assert.True(t, queryInfos[0].Success) + assert.Len(t, metrics, 3) + + cosmosSdkVersion := metrics[0] + assert.Equal(t, map[string]string{ + "version": "0.50.7", + }, cosmosSdkVersion.Labels) + assert.InDelta(t, 1, cosmosSdkVersion.Value, 0.01) + + appInfo := metrics[1] + assert.Equal(t, map[string]string{ + "name": "appd", + "app_name": "app", + "git_commit": "123abc", + "version": "1.2.3", + }, appInfo.Labels) + assert.InDelta(t, 1, appInfo.Value, 0.01) + + goVersion := metrics[2] + assert.Equal(t, map[string]string{ + "tags": "netgo,ledger", + "version": "1.22.3", + }, goVersion.Labels) + assert.InDelta(t, 1, goVersion.Value, 0.01) +} + +// func TestNodeInfoQuerierOk(t *testing.T) { +// httpmock.Activate() +// defer httpmock.DeactivateAndReset() +// +// httpmock.RegisterResponder( +// "GET", +// "https://example.com/status", +// httpmock.NewBytesResponder(200, assets.GetBytesOrPanic("status.json")), +// ) +// +// config := configPkg.TendermintConfig{ +// Address: "https://example.com", +// } +// logger := loggerPkg.GetNopLogger()() +// tracer := tracing.InitNoopTracer() +// client := tendermint.NewRPC(config, *logger, tracer) +// querier := NewQuerier(*logger, client, tracer) +// +// metrics, queryInfos := querier.Get(context.Background()) +// assert.Len(t, queryInfos, 1) +// assert.True(t, queryInfos[0].Success) +// assert.Len(t, metrics, 5) +// +// catchingUp := metrics[0] +// assert.Empty(t, catchingUp.Labels) +// assert.Zero(t, catchingUp.Value) +// +// timeSinceLatest := metrics[1] +// assert.Empty(t, timeSinceLatest.Labels) +// +// nodeInfo := metrics[2] +// assert.Equal(t, map[string]string{ +// "chain": "cosmoshub-4", +// "moniker": "freak12techno", +// }, nodeInfo.Labels) +// assert.InDelta(t, 1, nodeInfo.Value, 0.01) +// +// tendermintVersion := metrics[3] +// assert.Equal(t, map[string]string{ +// "version": "0.37.6", +// }, tendermintVersion.Labels) +// assert.InDelta(t, 1, tendermintVersion.Value, 0.01) +// +// votingPower := metrics[4] +// assert.Empty(t, votingPower.Labels) +// assert.Zero(t, votingPower.Value) +//}