Skip to content

Commit

Permalink
chore: add config test
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno committed Jun 22, 2024
1 parent 7cc26ca commit 3aff75e
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 15 deletions.
12 changes: 11 additions & 1 deletion cmd/cosmos-validators-exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,17 @@ func ExecuteValidateConfig(configPath string) {
logger.GetDefaultLogger().Fatal().Err(err).Msg("Config is invalid!")
}

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

Check warning on line 30 in cmd/cosmos-validators-exporter.go

View check run for this annotation

Codecov / codecov/patch

cmd/cosmos-validators-exporter.go#L30

Added line #L30 was not covered by tests

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

Check warning on line 35 in cmd/cosmos-validators-exporter.go

View check run for this annotation

Codecov / codecov/patch

cmd/cosmos-validators-exporter.go#L32-L35

Added lines #L32 - L35 were not covered by tests
}

entry.Msg(warning.Message)

Check warning on line 38 in cmd/cosmos-validators-exporter.go

View check run for this annotation

Codecov / codecov/patch

cmd/cosmos-validators-exporter.go#L38

Added line #L38 was not covered by tests
}

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

Expand Down
10 changes: 9 additions & 1 deletion pkg/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,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)

Check warning on line 63 in pkg/app.go

View check run for this annotation

Codecov / codecov/patch

pkg/app.go#L59-L63

Added lines #L59 - L63 were not covered by tests
}

entry.Msg(warning.Message)

Check warning on line 66 in pkg/app.go

View check run for this annotation

Codecov / codecov/patch

pkg/app.go#L66

Added line #L66 was not covered by tests
}

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

"github.com/rs/zerolog"

"github.com/BurntSushi/toml"
"github.com/creasty/defaults"
)
Expand Down Expand Up @@ -42,19 +40,14 @@ func (c *Config) Validate() error {
return nil
}

func (c *Config) DisplayWarnings(logger *zerolog.Logger) {
for _, chain := range c.Chains {
warnings := chain.DisplayWarnings()
func (c *Config) DisplayWarnings() []Warning {
warnings := []Warning{}

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

entry.Msg(warning.Message)
}
for _, chain := range c.Chains {
warnings = append(warnings, chain.DisplayWarnings()...)
}

return warnings
}

func (c *Config) GetCoingeckoCurrencies() []string {
Expand All @@ -66,6 +59,14 @@ 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
Expand Down
109 changes: 109 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package config

import (
"testing"

"github.com/guregu/null/v5"
"github.com/stretchr/testify/require"
)

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

config := Config{
TracingConfig: TracingConfig{Enabled: null.BoolFrom(true)},
}

err := config.Validate()
require.Error(t, err)
}

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

config := Config{}

err := config.Validate()
require.Error(t, err)
}

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

config := Config{
Chains: []*Chain{{}},
}

err := config.Validate()
require.Error(t, err)
}

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

config := Config{
Chains: []*Chain{{
Name: "chain",
LCDEndpoint: "test",
Validators: []Validator{{Address: "test"}},
}},
}

err := config.Validate()
require.NoError(t, err)
}

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

config := Config{
Chains: []*Chain{{
Name: "chain",
LCDEndpoint: "test",
Validators: []Validator{{Address: "test"}},
}},
}

warnings := config.DisplayWarnings()
require.NotEmpty(t, warnings)
}

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

config := Config{
Chains: []*Chain{{
Name: "chain",
LCDEndpoint: "test",
BaseDenom: "test",
Validators: []Validator{{Address: "test"}},
}},
}

warnings := config.DisplayWarnings()
require.Empty(t, warnings)
}

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

config := Config{
Chains: []*Chain{{
Denoms: DenomInfos{
{Denom: "denom1", CoingeckoCurrency: "denom1"},
{Denom: "denom2"},
},
ConsumerChains: []*ConsumerChain{{
Denoms: DenomInfos{
{Denom: "denom3", CoingeckoCurrency: "denom3"},
{Denom: "denom4"},
},
}},
}},
}

currencies := config.GetCoingeckoCurrencies()
require.Len(t, currencies, 2)
require.Contains(t, currencies, "denom1")
require.Contains(t, currencies, "denom3")
}

0 comments on commit 3aff75e

Please sign in to comment.