Skip to content

Commit

Permalink
chore: add utils and types test (#61)
Browse files Browse the repository at this point in the history
* chore: add utils and types test

* chore: add logger tests

* chore: add chain query test

* chore: fixed linting

* chore: add display warning

* chore: add tests for chain

* chore: add tests for consumer chain

* chore: add tests for denom info

* chore: add tests for tracing

* chore: fix linting

* chore: add tests for tracing

* chore: moved validator to a separate file

* chore: add config test

* chore: add tests for get config

* chore: added displayWarning for missing consensus address
  • Loading branch information
freak12techno authored Jun 22, 2024
1 parent cce5388 commit 4bb8f21
Show file tree
Hide file tree
Showing 28 changed files with 1,055 additions and 56 deletions.
8 changes: 8 additions & 0 deletions assets/fs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package assets

import (
"embed"
)

//go:embed *
var EmbedFS embed.FS
1 change: 1 addition & 0 deletions assets/invalid.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
notvalid
10 changes: 10 additions & 0 deletions assets/valid.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[[chains]]
name = "sentinel"
lcd-endpoint = "https://api.sentinel.quokkastake.io"
coingecko-currency = "sentinel"
base-denom = "udvpn"
denom = "dvpn"
bech-wallet-prefix = "sent"
validators = [
{ address = "sentvaloper1rw9wtyhsus7jvx55v3qv5nzun054ma6kz4237k" }
]
26 changes: 23 additions & 3 deletions cmd/cosmos-validators-exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"main/pkg"
configPkg "main/pkg/config"
"main/pkg/logger"
"os"

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

type OsFS struct {
}

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

func ExecuteMain(configPath string) {
app := pkg.NewApp(configPath, version)
filesystem := &OsFS{}
app := pkg.NewApp(configPath, filesystem, version)
app.Start()
}

func ExecuteValidateConfig(configPath string) {
config, err := configPkg.GetConfig(configPath)
filesystem := &OsFS{}
config, err := configPkg.GetConfig(configPath, filesystem)
if err != nil {
logger.GetDefaultLogger().Fatal().Err(err).Msg("Could not load config!")
}
Expand All @@ -27,7 +37,17 @@ func ExecuteValidateConfig(configPath string) {
logger.GetDefaultLogger().Fatal().Err(err).Msg("Config is invalid!")
}

config.DisplayWarnings(logger.GetDefaultLogger())
warnings := config.DisplayWarnings()

for _, warning := range warnings {
entry := logger.GetDefaultLogger().Warn()
for label, value := range warning.Labels {
entry = entry.Str(label, value)
}

entry.Msg(warning.Message)
}

logger.GetDefaultLogger().Info().Msg("Provided config is valid.")
}

Expand Down
15 changes: 12 additions & 3 deletions pkg/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pkg

import (
fetchersPkg "main/pkg/fetchers"
"main/pkg/fs"
generatorsPkg "main/pkg/generators"
coingeckoPkg "main/pkg/price_fetchers/coingecko"
dexScreenerPkg "main/pkg/price_fetchers/dex_screener"
Expand Down Expand Up @@ -45,8 +46,8 @@ type App struct {
Generators []generatorsPkg.Generator
}

func NewApp(configPath string, version string) *App {
appConfig, err := config.GetConfig(configPath)
func NewApp(configPath string, filesystem fs.FS, version string) *App {
appConfig, err := config.GetConfig(configPath, filesystem)
if err != nil {
loggerPkg.GetDefaultLogger().Fatal().Err(err).Msg("Could not load config")
}
Expand All @@ -56,7 +57,15 @@ func NewApp(configPath string, version string) *App {
}

logger := loggerPkg.GetLogger(appConfig.LogConfig)
appConfig.DisplayWarnings(logger)
warnings := appConfig.DisplayWarnings()
for _, warning := range warnings {
entry := logger.Warn()
for label, value := range warning.Labels {
entry = entry.Str(label, value)
}

entry.Msg(warning.Message)
}

tracer, err := tracing.InitTracer(appConfig.TracingConfig, version)
if err != nil {
Expand Down
21 changes: 14 additions & 7 deletions pkg/config/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"fmt"

"github.com/guregu/null/v5"

"github.com/rs/zerolog"
)

type Chain struct {
Expand Down Expand Up @@ -68,14 +66,23 @@ func (c *Chain) Validate() error {
return nil
}

func (c *Chain) DisplayWarnings(logger *zerolog.Logger) {
func (c *Chain) DisplayWarnings() []Warning {
warnings := []Warning{}

if c.BaseDenom == "" {
logger.Warn().
Str("chain", c.Name).
Msg("Base denom is not set")
warnings = append(warnings, Warning{
Message: "Base denom is not set",
Labels: map[string]string{"chain": c.Name},
})
}

for _, denom := range c.Denoms {
denom.DisplayWarnings(c, logger)
warnings = append(warnings, denom.DisplayWarnings(c)...)
}

for _, validator := range c.Validators {
warnings = append(warnings, validator.DisplayWarnings(c)...)
}

return warnings
}
139 changes: 139 additions & 0 deletions pkg/config/chain_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package config

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

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

chain := Chain{
LCDEndpoint: "example",
Name: "chain",
Queries: map[string]bool{"enabled": true},
}

assert.Equal(t, "example", chain.GetHost())
assert.Equal(t, "chain", chain.GetName())
assert.Len(t, chain.GetQueries(), 1)
}

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

chain := Chain{}
err := chain.Validate()
require.Error(t, err)
}

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

chain := Chain{Name: "test"}
err := chain.Validate()
require.Error(t, err)
}

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

chain := Chain{Name: "test", LCDEndpoint: "test"}
err := chain.Validate()
require.Error(t, err)
}

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

chain := Chain{
Name: "test",
LCDEndpoint: "test",
Validators: []Validator{{}},
}
err := chain.Validate()
require.Error(t, err)
}

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

chain := Chain{
Name: "test",
LCDEndpoint: "test",
Validators: []Validator{{Address: "test"}},
Denoms: DenomInfos{{}},
}
err := chain.Validate()
require.Error(t, err)
}

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

chain := Chain{
Name: "test",
LCDEndpoint: "test",
Validators: []Validator{{Address: "test"}},
Denoms: DenomInfos{{Denom: "ustake", DisplayDenom: "stake"}},
ConsumerChains: []*ConsumerChain{{}},
}
err := chain.Validate()
require.Error(t, err)
}

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

chain := Chain{
Name: "test",
LCDEndpoint: "test",
Validators: []Validator{{Address: "test"}},
Denoms: DenomInfos{{Denom: "ustake", DisplayDenom: "stake"}},
}
err := chain.Validate()
require.NoError(t, err)
}

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

chain := Chain{
Name: "test",
LCDEndpoint: "test",
Validators: []Validator{{Address: "test"}},
Denoms: DenomInfos{{Denom: "ustake", DisplayDenom: "stake", CoingeckoCurrency: "stake"}},
}
warnings := chain.DisplayWarnings()
require.NotEmpty(t, warnings)
}

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

chain := Chain{
Name: "test",
LCDEndpoint: "test",
BaseDenom: "ustake",
Validators: []Validator{{Address: "test", ConsensusAddress: "test"}},
Denoms: DenomInfos{{Denom: "ustake", DisplayDenom: "stake"}},
}
warnings := chain.DisplayWarnings()
require.NotEmpty(t, warnings)
}

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

chain := Chain{
Name: "test",
LCDEndpoint: "test",
BaseDenom: "ustake",
Validators: []Validator{{Address: "test", ConsensusAddress: "test"}},
Denoms: DenomInfos{{Denom: "ustake", DisplayDenom: "stake", CoingeckoCurrency: "stake"}},
}
warnings := chain.DisplayWarnings()
require.Empty(t, warnings)
}
42 changes: 18 additions & 24 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,12 @@ package config
import (
"errors"
"fmt"
"os"

"github.com/rs/zerolog"
"main/pkg/fs"

"github.com/BurntSushi/toml"
"github.com/creasty/defaults"
)

type Validator struct {
Address string `toml:"address"`
ConsensusAddress string `toml:"consensus-address"`
}

func (v *Validator) Validate() error {
if v.Address == "" {
return errors.New("validator address is expected!")
}

return nil
}

type Config struct {
LogConfig LogConfig `toml:"log"`
TracingConfig TracingConfig `toml:"tracing"`
Expand Down Expand Up @@ -55,10 +40,14 @@ func (c *Config) Validate() error {
return nil
}

func (c *Config) DisplayWarnings(logger *zerolog.Logger) {
func (c *Config) DisplayWarnings() []Warning {
warnings := []Warning{}

for _, chain := range c.Chains {
chain.DisplayWarnings(logger)
warnings = append(warnings, chain.DisplayWarnings()...)
}

return warnings
}

func (c *Config) GetCoingeckoCurrencies() []string {
Expand All @@ -70,13 +59,21 @@ func (c *Config) GetCoingeckoCurrencies() []string {
currencies = append(currencies, denom.CoingeckoCurrency)
}
}

for _, consumerChain := range chain.ConsumerChains {
for _, denom := range consumerChain.Denoms {
if denom.CoingeckoCurrency != "" {
currencies = append(currencies, denom.CoingeckoCurrency)
}
}
}
}

return currencies
}

func GetConfig(path string) (*Config, error) {
configBytes, err := os.ReadFile(path)
func GetConfig(path string, filesystem fs.FS) (*Config, error) {
configBytes, err := filesystem.ReadFile(path)
if err != nil {
return nil, err
}
Expand All @@ -88,9 +85,6 @@ func GetConfig(path string) (*Config, error) {
return nil, err
}

if err = defaults.Set(&configStruct); err != nil {
return nil, err
}

defaults.MustSet(&configStruct)
return &configStruct, nil
}
Loading

0 comments on commit 4bb8f21

Please sign in to comment.