Skip to content

Commit

Permalink
fix: Server config index-events incorrectly marshaled (cosmos#10067)
Browse files Browse the repository at this point in the history
* Add changelog line.

* [10016]: Add some unit tests for the index-events and global-labels config fields.

* [10016]: Fix the marshalling of the index-events config value into the config file.

* [10016]: Remove the StringsJoin func map entry from the config template creation because it isn't needed.

* [10016]: Add a unit test for SetConfigTemplate.

Co-authored-by: Tyler <48813565+technicallyty@users.noreply.github.com>
  • Loading branch information
dwedul-figure and technicallyty authored Sep 8, 2021
1 parent d6c3017 commit eb0113e
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* [\#9829](https://github.com/cosmos/cosmos-sdk/pull/9829) Fixed Coin denom sorting not being checked during `Balance.Validate` check. Refactored the Validation logic to use `Coins.Validate` for `Balance.Coins`.
+ [\#9965](https://github.com/cosmos/cosmos-sdk/pull/9965) Fixed `simd version` command output to report the right release tag.
+ [\#9980](https://github.com/cosmos/cosmos-sdk/pull/9980) Returning the error when the invalid argument is passed to bank query total supply cli.
* (server) [#10016](https://github.com/cosmos/cosmos-sdk/issues/10016) Fix marshaling of index-events into server config file.

### State Machine Breaking

Expand Down
110 changes: 110 additions & 0 deletions server/config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package config

import (
"bytes"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"path/filepath"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -18,3 +22,109 @@ func TestSetMinimumFees(t *testing.T) {
cfg.SetMinGasPrices(sdk.DecCoins{sdk.NewInt64DecCoin("foo", 5)})
require.Equal(t, "5.000000000000000000foo", cfg.MinGasPrices)
}

func TestIndexEventsMarshalling(t *testing.T) {
expectedIn := `index-events = ["key1", "key2", ]` + "\n"
cfg := DefaultConfig()
cfg.IndexEvents = []string{"key1", "key2"}
var buffer bytes.Buffer

err := configTemplate.Execute(&buffer, cfg)
require.NoError(t, err, "executing template")
actual := buffer.String()
assert.Contains(t, actual, expectedIn, "config file contents")
}

func TestIndexEventsWriteRead(t *testing.T) {
expected := []string{"key3", "key4"}
// Create config with two IndexEvents entries, and write it to a file.
confFile := filepath.Join(t.TempDir(), "app.toml")
conf := DefaultConfig()
conf.IndexEvents = expected
WriteConfigFile(confFile, conf)

// Read that file into viper.
vpr := viper.New()
vpr.SetConfigFile(confFile)
err := vpr.ReadInConfig()
require.NoError(t, err, "reading config file into viper")
// Check that the raw viper value is correct.
actualRaw := vpr.GetStringSlice("index-events")
require.Equal(t, expected, actualRaw, "viper's index events")
// Check that it is parsed into the config correctly.
cfg, perr := ParseConfig(vpr)
require.NoError(t, perr, "parsing config")
actual := cfg.IndexEvents
require.Equal(t, expected, actual, "config value")
}

func TestGlobalLabelsEventsMarshalling(t *testing.T) {
expectedIn := `global-labels = [
["labelname1", "labelvalue1"],
["labelname2", "labelvalue2"],
]` + "\n"
cfg := DefaultConfig()
cfg.Telemetry.GlobalLabels = [][]string{{"labelname1", "labelvalue1"}, {"labelname2", "labelvalue2"}}
var buffer bytes.Buffer

err := configTemplate.Execute(&buffer, cfg)
require.NoError(t, err, "executing template")
actual := buffer.String()
assert.Contains(t, actual, expectedIn, "config file contents")
}

func TestGlobalLabelsWriteRead(t *testing.T) {
expected := [][]string{{"labelname3", "labelvalue3"}, {"labelname4", "labelvalue4"}}
expectedRaw := make([]interface{}, len(expected))
for i, exp := range expected {
pair := make([]interface{}, len(exp))
for j, s := range exp {
pair[j] = s
}
expectedRaw[i] = pair
}

// Create config with two GlobalLabels entries, and write it to a file.
confFile := filepath.Join(t.TempDir(), "app.toml")
conf := DefaultConfig()
conf.Telemetry.GlobalLabels = expected
WriteConfigFile(confFile, conf)

// Read that file into viper.
vpr := viper.New()
vpr.SetConfigFile(confFile)
rerr := vpr.ReadInConfig()
require.NoError(t, rerr, "reading config file into viper")
// Check that the raw viper value is correct.
actualRaw := vpr.Get("telemetry.global-labels")
require.Equal(t, expectedRaw, actualRaw, "viper value")
// Check that it is parsed into the config correctly.
cfg, perr := ParseConfig(vpr)
require.NoError(t, perr, "parsing config")
actual := cfg.Telemetry.GlobalLabels
require.Equal(t, expected, actual, "config value")
}

func TestSetConfigTemplate(t *testing.T) {
conf := DefaultConfig()
var initBuffer, setBuffer bytes.Buffer

// Use the configTemplate defined during init() to create a config string.
ierr := configTemplate.Execute(&initBuffer, conf)
require.NoError(t, ierr, "initial configTemplate.Execute")
expected := initBuffer.String()

// Set the template to the default one.
initTmpl := configTemplate
require.NotPanics(t, func() {
SetConfigTemplate(DefaultConfigTemplate)
}, "SetConfigTemplate")
setTmpl := configTemplate
require.NotSame(t, initTmpl, setTmpl, "configTemplate after set")

// Create the string again and make sure it's the same.
serr := configTemplate.Execute(&setBuffer, conf)
require.NoError(t, serr, "after SetConfigTemplate, configTemplate.Execute")
actual := setBuffer.String()
require.Equal(t, expected, actual, "resulting config strings")
}
2 changes: 1 addition & 1 deletion server/config/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ inter-block-cache = {{ .BaseConfig.InterBlockCache }}
#
# Example:
# ["message.sender", "message.recipient"]
index-events = {{ .BaseConfig.IndexEvents }}
index-events = [{{ range .BaseConfig.IndexEvents }}{{ printf "%q, " . }}{{end}}]
###############################################################################
### Telemetry Configuration ###
Expand Down

0 comments on commit eb0113e

Please sign in to comment.