Skip to content

Commit

Permalink
chore: add cosmovisor client test (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno authored Jun 30, 2024
1 parent b182b5e commit 200c299
Show file tree
Hide file tree
Showing 14 changed files with 452 additions and 37 deletions.
2 changes: 2 additions & 0 deletions assets/cosmovisor-app-version-ok.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
9:37AM INF running app args=["version","--long","--output","json"] module=cosmovisor path=/home/validator/.decentr/cosmovisor/upgrades/07-tendermint-13/bin/decentrd
{"name":"decentr","server_name":"decentrd","version":"1.6.4","commit":"2db736296c1089f3fbdf7328e7309e9fc7be1f13","build_tags":"","go":"go version go1.21.3 linux/amd64","build_deps":["filippo.io/[email protected]","github.com/99designs/[email protected] =\u003e github.com/cosmos/[email protected]","github.com/ChainSafe/[email protected]","github.com/Workiva/[email protected]","github.com/armon/[email protected]","github.com/beorn7/[email protected]","github.com/bgentry/[email protected]","github.com/btcsuite/[email protected]","github.com/cespare/xxhash/[email protected]","github.com/coinbase/[email protected]","github.com/confio/ics23/[email protected] =\u003e github.com/cosmos/cosmos-sdk/ics23/[email protected]","github.com/cosmos/[email protected]","github.com/cosmos/[email protected]","github.com/cosmos/[email protected]","github.com/cosmos/[email protected]","github.com/cosmos/[email protected]","github.com/creachadair/[email protected]","github.com/davecgh/[email protected]","github.com/desertbit/[email protected]","github.com/dvsekhvalnov/[email protected]","github.com/felixge/[email protected]","github.com/fsnotify/[email protected]","github.com/go-kit/[email protected]","github.com/go-kit/[email protected]","github.com/go-logfmt/[email protected]","github.com/godbus/[email protected]","github.com/gofrs/[email protected]+incompatible","github.com/gogo/[email protected]","github.com/gogo/[email protected] =\u003e github.com/regen-network/[email protected]","github.com/golang/[email protected]","github.com/golang/[email protected]","github.com/google/[email protected]","github.com/google/[email protected]","github.com/gorilla/[email protected]","github.com/gorilla/[email protected]","github.com/gorilla/[email protected]","github.com/grpc-ecosystem/[email protected]","github.com/grpc-ecosystem/[email protected]","github.com/gsterjov/[email protected]","github.com/gtank/[email protected]","github.com/gtank/[email protected]","github.com/hashicorp/[email protected]","github.com/hashicorp/[email protected]","github.com/hashicorp/[email protected]","github.com/hdevalence/[email protected]","github.com/improbable-eng/[email protected]","github.com/klauspost/[email protected]","github.com/lib/[email protected]","github.com/libp2p/[email protected]","github.com/magiconair/[email protected]","github.com/mattn/[email protected]","github.com/mattn/[email protected]","github.com/matttproud/[email protected]","github.com/mimoo/[email protected]","github.com/minio/[email protected]","github.com/mitchellh/[email protected]","github.com/mtibben/[email protected]","github.com/pelletier/go-toml/[email protected]","github.com/pkg/[email protected]","github.com/pmezard/[email protected]","github.com/prometheus/[email protected]","github.com/prometheus/[email protected]","github.com/prometheus/[email protected]","github.com/prometheus/[email protected]","github.com/rakyll/[email protected]","github.com/rcrowley/[email protected]","github.com/regen-network/[email protected]","github.com/rs/[email protected]","github.com/rs/[email protected]","github.com/spf13/[email protected]","github.com/spf13/[email protected]","github.com/spf13/[email protected]","github.com/spf13/[email protected]","github.com/spf13/[email protected]","github.com/spf13/[email protected]","github.com/stretchr/[email protected]","github.com/subosito/[email protected]","github.com/syndtr/[email protected]","github.com/tendermint/[email protected]","github.com/tendermint/[email protected]","github.com/tendermint/[email protected]","github.com/tendermint/[email protected]","github.com/tendermint/[email protected]","github.com/tendermint/[email protected]","golang.org/x/[email protected]","golang.org/x/[email protected]","golang.org/x/[email protected]","golang.org/x/[email protected]","golang.org/x/[email protected]","golang.org/x/[email protected]","google.golang.org/[email protected]","google.golang.org/[email protected] =\u003e google.golang.org/[email protected]","google.golang.org/[email protected]","gopkg.in/[email protected]","gopkg.in/[email protected]","gopkg.in/[email protected]","nhooyr.io/[email protected]"],"cosmos_sdk_version":"v0.45.9"}
3 changes: 3 additions & 0 deletions assets/cosmovisor-version-ok.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
cosmovisor version: v1.5.0
9:33AM INF running app args=["version"] module=cosmovisor path=/home/validator/.decentr/cosmovisor/upgrades/07-tendermint-13/bin/decentrd
1.6.4
Empty file.
6 changes: 6 additions & 0 deletions assets/cosmovisor/upgrades/v15/fs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package v15

import _ "embed"

//go:embed .gitkeep
var _ string
13 changes: 3 additions & 10 deletions cmd/cosmos-node-exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package main
import (
"main/pkg"
"main/pkg/config"
"main/pkg/fs"
"main/pkg/logger"
"os"

"github.com/spf13/cobra"
)
Expand All @@ -13,22 +13,15 @@ var (
version = "unknown"
)

type OsFS struct {
}

func (fs *OsFS) ReadFile(name string) ([]byte, error) {
return os.ReadFile(name)
}

func ExecuteMain(configPath string) {
filesystem := &OsFS{}
filesystem := &fs.OsFS{}

app := pkg.NewApp(filesystem, configPath, version)
app.Start()
}

func ExecuteValidateConfig(configPath string) {
filesystem := &OsFS{}
filesystem := &fs.OsFS{}

appConfig, err := config.GetConfig(filesystem, configPath)
if err != nil {
Expand Down
52 changes: 32 additions & 20 deletions pkg/clients/cosmovisor/cosmovisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import (
"fmt"
"main/pkg/config"
"main/pkg/constants"
"main/pkg/exec"
"main/pkg/fs"
"main/pkg/query_info"
"main/pkg/types"
"main/pkg/utils"
"os"
"os/exec"
"strings"

"go.opentelemetry.io/otel/trace"
Expand All @@ -20,9 +21,12 @@ import (
)

type Cosmovisor struct {
Logger zerolog.Logger
Config config.CosmovisorConfig
Tracer trace.Tracer
Logger zerolog.Logger
Config config.CosmovisorConfig
Tracer trace.Tracer
CommandExecutor exec.CommandExecutor
Filesystem fs.FS
UpgradeSubfolderPath string
}

func NewCosmovisor(
Expand All @@ -31,9 +35,12 @@ func NewCosmovisor(
tracer trace.Tracer,
) *Cosmovisor {
return &Cosmovisor{
Logger: logger.With().Str("component", "cosmovisor").Logger(),
Config: config.CosmovisorConfig,
Tracer: tracer,
Logger: logger.With().Str("component", "cosmovisor").Logger(),
Config: config.CosmovisorConfig,
Tracer: tracer,
CommandExecutor: &exec.NativeCommandExecutor{},
Filesystem: &fs.OsFS{},
UpgradeSubfolderPath: "/cosmovisor/upgrades",
}
}

Expand All @@ -43,8 +50,9 @@ func NewCosmovisor(
func getJsonString(input string) string {
split := strings.Split(input, "\n")
for _, line := range split {
if strings.HasPrefix(line, "{") && strings.HasSuffix(line, "}") {
return line
trimmed := strings.TrimSpace(line)
if strings.HasPrefix(trimmed, "{") && strings.HasSuffix(trimmed, "}") {
return trimmed
}
}

Expand All @@ -65,14 +73,17 @@ func (c *Cosmovisor) GetVersion(ctx context.Context) (types.VersionInfo, query_i
Success: false,
}

cmd := exec.Command(c.Config.CosmovisorPath, "run", "version", "--long", "--output", "json")
cmd.Env = append(
env := append(
os.Environ(),
"DAEMON_NAME="+c.Config.ChainBinaryName,
"DAEMON_HOME="+c.Config.ChainFolder,
)

out, err := cmd.CombinedOutput()
out, err := c.CommandExecutor.RunWithEnv(
c.Config.CosmovisorPath,
[]string{"run", "version", "--long", "--output", "json"},
env,
)
if err != nil {
c.Logger.Error().
Err(err).
Expand Down Expand Up @@ -111,14 +122,13 @@ func (c *Cosmovisor) GetCosmovisorVersion(ctx context.Context) (string, query_in
Success: false,
}

cmd := exec.Command(c.Config.CosmovisorPath, "version")
cmd.Env = append(
env := append(
os.Environ(),
"DAEMON_NAME="+c.Config.ChainBinaryName,
"DAEMON_HOME="+c.Config.ChainFolder,
)

out, err := cmd.CombinedOutput()
out, err := c.CommandExecutor.RunWithEnv(c.Config.CosmovisorPath, []string{"version"}, env)
if err != nil {
c.Logger.Error().
Err(err).
Expand Down Expand Up @@ -154,8 +164,8 @@ func (c *Cosmovisor) GetUpgrades(ctx context.Context) (types.UpgradesPresent, qu
Success: false,
}

upgradesFolder := c.Config.ChainFolder + "/cosmovisor/upgrades"
upgradesFolderContent, err := os.ReadDir(upgradesFolder)
upgradesFolder := c.Config.ChainFolder + c.UpgradeSubfolderPath
upgradesFolderContent, err := c.Filesystem.ReadDir(upgradesFolder)
if err != nil {
span.RecordError(err)
c.Logger.Error().Err(err).Msg("Could not fetch Cosmovisor upgrades folder content")
Expand All @@ -166,7 +176,6 @@ func (c *Cosmovisor) GetUpgrades(ctx context.Context) (types.UpgradesPresent, qu

for _, upgradeFolder := range upgradesFolderContent {
if !upgradeFolder.IsDir() {
upgrades[upgradeFolder.Name()] = false
continue
}

Expand All @@ -177,11 +186,14 @@ func (c *Cosmovisor) GetUpgrades(ctx context.Context) (types.UpgradesPresent, qu
c.Config.ChainBinaryName,
)

if _, err := os.Stat(upgradeBinaryPath); err != nil {
if errors.Is(err, os.ErrNotExist) {
if _, err := c.Filesystem.Stat(upgradeBinaryPath); err != nil {
if !errors.Is(err, os.ErrNotExist) {
c.Logger.Warn().Err(err).Msg("Error fetching Cosmovisor upgrade")
span.RecordError(err)
return upgrades, cosmovisorGetUpgradesQueryInfo, err
}

upgrades[upgradeFolder.Name()] = false
} else {
upgrades[upgradeFolder.Name()] = true
}
Expand Down
Loading

0 comments on commit 200c299

Please sign in to comment.