From a61ceddd1082979b382fed54c27fda3d406523d6 Mon Sep 17 00:00:00 2001 From: Jae Kwon Date: Fri, 3 Sep 2021 19:21:00 -0700 Subject: [PATCH] fix tests... --- pkgs/bft/config/config.go | 14 +++++++------- pkgs/bft/config/toml_test.go | 13 ++++++++----- pkgs/bft/config/utils.go | 2 +- pkgs/bft/consensus/config/config.go | 2 +- pkgs/bft/consensus/config/utils.go | 4 ++-- pkgs/bft/mempool/config/config.go | 2 +- pkgs/bft/mempool/config/utils.go | 2 +- pkgs/bft/node/node_test.go | 2 +- pkgs/bft/rpc/config/config.go | 4 ++-- pkgs/bft/rpc/config/utils.go | 2 +- pkgs/bft/rpc/lib/test/main.go | 2 +- pkgs/bft/types/genesis.go | 7 +++++++ pkgs/bft/types/genesis_test.go | 2 +- pkgs/bft/types/package.go | 1 + pkgs/sdk/baseapp_test.go | 2 +- 15 files changed, 36 insertions(+), 25 deletions(-) diff --git a/pkgs/bft/config/config.go b/pkgs/bft/config/config.go index 425d242baeb..4f1231ec7cd 100644 --- a/pkgs/bft/config/config.go +++ b/pkgs/bft/config/config.go @@ -39,7 +39,7 @@ func DefaultConfig() *Config { // LoadOrMakeDefaultConfig() loads configuration or saves a default one. func LoadOrMakeDefaultConfig(root string) (cfg *Config) { - configPath := rootify(defaultConfigFilePath, root) + configPath := join(root, defaultConfigFilePath) if osm.FileExists(configPath) { cfg = LoadConfigFile(configPath) cfg.SetRootDir(root) @@ -238,7 +238,7 @@ func DefaultBaseConfig() BaseConfig { func TestBaseConfig() BaseConfig { cfg := DefaultBaseConfig() cfg.chainID = "tendermint_test" - cfg.ProxyApp = "kvstore" + cfg.ProxyApp = "mock://kvstore" cfg.FastSyncMode = false cfg.DBBackend = "memdb" return cfg @@ -250,27 +250,27 @@ func (cfg BaseConfig) ChainID() string { // GenesisFile returns the full path to the genesis.json file func (cfg BaseConfig) GenesisFile() string { - return rootify(cfg.Genesis, cfg.RootDir) + return join(cfg.RootDir, cfg.Genesis) } // PrivValidatorKeyFile returns the full path to the priv_validator_key.json file func (cfg BaseConfig) PrivValidatorKeyFile() string { - return rootify(cfg.PrivValidatorKey, cfg.RootDir) + return join(cfg.RootDir, cfg.PrivValidatorKey) } // PrivValidatorFile returns the full path to the priv_validator_state.json file func (cfg BaseConfig) PrivValidatorStateFile() string { - return rootify(cfg.PrivValidatorState, cfg.RootDir) + return join(cfg.RootDir, cfg.PrivValidatorState) } // NodeKeyFile returns the full path to the node_key.json file func (cfg BaseConfig) NodeKeyFile() string { - return rootify(cfg.NodeKey, cfg.RootDir) + return join(cfg.RootDir, cfg.NodeKey) } // DBDir returns the full path to the database directory func (cfg BaseConfig) DBDir() string { - return rootify(cfg.DBPath, cfg.RootDir) + return join(cfg.RootDir, cfg.DBPath) } // ValidateBasic performs basic validation (checking param bounds, etc.) and diff --git a/pkgs/bft/config/toml_test.go b/pkgs/bft/config/toml_test.go index 5910f10c56a..80f092bf949 100644 --- a/pkgs/bft/config/toml_test.go +++ b/pkgs/bft/config/toml_test.go @@ -3,7 +3,6 @@ package config import ( "io/ioutil" "os" - "path/filepath" "strings" "testing" @@ -13,7 +12,7 @@ import ( func ensureFiles(t *testing.T, rootDir string, files ...string) { for _, f := range files { - p := rootify(rootDir, f) + p := join(f, rootDir) _, err := os.Stat(p) assert.Nil(t, err, p) } @@ -28,10 +27,14 @@ func TestEnsureRoot(t *testing.T) { defer os.RemoveAll(tmpDir) // nolint: errcheck // create root dir - EnsureRoot(tmpDir) + throwaway := DefaultConfig() + throwaway.SetRootDir(tmpDir) + throwaway.EnsureDirs() + configPath := join(tmpDir, defaultConfigFilePath) + WriteConfigFile(configPath, throwaway) // make sure config is set properly - data, err := ioutil.ReadFile(filepath.Join(tmpDir, defaultConfigFilePath)) + data, err := ioutil.ReadFile(join(tmpDir, defaultConfigFilePath)) require.Nil(err) if !checkConfig(string(data)) { @@ -52,7 +55,7 @@ func TestEnsureTestRoot(t *testing.T) { rootDir := cfg.RootDir // make sure config is set properly - data, err := ioutil.ReadFile(filepath.Join(rootDir, defaultConfigFilePath)) + data, err := ioutil.ReadFile(join(rootDir, defaultConfigFilePath)) require.Nil(err) if !checkConfig(string(data)) { diff --git a/pkgs/bft/config/utils.go b/pkgs/bft/config/utils.go index 9dc96c26b46..5a6eec09e43 100644 --- a/pkgs/bft/config/utils.go +++ b/pkgs/bft/config/utils.go @@ -3,7 +3,7 @@ package config import "path/filepath" // helper function to make config creation independent of root dir -func rootify(path, root string) string { +func join(root, path string) string { if filepath.IsAbs(path) { return path } diff --git a/pkgs/bft/consensus/config/config.go b/pkgs/bft/consensus/config/config.go index 6358473b30b..67ed2c05504 100644 --- a/pkgs/bft/consensus/config/config.go +++ b/pkgs/bft/consensus/config/config.go @@ -111,7 +111,7 @@ func (cfg *ConsensusConfig) WalFile() string { if cfg.walFile != "" { return cfg.walFile } - return rootify(cfg.WalPath, cfg.RootDir) + return join(cfg.RootDir, cfg.WalPath) } // SetWalFile sets the path to the write-ahead log file diff --git a/pkgs/bft/consensus/config/utils.go b/pkgs/bft/consensus/config/utils.go index 9dc96c26b46..5ba8c40b082 100644 --- a/pkgs/bft/consensus/config/utils.go +++ b/pkgs/bft/consensus/config/utils.go @@ -2,8 +2,8 @@ package config import "path/filepath" -// helper function to make config creation independent of root dir -func rootify(path, root string) string { +// join path to root unless path is already absolute. +func join(root, path string) string { if filepath.IsAbs(path) { return path } diff --git a/pkgs/bft/mempool/config/config.go b/pkgs/bft/mempool/config/config.go index d5c58bbfd90..2ca0af39738 100644 --- a/pkgs/bft/mempool/config/config.go +++ b/pkgs/bft/mempool/config/config.go @@ -39,7 +39,7 @@ func TestMempoolConfig() *MempoolConfig { // WalDir returns the full path to the mempool's write-ahead log func (cfg *MempoolConfig) WalDir() string { - return rootify(cfg.WalPath, cfg.RootDir) + return join(cfg.RootDir, cfg.WalPath) } // WalEnabled returns true if the WAL is enabled. diff --git a/pkgs/bft/mempool/config/utils.go b/pkgs/bft/mempool/config/utils.go index 9dc96c26b46..5a6eec09e43 100644 --- a/pkgs/bft/mempool/config/utils.go +++ b/pkgs/bft/mempool/config/utils.go @@ -3,7 +3,7 @@ package config import "path/filepath" // helper function to make config creation independent of root dir -func rootify(path, root string) string { +func join(root, path string) string { if filepath.IsAbs(path) { return path } diff --git a/pkgs/bft/node/node_test.go b/pkgs/bft/node/node_test.go index 98af1db9d10..52c55794e4c 100644 --- a/pkgs/bft/node/node_test.go +++ b/pkgs/bft/node/node_test.go @@ -285,7 +285,7 @@ func TestNodeNewNodeCustomReactors(t *testing.T) { n, err := NewNode(config, privval.LoadOrGenFilePV(config.PrivValidatorKeyFile(), config.PrivValidatorStateFile()), nodeKey, - proxy.DefaultClientCreator(config.ProxyApp, config.ABCI, config.DBDir()), + proxy.DefaultClientCreator(nil, config.ProxyApp, config.ABCI, config.DBDir()), DefaultGenesisDocProviderFunc(config), DefaultDBProvider, log.TestingLogger(), diff --git a/pkgs/bft/rpc/config/config.go b/pkgs/bft/rpc/config/config.go index 253f11196fd..85459bbc611 100644 --- a/pkgs/bft/rpc/config/config.go +++ b/pkgs/bft/rpc/config/config.go @@ -149,7 +149,7 @@ func (cfg RPCConfig) KeyFile() string { if filepath.IsAbs(path) { return path } - return rootify(filepath.Join(defaultConfigDir, path), cfg.RootDir) + return join(cfg.RootDir, filepath.Join(defaultConfigDir, path)) } func (cfg RPCConfig) CertFile() string { @@ -157,7 +157,7 @@ func (cfg RPCConfig) CertFile() string { if filepath.IsAbs(path) { return path } - return rootify(filepath.Join(defaultConfigDir, path), cfg.RootDir) + return join(cfg.RootDir, filepath.Join(defaultConfigDir, path)) } func (cfg RPCConfig) IsTLSEnabled() bool { diff --git a/pkgs/bft/rpc/config/utils.go b/pkgs/bft/rpc/config/utils.go index 9dc96c26b46..5a6eec09e43 100644 --- a/pkgs/bft/rpc/config/utils.go +++ b/pkgs/bft/rpc/config/utils.go @@ -3,7 +3,7 @@ package config import "path/filepath" // helper function to make config creation independent of root dir -func rootify(path, root string) string { +func join(root, path string) string { if filepath.IsAbs(path) { return path } diff --git a/pkgs/bft/rpc/lib/test/main.go b/pkgs/bft/rpc/lib/test/main.go index 66e64c1b3a8..6b595d3b17c 100644 --- a/pkgs/bft/rpc/lib/test/main.go +++ b/pkgs/bft/rpc/lib/test/main.go @@ -31,7 +31,7 @@ func main() { ) // Stop upon receiving SIGTERM or CTRL-C. - osm.TrapSignal(logger, func() {}) + osm.TrapSignal(func() {}) rpcserver.RegisterRPCFuncs(mux, routes, logger) config := rpcserver.DefaultConfig() diff --git a/pkgs/bft/types/genesis.go b/pkgs/bft/types/genesis.go index fbddfa5bc79..5c3aa77f16e 100644 --- a/pkgs/bft/types/genesis.go +++ b/pkgs/bft/types/genesis.go @@ -125,3 +125,10 @@ func GenesisDocFromFile(genDocFile string) (*GenesisDoc, error) { } return genDoc, nil } + +//---------------------------------------- +// Mock AppState (for testing) + +type MockAppState struct { + AccountOwner string `json:"account_owner"` +} diff --git a/pkgs/bft/types/genesis_test.go b/pkgs/bft/types/genesis_test.go index 144baa92868..0c950fd837d 100644 --- a/pkgs/bft/types/genesis_test.go +++ b/pkgs/bft/types/genesis_test.go @@ -38,7 +38,7 @@ func TestGenesisBad(t *testing.T) { func TestGenesisGood(t *testing.T) { // test a good one by raw json - genDocBytes := []byte(`{"genesis_time":"0001-01-01T00:00:00Z","chain_id":"test-chain-QDKdJr","consensus_params":null,"validators":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"AT/+aaL1eB0477Mud9JMm8Sh8BIvOYlPGC9KkIUmFaE="},"power":"10","name":""}],"app_hash":"","app_state":{"account_owner": "Bob"}}`) + genDocBytes := []byte(`{"genesis_time":"0001-01-01T00:00:00Z","chain_id":"test-chain-QDKdJr","consensus_params":null,"validators":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"AT/+aaL1eB0477Mud9JMm8Sh8BIvOYlPGC9KkIUmFaE="},"power":"10","name":""}],"app_hash":"","app_state":{"@type":"/tm.MockAppState","account_owner":"Bob"}}`) _, err := GenesisDocFromJSON(genDocBytes) assert.NoError(t, err, "expected no error for good genDoc json") diff --git a/pkgs/bft/types/package.go b/pkgs/bft/types/package.go index 97600aedb5c..9a795b88d38 100644 --- a/pkgs/bft/types/package.go +++ b/pkgs/bft/types/package.go @@ -47,4 +47,5 @@ var Package = amino.RegisterPackage(amino.NewPackage( // Misc. TxResult{}, + MockAppState{}, )) diff --git a/pkgs/sdk/baseapp_test.go b/pkgs/sdk/baseapp_test.go index 9c26e9b2c13..46e81800728 100644 --- a/pkgs/sdk/baseapp_test.go +++ b/pkgs/sdk/baseapp_test.go @@ -287,7 +287,7 @@ func TestInitChainer(t *testing.T) { require.Nil(t, err) require.Equal(t, int64(0), app.LastBlockHeight()) - app.InitChain(abci.RequestInitChain{AppStateBytes: []byte("{}"), ChainID: "test-chain-id"}) // must have valid JSON genesis file, even if empty + app.InitChain(abci.RequestInitChain{AppState: nil, ChainID: "test-chain-id"}) // must have valid JSON genesis file, even if empty // assert that chainID is set correctly in InitChain chainID := app.deliverState.ctx.ChainID()