github.com/cosmos/cosmos-sdk@v0.50.10/server/config/config_test.go (about)

     1  package config
     2  
     3  import (
     4  	"bytes"
     5  	"os"
     6  	"path/filepath"
     7  	"testing"
     8  
     9  	"github.com/spf13/viper"
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  
    13  	sdk "github.com/cosmos/cosmos-sdk/types"
    14  )
    15  
    16  func TestDefaultConfig(t *testing.T) {
    17  	cfg := DefaultConfig()
    18  	require.True(t, cfg.GetMinGasPrices().IsZero())
    19  }
    20  
    21  func TestGetAndSetMinimumGas(t *testing.T) {
    22  	cfg := DefaultConfig()
    23  
    24  	input := sdk.DecCoins{sdk.NewInt64DecCoin("foo", 5)}
    25  	cfg.SetMinGasPrices(input)
    26  	require.Equal(t, "5.000000000000000000foo", cfg.MinGasPrices)
    27  	require.EqualValues(t, cfg.GetMinGasPrices(), input)
    28  
    29  	input = sdk.DecCoins{sdk.NewInt64DecCoin("bar", 1), sdk.NewInt64DecCoin("foo", 5)}
    30  	cfg.SetMinGasPrices(input)
    31  	require.Equal(t, "1.000000000000000000bar,5.000000000000000000foo", cfg.MinGasPrices)
    32  	require.EqualValues(t, cfg.GetMinGasPrices(), input)
    33  }
    34  
    35  func TestIndexEventsMarshalling(t *testing.T) {
    36  	expectedIn := `index-events = ["key1", "key2", ]` + "\n"
    37  	cfg := DefaultConfig()
    38  	cfg.IndexEvents = []string{"key1", "key2"}
    39  	var buffer bytes.Buffer
    40  
    41  	err := configTemplate.Execute(&buffer, cfg)
    42  	require.NoError(t, err, "executing template")
    43  	actual := buffer.String()
    44  	require.Contains(t, actual, expectedIn, "config file contents")
    45  }
    46  
    47  func TestStreamingConfig(t *testing.T) {
    48  	cfg := Config{
    49  		Streaming: StreamingConfig{
    50  			ABCI: ABCIListenerConfig{
    51  				Keys:          []string{"one", "two"},
    52  				Plugin:        "plugin-A",
    53  				StopNodeOnErr: false,
    54  			},
    55  		},
    56  	}
    57  
    58  	testDir := t.TempDir()
    59  	cfgFile := filepath.Join(testDir, "app.toml")
    60  	WriteConfigFile(cfgFile, &cfg)
    61  
    62  	cfgFileBz, err := os.ReadFile(cfgFile)
    63  	require.NoError(t, err, "reading %s", cfgFile)
    64  	cfgFileContents := string(cfgFileBz)
    65  	t.Logf("Config file contents: %s:\n%s", cfgFile, cfgFileContents)
    66  
    67  	expectedLines := []string{
    68  		`keys = ["one", "two", ]`,
    69  		`plugin = "plugin-A"`,
    70  		`stop-node-on-err = false`,
    71  	}
    72  
    73  	for _, line := range expectedLines {
    74  		assert.Contains(t, cfgFileContents, line+"\n", "config file contents")
    75  	}
    76  
    77  	vpr := viper.New()
    78  	vpr.SetConfigFile(cfgFile)
    79  	err = vpr.ReadInConfig()
    80  	require.NoError(t, err, "reading config file into viper")
    81  
    82  	var actual Config
    83  	err = vpr.Unmarshal(&actual)
    84  	require.NoError(t, err, "vpr.Unmarshal")
    85  
    86  	assert.Equal(t, cfg.Streaming, actual.Streaming, "Streaming")
    87  }
    88  
    89  func TestParseStreaming(t *testing.T) {
    90  	expectedKeys := `keys = ["*", ]` + "\n"
    91  	expectedPlugin := `plugin = "abci_v1"` + "\n"
    92  	expectedStopNodeOnErr := `stop-node-on-err = true` + "\n"
    93  
    94  	cfg := DefaultConfig()
    95  	cfg.Streaming.ABCI.Keys = []string{"*"}
    96  	cfg.Streaming.ABCI.Plugin = "abci_v1"
    97  	cfg.Streaming.ABCI.StopNodeOnErr = true
    98  
    99  	var buffer bytes.Buffer
   100  	err := configTemplate.Execute(&buffer, cfg)
   101  	require.NoError(t, err, "executing template")
   102  	actual := buffer.String()
   103  	require.Contains(t, actual, expectedKeys, "config file contents")
   104  	require.Contains(t, actual, expectedPlugin, "config file contents")
   105  	require.Contains(t, actual, expectedStopNodeOnErr, "config file contents")
   106  }
   107  
   108  func TestReadConfig(t *testing.T) {
   109  	cfg := DefaultConfig()
   110  	tmpFile := filepath.Join(t.TempDir(), "config")
   111  	WriteConfigFile(tmpFile, cfg)
   112  
   113  	v := viper.New()
   114  	otherCfg, err := GetConfig(v)
   115  	require.NoError(t, err)
   116  
   117  	require.Equal(t, *cfg, otherCfg)
   118  }
   119  
   120  func TestIndexEventsWriteRead(t *testing.T) {
   121  	expected := []string{"key3", "key4"}
   122  
   123  	// Create config with two IndexEvents entries, and write it to a file.
   124  	confFile := filepath.Join(t.TempDir(), "app.toml")
   125  	conf := DefaultConfig()
   126  	conf.IndexEvents = expected
   127  
   128  	WriteConfigFile(confFile, conf)
   129  
   130  	// read the file into Viper
   131  	vpr := viper.New()
   132  	vpr.SetConfigFile(confFile)
   133  
   134  	err := vpr.ReadInConfig()
   135  	require.NoError(t, err, "reading config file into viper")
   136  
   137  	// Check that the raw viper value is correct.
   138  	actualRaw := vpr.GetStringSlice("index-events")
   139  	require.Equal(t, expected, actualRaw, "viper's index events")
   140  
   141  	// Check that it is parsed into the config correctly.
   142  	cfg, perr := ParseConfig(vpr)
   143  	require.NoError(t, perr, "parsing config")
   144  
   145  	actual := cfg.IndexEvents
   146  	require.Equal(t, expected, actual, "config value")
   147  }
   148  
   149  func TestGlobalLabelsEventsMarshalling(t *testing.T) {
   150  	expectedIn := `global-labels = [
   151    ["labelname1", "labelvalue1"],
   152    ["labelname2", "labelvalue2"],
   153  ]`
   154  	cfg := DefaultConfig()
   155  	cfg.Telemetry.GlobalLabels = [][]string{{"labelname1", "labelvalue1"}, {"labelname2", "labelvalue2"}}
   156  	var buffer bytes.Buffer
   157  
   158  	err := configTemplate.Execute(&buffer, cfg)
   159  	require.NoError(t, err, "executing template")
   160  	actual := buffer.String()
   161  	require.Contains(t, actual, expectedIn, "config file contents")
   162  }
   163  
   164  func TestGlobalLabelsWriteRead(t *testing.T) {
   165  	expected := [][]string{{"labelname3", "labelvalue3"}, {"labelname4", "labelvalue4"}}
   166  	expectedRaw := make([]interface{}, len(expected))
   167  	for i, exp := range expected {
   168  		pair := make([]interface{}, len(exp))
   169  		for j, s := range exp {
   170  			pair[j] = s
   171  		}
   172  		expectedRaw[i] = pair
   173  	}
   174  
   175  	// Create config with two GlobalLabels entries, and write it to a file.
   176  	confFile := filepath.Join(t.TempDir(), "app.toml")
   177  	conf := DefaultConfig()
   178  	conf.Telemetry.GlobalLabels = expected
   179  	WriteConfigFile(confFile, conf)
   180  
   181  	// Read that file into viper.
   182  	vpr := viper.New()
   183  	vpr.SetConfigFile(confFile)
   184  	rerr := vpr.ReadInConfig()
   185  	require.NoError(t, rerr, "reading config file into viper")
   186  	// Check that the raw viper value is correct.
   187  	actualRaw := vpr.Get("telemetry.global-labels")
   188  	require.Equal(t, expectedRaw, actualRaw, "viper value")
   189  	// Check that it is parsed into the config correctly.
   190  	cfg, perr := ParseConfig(vpr)
   191  	require.NoError(t, perr, "parsing config")
   192  	actual := cfg.Telemetry.GlobalLabels
   193  	require.Equal(t, expected, actual, "config value")
   194  }
   195  
   196  func TestSetConfigTemplate(t *testing.T) {
   197  	conf := DefaultConfig()
   198  	var initBuffer, setBuffer bytes.Buffer
   199  
   200  	// Use the configTemplate defined during init() to create a config string.
   201  	ierr := configTemplate.Execute(&initBuffer, conf)
   202  	require.NoError(t, ierr, "initial configTemplate.Execute")
   203  	expected := initBuffer.String()
   204  
   205  	// Set the template to the default one.
   206  	initTmpl := configTemplate
   207  	require.NotPanics(t, func() {
   208  		SetConfigTemplate(DefaultConfigTemplate)
   209  	}, "SetConfigTemplate")
   210  	setTmpl := configTemplate
   211  	require.NotSame(t, initTmpl, setTmpl, "configTemplate after set")
   212  
   213  	// Create the string again and make sure it's the same.
   214  	serr := configTemplate.Execute(&setBuffer, conf)
   215  	require.NoError(t, serr, "after SetConfigTemplate, configTemplate.Execute")
   216  	actual := setBuffer.String()
   217  	require.Equal(t, expected, actual, "resulting config strings")
   218  }
   219  
   220  func TestAppConfig(t *testing.T) {
   221  	appConfigFile := filepath.Join(t.TempDir(), "app.toml")
   222  	defer func() {
   223  		_ = os.Remove(appConfigFile)
   224  	}()
   225  
   226  	defAppConfig := DefaultConfig()
   227  	SetConfigTemplate(DefaultConfigTemplate)
   228  	WriteConfigFile(appConfigFile, defAppConfig)
   229  
   230  	v := viper.New()
   231  	v.SetConfigFile(appConfigFile)
   232  	require.NoError(t, v.ReadInConfig())
   233  	appCfg := new(Config)
   234  	require.NoError(t, v.Unmarshal(appCfg))
   235  	require.EqualValues(t, appCfg, defAppConfig)
   236  }