From eb0113e4bc58163c4f4555adb10e0fde6f650a62 Mon Sep 17 00:00:00 2001 From: Daniel Wedul <72031080+dwedul-figure@users.noreply.github.com> Date: Wed, 8 Sep 2021 17:02:09 -0600 Subject: [PATCH] fix: Server config index-events incorrectly marshaled (#10067) * 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> --- CHANGELOG.md | 1 + server/config/config_test.go | 110 +++++++++++++++++++++++++++++++++++ server/config/toml.go | 2 +- 3 files changed, 112 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b2a0b6165275..bffd2fc2adaf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/server/config/config_test.go b/server/config/config_test.go index e828a0716dc8..4c396cf45586 100644 --- a/server/config/config_test.go +++ b/server/config/config_test.go @@ -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" @@ -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") +} diff --git a/server/config/toml.go b/server/config/toml.go index 9ecce83bcd17..fe1112b1296b 100644 --- a/server/config/toml.go +++ b/server/config/toml.go @@ -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 ###