github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/tm2/pkg/bft/config/config_test.go (about)

     1  package config
     2  
     3  import (
     4  	"path/filepath"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/require"
     9  
    10  	// allows the default config to have a valid DB
    11  	_ "github.com/gnolang/gno/tm2/pkg/db/goleveldb"
    12  )
    13  
    14  func TestConfig_LoadOrMakeConfigWithOptions(t *testing.T) {
    15  	t.Parallel()
    16  
    17  	t.Run("existing configuration", func(t *testing.T) {
    18  		t.Parallel()
    19  
    20  		// Provide an empty directory
    21  		cfgDir := t.TempDir()
    22  		cfgPath := filepath.Join(cfgDir, defaultConfigPath)
    23  
    24  		// Create a default config
    25  		cfg := DefaultConfig()
    26  		cfg.SetRootDir(cfgDir)
    27  
    28  		// Make an incremental changes
    29  		cfg.Moniker = "custom moniker"
    30  
    31  		// Make sure the cfg paths are initialized
    32  		require.NoError(t, cfg.EnsureDirs())
    33  
    34  		// Write the config
    35  		require.NoError(t, WriteConfigFile(cfgPath, cfg))
    36  
    37  		// Load the config
    38  		loadedCfg, loadErr := LoadOrMakeConfigWithOptions(cfgDir)
    39  		require.NoError(t, loadErr)
    40  
    41  		assert.Equal(t, cfg, loadedCfg)
    42  	})
    43  
    44  	t.Run("no existing config", func(t *testing.T) {
    45  		t.Parallel()
    46  
    47  		// Provide an empty directory
    48  		cfgDir := t.TempDir()
    49  		cfgPath := filepath.Join(cfgDir, defaultConfigPath)
    50  
    51  		cfg, err := LoadOrMakeConfigWithOptions(cfgDir)
    52  		require.NoError(t, err)
    53  
    54  		// Make sure the returned cfg is the default one
    55  		expectedCfg := DefaultConfig()
    56  		expectedCfg.SetRootDir(cfgDir)
    57  
    58  		assert.Equal(t, expectedCfg, cfg)
    59  
    60  		// Make sure the returned config was saved
    61  		loadedCfg, loadErr := LoadConfigFile(cfgPath)
    62  		require.NoError(t, loadErr)
    63  
    64  		loadedCfg.SetRootDir(cfgDir)
    65  
    66  		assert.Equal(t, cfg, loadedCfg)
    67  	})
    68  
    69  	t.Run("no existing config, with options", func(t *testing.T) {
    70  		t.Parallel()
    71  
    72  		moniker := "dummy moniker"
    73  
    74  		// Provide an empty directory
    75  		cfgDir := t.TempDir()
    76  		cfgPath := filepath.Join(cfgDir, defaultConfigPath)
    77  
    78  		cfg, err := LoadOrMakeConfigWithOptions(
    79  			cfgDir,
    80  			func(cfg *Config) {
    81  				cfg.BaseConfig.Moniker = moniker
    82  			},
    83  		)
    84  		require.NoError(t, err)
    85  
    86  		// Make sure the returned config was saved
    87  		loadedCfg, loadErr := LoadConfigFile(cfgPath)
    88  		require.NoError(t, loadErr)
    89  
    90  		loadedCfg.SetRootDir(cfgDir)
    91  
    92  		assert.Equal(t, cfg, loadedCfg)
    93  	})
    94  }
    95  
    96  func TestConfig_ValidateBaseConfig(t *testing.T) {
    97  	t.Parallel()
    98  
    99  	t.Run("valid default config", func(t *testing.T) {
   100  		t.Parallel()
   101  
   102  		c := DefaultConfig()
   103  
   104  		assert.NoError(t, c.BaseConfig.ValidateBasic())
   105  	})
   106  
   107  	t.Run("invalid moniker", func(t *testing.T) {
   108  		t.Parallel()
   109  
   110  		c := DefaultConfig()
   111  		c.Moniker = ""
   112  
   113  		assert.ErrorIs(t, c.BaseConfig.ValidateBasic(), errInvalidMoniker)
   114  	})
   115  
   116  	t.Run("invalid DB backend", func(t *testing.T) {
   117  		t.Parallel()
   118  
   119  		c := DefaultConfig()
   120  		c.DBBackend = "totally valid backend"
   121  
   122  		assert.ErrorIs(t, c.BaseConfig.ValidateBasic(), errInvalidDBBackend)
   123  	})
   124  
   125  	t.Run("DB path not set", func(t *testing.T) {
   126  		t.Parallel()
   127  
   128  		c := DefaultConfig()
   129  		c.DBPath = ""
   130  
   131  		assert.ErrorIs(t, c.BaseConfig.ValidateBasic(), errInvalidDBPath)
   132  	})
   133  
   134  	t.Run("priv validator key path not set", func(t *testing.T) {
   135  		t.Parallel()
   136  
   137  		c := DefaultConfig()
   138  		c.PrivValidatorKey = ""
   139  
   140  		assert.ErrorIs(t, c.BaseConfig.ValidateBasic(), errInvalidPrivValidatorKeyPath)
   141  	})
   142  
   143  	t.Run("priv validator state path not set", func(t *testing.T) {
   144  		t.Parallel()
   145  
   146  		c := DefaultConfig()
   147  		c.PrivValidatorState = ""
   148  
   149  		assert.ErrorIs(t, c.BaseConfig.ValidateBasic(), errInvalidPrivValidatorStatePath)
   150  	})
   151  
   152  	t.Run("invalid priv validator listen address", func(t *testing.T) {
   153  		t.Parallel()
   154  
   155  		c := DefaultConfig()
   156  		c.PrivValidatorListenAddr = "beep.boop"
   157  
   158  		assert.ErrorIs(t, c.BaseConfig.ValidateBasic(), errInvalidPrivValidatorListenAddress)
   159  	})
   160  
   161  	t.Run("node key path not set", func(t *testing.T) {
   162  		t.Parallel()
   163  
   164  		c := DefaultConfig()
   165  		c.NodeKey = ""
   166  
   167  		assert.ErrorIs(t, c.BaseConfig.ValidateBasic(), errInvalidNodeKeyPath)
   168  	})
   169  
   170  	t.Run("invalid ABCI mechanism", func(t *testing.T) {
   171  		t.Parallel()
   172  
   173  		c := DefaultConfig()
   174  		c.ABCI = "hopes and dreams"
   175  
   176  		assert.ErrorIs(t, c.BaseConfig.ValidateBasic(), errInvalidABCIMechanism)
   177  	})
   178  
   179  	t.Run("invalid prof listen address", func(t *testing.T) {
   180  		t.Parallel()
   181  
   182  		c := DefaultConfig()
   183  		c.ProfListenAddress = "beep.boop"
   184  
   185  		assert.ErrorIs(t, c.BaseConfig.ValidateBasic(), errInvalidProfListenAddress)
   186  	})
   187  }