Skip to content

Commit

Permalink
chore: add cosmovisor querier test (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno authored Jun 30, 2024
1 parent 87f4f0d commit 396b3f0
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 80 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@ lint:
test:
go test -coverpkg=./... -coverprofile cover.out ./...

test-verbose:
go test -coverpkg=./... -coverprofile cover.out -v ./...


coverage:
go tool cover -html=cover.out
4 changes: 2 additions & 2 deletions pkg/clients/cosmovisor/cosmovisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ type Cosmovisor struct {
}

func NewCosmovisor(
config config.NodeConfig,
config config.CosmovisorConfig,
logger zerolog.Logger,
tracer trace.Tracer,
) *Cosmovisor {
return &Cosmovisor{
Logger: logger.With().Str("component", "cosmovisor").Logger(),
Config: config.CosmovisorConfig,
Config: config,
Tracer: tracer,
CommandExecutor: &exec.NativeCommandExecutor{},
Filesystem: &fs.OsFS{},
Expand Down
132 changes: 55 additions & 77 deletions pkg/clients/cosmovisor/cosmovisor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@ import (
func TestCosmovisorGetCosmovisorVersionFail(t *testing.T) {
t.Parallel()

config := configPkg.NodeConfig{
CosmovisorConfig: configPkg.CosmovisorConfig{
Enabled: null.BoolFrom(true),
ChainBinaryName: "gaiad",
ChainFolder: "/home/validator/.gaia",
CosmovisorPath: "/home/validator/go/bin/cosmovisor",
},
config := configPkg.CosmovisorConfig{
Enabled: null.BoolFrom(true),
ChainBinaryName: "gaiad",
ChainFolder: "/home/validator/.gaia",
CosmovisorPath: "/home/validator/go/bin/cosmovisor",
}
logger := loggerPkg.GetNopLogger()
tracer := tracing.InitNoopTracer()
Expand All @@ -40,13 +38,11 @@ func TestCosmovisorGetCosmovisorVersionFail(t *testing.T) {
func TestCosmovisorGetCosmovisorVersionInvalid(t *testing.T) {
t.Parallel()

config := configPkg.NodeConfig{
CosmovisorConfig: configPkg.CosmovisorConfig{
Enabled: null.BoolFrom(true),
ChainBinaryName: "gaiad",
ChainFolder: "/home/validator/.gaia",
CosmovisorPath: "/home/validator/go/bin/cosmovisor",
},
config := configPkg.CosmovisorConfig{
Enabled: null.BoolFrom(true),
ChainBinaryName: "gaiad",
ChainFolder: "/home/validator/.gaia",
CosmovisorPath: "/home/validator/go/bin/cosmovisor",
}
logger := loggerPkg.GetNopLogger()
tracer := tracing.InitNoopTracer()
Expand All @@ -65,13 +61,11 @@ func TestCosmovisorGetCosmovisorVersionInvalid(t *testing.T) {
func TestCosmovisorGetCosmovisorVersionValid(t *testing.T) {
t.Parallel()

config := configPkg.NodeConfig{
CosmovisorConfig: configPkg.CosmovisorConfig{
Enabled: null.BoolFrom(true),
ChainBinaryName: "gaiad",
ChainFolder: "/home/validator/.gaia",
CosmovisorPath: "/home/validator/go/bin/cosmovisor",
},
config := configPkg.CosmovisorConfig{
Enabled: null.BoolFrom(true),
ChainBinaryName: "gaiad",
ChainFolder: "/home/validator/.gaia",
CosmovisorPath: "/home/validator/go/bin/cosmovisor",
}
logger := loggerPkg.GetNopLogger()
tracer := tracing.InitNoopTracer()
Expand All @@ -89,13 +83,11 @@ func TestCosmovisorGetCosmovisorVersionValid(t *testing.T) {
func TestCosmovisorGetAppVersionFail(t *testing.T) {
t.Parallel()

config := configPkg.NodeConfig{
CosmovisorConfig: configPkg.CosmovisorConfig{
Enabled: null.BoolFrom(true),
ChainBinaryName: "gaiad",
ChainFolder: "/home/validator/.gaia",
CosmovisorPath: "/home/validator/go/bin/cosmovisor",
},
config := configPkg.CosmovisorConfig{
Enabled: null.BoolFrom(true),
ChainBinaryName: "gaiad",
ChainFolder: "/home/validator/.gaia",
CosmovisorPath: "/home/validator/go/bin/cosmovisor",
}
logger := loggerPkg.GetNopLogger()
tracer := tracing.InitNoopTracer()
Expand All @@ -111,13 +103,11 @@ func TestCosmovisorGetAppVersionFail(t *testing.T) {
func TestCosmovisorGetAppVersionInvalid(t *testing.T) {
t.Parallel()

config := configPkg.NodeConfig{
CosmovisorConfig: configPkg.CosmovisorConfig{
Enabled: null.BoolFrom(true),
ChainBinaryName: "gaiad",
ChainFolder: "/home/validator/.gaia",
CosmovisorPath: "/home/validator/go/bin/cosmovisor",
},
config := configPkg.CosmovisorConfig{
Enabled: null.BoolFrom(true),
ChainBinaryName: "gaiad",
ChainFolder: "/home/validator/.gaia",
CosmovisorPath: "/home/validator/go/bin/cosmovisor",
}
logger := loggerPkg.GetNopLogger()
tracer := tracing.InitNoopTracer()
Expand All @@ -136,13 +126,11 @@ func TestCosmovisorGetAppVersionInvalid(t *testing.T) {
func TestCosmovisorGetAppVersionValid(t *testing.T) {
t.Parallel()

config := configPkg.NodeConfig{
CosmovisorConfig: configPkg.CosmovisorConfig{
Enabled: null.BoolFrom(true),
ChainBinaryName: "gaiad",
ChainFolder: "/home/validator/.gaia",
CosmovisorPath: "/home/validator/go/bin/cosmovisor",
},
config := configPkg.CosmovisorConfig{
Enabled: null.BoolFrom(true),
ChainBinaryName: "gaiad",
ChainFolder: "/home/validator/.gaia",
CosmovisorPath: "/home/validator/go/bin/cosmovisor",
}
logger := loggerPkg.GetNopLogger()
tracer := tracing.InitNoopTracer()
Expand All @@ -160,13 +148,11 @@ func TestCosmovisorGetAppVersionValid(t *testing.T) {
func TestCosmovisorGetUpgradesFail(t *testing.T) {
t.Parallel()

config := configPkg.NodeConfig{
CosmovisorConfig: configPkg.CosmovisorConfig{
Enabled: null.BoolFrom(true),
ChainBinaryName: "gaiad",
ChainFolder: "/home/validator/.gaia",
CosmovisorPath: "/home/validator/go/bin/cosmovisor",
},
config := configPkg.CosmovisorConfig{
Enabled: null.BoolFrom(true),
ChainBinaryName: "gaiad",
ChainFolder: "/home/validator/.gaia",
CosmovisorPath: "/home/validator/go/bin/cosmovisor",
}
logger := loggerPkg.GetNopLogger()
tracer := tracing.InitNoopTracer()
Expand All @@ -182,13 +168,11 @@ func TestCosmovisorGetUpgradesFail(t *testing.T) {
func TestCosmovisorGetUpgradesIsNotDir(t *testing.T) {
t.Parallel()

config := configPkg.NodeConfig{
CosmovisorConfig: configPkg.CosmovisorConfig{
Enabled: null.BoolFrom(true),
ChainBinaryName: "gaiad",
ChainFolder: "",
CosmovisorPath: "/home/validator/go/bin/cosmovisor",
},
config := configPkg.CosmovisorConfig{
Enabled: null.BoolFrom(true),
ChainBinaryName: "gaiad",
ChainFolder: "",
CosmovisorPath: "/home/validator/go/bin/cosmovisor",
}
logger := loggerPkg.GetNopLogger()
tracer := tracing.InitNoopTracer()
Expand All @@ -205,13 +189,11 @@ func TestCosmovisorGetUpgradesIsNotDir(t *testing.T) {
func TestCosmovisorGetUpgradesGetFileError(t *testing.T) {
t.Parallel()

config := configPkg.NodeConfig{
CosmovisorConfig: configPkg.CosmovisorConfig{
Enabled: null.BoolFrom(true),
ChainBinaryName: "gaiad",
ChainFolder: "",
CosmovisorPath: "/home/validator/go/bin/cosmovisor",
},
config := configPkg.CosmovisorConfig{
Enabled: null.BoolFrom(true),
ChainBinaryName: "gaiad",
ChainFolder: "",
CosmovisorPath: "/home/validator/go/bin/cosmovisor",
}
logger := loggerPkg.GetNopLogger()
tracer := tracing.InitNoopTracer()
Expand All @@ -228,13 +210,11 @@ func TestCosmovisorGetUpgradesGetFileError(t *testing.T) {
func TestCosmovisorGetUpgradesGetFileNotFound(t *testing.T) {
t.Parallel()

config := configPkg.NodeConfig{
CosmovisorConfig: configPkg.CosmovisorConfig{
Enabled: null.BoolFrom(true),
ChainBinaryName: "gaiad",
ChainFolder: "",
CosmovisorPath: "/home/validator/go/bin/cosmovisor",
},
config := configPkg.CosmovisorConfig{
Enabled: null.BoolFrom(true),
ChainBinaryName: "gaiad",
ChainFolder: "",
CosmovisorPath: "/home/validator/go/bin/cosmovisor",
}
logger := loggerPkg.GetNopLogger()
tracer := tracing.InitNoopTracer()
Expand All @@ -251,13 +231,11 @@ func TestCosmovisorGetUpgradesGetFileNotFound(t *testing.T) {
func TestCosmovisorGetUpgradesGetFileOk(t *testing.T) {
t.Parallel()

config := configPkg.NodeConfig{
CosmovisorConfig: configPkg.CosmovisorConfig{
Enabled: null.BoolFrom(true),
ChainBinaryName: "gaiad",
ChainFolder: "",
CosmovisorPath: "/home/validator/go/bin/cosmovisor",
},
config := configPkg.CosmovisorConfig{
Enabled: null.BoolFrom(true),
ChainBinaryName: "gaiad",
ChainFolder: "",
CosmovisorPath: "/home/validator/go/bin/cosmovisor",
}
logger := loggerPkg.GetNopLogger()
tracer := tracing.InitNoopTracer()
Expand Down
2 changes: 1 addition & 1 deletion pkg/node_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func NewNodeHandler(
}

if config.CosmovisorConfig.Enabled.Bool {
cosmovisor = cosmovisorPkg.NewCosmovisor(config, appLogger, tracer)
cosmovisor = cosmovisorPkg.NewCosmovisor(config.CosmovisorConfig, appLogger, tracer)
}

if config.GrpcConfig.Enabled.Bool {
Expand Down
79 changes: 79 additions & 0 deletions pkg/queriers/cosmovisor/querier_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package cosmovisor

import (
"context"
"main/assets"
"main/pkg/clients/cosmovisor"
configPkg "main/pkg/config"
"main/pkg/exec"
loggerPkg "main/pkg/logger"
"main/pkg/tracing"
"testing"

"github.com/stretchr/testify/assert"
"gopkg.in/guregu/null.v4"
)

func TestCosmovisorQuerierBase(t *testing.T) {
t.Parallel()

config := configPkg.CosmovisorConfig{
Enabled: null.BoolFrom(true),
ChainBinaryName: "gaiad",
ChainFolder: "",
CosmovisorPath: "/home/validator/go/bin/cosmovisor",
}
logger := loggerPkg.GetNopLogger()
tracer := tracing.InitNoopTracer()
client := cosmovisor.NewCosmovisor(config, *logger, tracer)

querier := NewQuerier(*logger, client, tracer)
assert.True(t, querier.Enabled())
assert.Equal(t, "cosmovisor-querier", querier.Name())
}

func TestCosmovisorQuerierFail(t *testing.T) {
t.Parallel()

config := configPkg.CosmovisorConfig{
Enabled: null.BoolFrom(true),
ChainBinaryName: "gaiad",
ChainFolder: "",
CosmovisorPath: "/home/validator/go/bin/cosmovisor",
}
logger := loggerPkg.GetNopLogger()
tracer := tracing.InitNoopTracer()
client := cosmovisor.NewCosmovisor(config, *logger, tracer)
client.CommandExecutor = &exec.TestCommandExecutor{Fail: true}

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 TestCosmovisorQuerierOk(t *testing.T) {
t.Parallel()

config := configPkg.CosmovisorConfig{
Enabled: null.BoolFrom(true),
ChainBinaryName: "gaiad",
ChainFolder: "",
CosmovisorPath: "/home/validator/go/bin/cosmovisor",
}
logger := loggerPkg.GetNopLogger()
tracer := tracing.InitNoopTracer()
client := cosmovisor.NewCosmovisor(config, *logger, tracer)
client.CommandExecutor = &exec.TestCommandExecutor{Expected: assets.GetBytesOrPanic("cosmovisor-version-ok.txt")}

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, 1)

metric := metrics[0]
assert.Equal(t, map[string]string{"version": "v1.5.0"}, metric.Labels)
assert.InDelta(t, 1, metric.Value, 0.01)
}

0 comments on commit 396b3f0

Please sign in to comment.