Skip to content

Commit

Permalink
chore: add node info querier test
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno committed Jun 30, 2024
1 parent 67d8787 commit f5f859e
Show file tree
Hide file tree
Showing 4 changed files with 206 additions and 14 deletions.
4 changes: 2 additions & 2 deletions pkg/clients/grpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
)
Expand Down
12 changes: 1 addition & 11 deletions pkg/clients/grpc/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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",
Expand Down Expand Up @@ -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()
Expand All @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion pkg/node_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
202 changes: 202 additions & 0 deletions pkg/queriers/node_info/querier_test.go
Original file line number Diff line number Diff line change
@@ -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)
//}

0 comments on commit f5f859e

Please sign in to comment.