From 46151720f842a17fdc368a60abd8b0dcc884304a Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Thu, 4 May 2017 22:33:08 -0400 Subject: [PATCH] fix tests --- cmd/tendermint/commands/root.go | 2 +- config/config.go | 33 +++++++++++++++++++++++++++++--- config/toml.go | 10 +++++++++- consensus/common_test.go | 34 ++++++++++----------------------- consensus/replay_test.go | 24 ++++++++++++----------- consensus/state_test.go | 8 ++++---- mempool/mempool_test.go | 18 ++--------------- node/node_test.go | 5 ++--- p2p/switch_test.go | 6 ++++-- rpc/test/helpers.go | 22 ++++++++++----------- state/state_test.go | 31 ++++++++++-------------------- 11 files changed, 96 insertions(+), 97 deletions(-) diff --git a/cmd/tendermint/commands/root.go b/cmd/tendermint/commands/root.go index 5771a9b29..fd369d934 100644 --- a/cmd/tendermint/commands/root.go +++ b/cmd/tendermint/commands/root.go @@ -23,7 +23,7 @@ var RootCmd = &cobra.Command{ Short: "Tendermint Core (BFT Consensus) in Go", PersistentPreRunE: func(cmd *cobra.Command, args []string) error { err := viper.Unmarshal(config) - cfg.SetRoot(config, config.RootDir) + config.SetRoot(config.RootDir) cfg.EnsureRoot(config.RootDir) logger.SetLogLevel(config.LogLevel) return err diff --git a/config/config.go b/config/config.go index 178f19dbc..2c78524b5 100644 --- a/config/config.go +++ b/config/config.go @@ -28,19 +28,20 @@ func DefaultConfig() *Config { func TestConfig() *Config { return &Config{ - BaseConfig: DefaultBaseConfig(), - P2P: DefaultP2PConfig(), + BaseConfig: TestBaseConfig(), + P2P: TestP2PConfig(), Mempool: DefaultMempoolConfig(), Consensus: TestConsensusConfig(), } } // Set the RootDir for all Config structs -func SetRoot(cfg *Config, root string) { +func (cfg *Config) SetRoot(root string) *Config { cfg.BaseConfig.RootDir = root cfg.P2P.RootDir = root cfg.Mempool.RootDir = root cfg.Consensus.RootDir = root + return cfg } // BaseConfig struct for a Tendermint node @@ -119,6 +120,17 @@ func DefaultBaseConfig() BaseConfig { } } +func TestBaseConfig() BaseConfig { + conf := DefaultBaseConfig() + conf.ChainID = "tendermint_test" + conf.ProxyApp = "dummy" + conf.FastSync = false + conf.DBBackend = "memdb" + conf.RPCListenAddress = "tcp://0.0.0.0:36657" + conf.GRPCListenAddress = "tcp://0.0.0.0:36658" + return conf +} + func (b BaseConfig) GenesisFile() string { return rootify(b.Genesis, b.RootDir) } @@ -151,6 +163,13 @@ func DefaultP2PConfig() *P2PConfig { } } +func TestP2PConfig() *P2PConfig { + conf := DefaultP2PConfig() + conf.ListenAddress = "tcp://0.0.0.0:36656" + conf.SkipUPNP = true + return conf +} + func (p *P2PConfig) AddrBookFile() string { return rootify(p.AddrBook, p.RootDir) } @@ -182,6 +201,7 @@ type ConsensusConfig struct { RootDir string `mapstructure:"home"` WalPath string `mapstructure:"wal_file"` WalLight bool `mapstructure:"wal_light"` + walFile string // overrides WalPath if set // All timeouts are in ms TimeoutPropose int `mapstructure:"timeout_propose"` @@ -256,9 +276,16 @@ func TestConsensusConfig() *ConsensusConfig { } func (c *ConsensusConfig) WalFile() string { + if c.walFile != "" { + return c.walFile + } return rootify(c.WalPath, c.RootDir) } +func (c *ConsensusConfig) SetWalFile(walFile string) { + c.walFile = walFile +} + // helper function to make config creation independent of root dir func rootify(path, root string) string { if filepath.IsAbs(path) { diff --git a/config/toml.go b/config/toml.go index aff6bd1aa..d07b3281f 100644 --- a/config/toml.go +++ b/config/toml.go @@ -3,9 +3,11 @@ package config import ( "os" "path" + "path/filepath" "strings" cmn "github.com/tendermint/tmlibs/common" + "github.com/tendermint/tmlibs/logger" ) /****** these are for production settings ***********/ @@ -44,7 +46,9 @@ func defaultConfig(moniker string) (defaultConfig string) { /****** these are for test settings ***********/ -func initTestRoot(rootDir string) { +func ResetTestRoot(testName string) *Config { + rootDir := os.ExpandEnv("$HOME/.tendermint_test") + rootDir = filepath.Join(rootDir, testName) // Remove ~/.tendermint_test_bak if cmn.FileExists(rootDir + "_bak") { err := os.RemoveAll(rootDir + "_bak") @@ -77,6 +81,10 @@ func initTestRoot(rootDir string) { } // we always overwrite the priv val cmn.MustWriteFile(privFilePath, []byte(testPrivValidator), 0644) + + config := TestConfig().SetRoot(rootDir) + logger.SetLogLevel(config.LogLevel) + return config } var testConfigTmpl = `# This is a TOML config file. diff --git a/consensus/common_test.go b/consensus/common_test.go index ed7be9f36..463b748f8 100644 --- a/consensus/common_test.go +++ b/consensus/common_test.go @@ -15,7 +15,6 @@ import ( abci "github.com/tendermint/abci/types" bc "github.com/tendermint/tendermint/blockchain" cfg "github.com/tendermint/tendermint/config" - "github.com/tendermint/tendermint/config/tendermint_test" mempl "github.com/tendermint/tendermint/mempool" "github.com/tendermint/tendermint/p2p" sm "github.com/tendermint/tendermint/state" @@ -28,7 +27,7 @@ import ( ) // genesis, chain_id, priv_val -var config *NodeConfig // NOTE: must be reset for each _test.go file +var config *cfg.Config // NOTE: must be reset for each _test.go file var ensureTimeout = time.Duration(2) func ensureDir(dir string, mode os.FileMode) { @@ -37,21 +36,8 @@ func ensureDir(dir string, mode os.FileMode) { } } -type NodeConfig struct { - cfg.Config `mapstructure:",squash"` - P2P *p2p.Config `mapstructure:"p2p"` - Mempool *mempl.Config `mapstructure:"mempool"` - Consensus *Config `mapstructure:"consensus"` -} - -// TODO: This is the same as NodeConfig. Should we move the configs to types (?) -func ResetConfig(name string) *NodeConfig { - viperConfig := tendermint_test.ResetConfig(name) - config := new(NodeConfig) - if err := viperConfig.Unmarshal(config); err != nil { - panic(err) - } - return config +func ResetConfig(name string) *cfg.Config { + return cfg.ResetTestRoot(name) } //------------------------------------------------------------------------------- @@ -251,7 +237,7 @@ func newConsensusState(state *sm.State, pv *types.PrivValidator, app abci.Applic return newConsensusStateWithConfig(config, state, pv, app) } -func newConsensusStateWithConfig(thisConfig *NodeConfig, state *sm.State, pv *types.PrivValidator, app abci.Application) *ConsensusState { +func newConsensusStateWithConfig(thisConfig *cfg.Config, state *sm.State, pv *types.PrivValidator, app abci.Application) *ConsensusState { // Get BlockStore blockDB := dbm.NewMemDB() blockStore := bc.NewBlockStore(blockDB) @@ -274,8 +260,8 @@ func newConsensusStateWithConfig(thisConfig *NodeConfig, state *sm.State, pv *ty return cs } -func loadPrivValidator(config *NodeConfig) *types.PrivValidator { - privValidatorFile := config.PrivValidatorFile +func loadPrivValidator(config *cfg.Config) *types.PrivValidator { + privValidatorFile := config.PrivValidatorFile() ensureDir(path.Dir(privValidatorFile), 0700) privValidator := types.LoadOrGenPrivValidator(privValidatorFile) privValidator.Reset() @@ -284,7 +270,7 @@ func loadPrivValidator(config *NodeConfig) *types.PrivValidator { func fixedConsensusState() *ConsensusState { stateDB := dbm.NewMemDB() - state := sm.MakeGenesisStateFromFile(stateDB, config.GenesisFile) + state := sm.MakeGenesisStateFromFile(stateDB, config.GenesisFile()) privValidator := loadPrivValidator(config) cs := newConsensusState(state, privValidator, counter.NewCounterApplication(true)) return cs @@ -292,7 +278,7 @@ func fixedConsensusState() *ConsensusState { func fixedConsensusStateDummy() *ConsensusState { stateDB := dbm.NewMemDB() - state := sm.MakeGenesisStateFromFile(stateDB, config.GenesisFile) + state := sm.MakeGenesisStateFromFile(stateDB, config.GenesisFile()) privValidator := loadPrivValidator(config) cs := newConsensusState(state, privValidator, dummy.NewDummyApplication()) return cs @@ -338,7 +324,7 @@ func randConsensusNet(nValidators int, testName string, tickerFunc func() Timeou state := sm.MakeGenesisState(db, genDoc) state.Save() thisConfig := ResetConfig(Fmt("%s_%d", testName, i)) - ensureDir(path.Dir(thisConfig.Consensus.WalFile), 0700) // dir for wal + ensureDir(path.Dir(thisConfig.Consensus.WalFile()), 0700) // dir for wal css[i] = newConsensusStateWithConfig(thisConfig, state, privVals[i], appFunc()) css[i].SetTimeoutTicker(tickerFunc()) } @@ -354,7 +340,7 @@ func randConsensusNetWithPeers(nValidators, nPeers int, testName string, tickerF state := sm.MakeGenesisState(db, genDoc) state.Save() thisConfig := ResetConfig(Fmt("%s_%d", testName, i)) - ensureDir(path.Dir(thisConfig.Consensus.WalFile), 0700) // dir for wal + ensureDir(path.Dir(thisConfig.Consensus.WalFile()), 0700) // dir for wal var privVal *types.PrivValidator if i < nValidators { privVal = privVals[i] diff --git a/consensus/replay_test.go b/consensus/replay_test.go index 9ab04359c..778e5c458 100644 --- a/consensus/replay_test.go +++ b/consensus/replay_test.go @@ -15,11 +15,13 @@ import ( "github.com/tendermint/abci/example/dummy" "github.com/tendermint/go-crypto" "github.com/tendermint/go-wire" + cmn "github.com/tendermint/tmlibs/common" + dbm "github.com/tendermint/tmlibs/db" + + cfg "github.com/tendermint/tendermint/config" "github.com/tendermint/tendermint/proxy" sm "github.com/tendermint/tendermint/state" "github.com/tendermint/tendermint/types" - cmn "github.com/tendermint/tmlibs/common" - dbm "github.com/tendermint/tmlibs/db" ) func init() { @@ -133,7 +135,7 @@ func waitForBlock(newBlockCh chan interface{}, thisCase *testCase, i int) { func runReplayTest(t *testing.T, cs *ConsensusState, walFile string, newBlockCh chan interface{}, thisCase *testCase, i int) { - cs.config.WalFile = walFile + cs.config.SetWalFile(walFile) started, err := cs.Start() if err != nil { t.Fatalf("Cannot start consensus: %v", err) @@ -314,9 +316,9 @@ func testHandshakeReplay(t *testing.T, nBlocks int, mode uint) { t.Fatal(err) } walFile := writeWAL(string(walBody)) - config.Consensus.WalFile = walFile + config.Consensus.SetWalFile(walFile) - privVal := types.LoadPrivValidator(config.PrivValidatorFile) + privVal := types.LoadPrivValidator(config.PrivValidatorFile()) testPartSize = config.Consensus.BlockPartSize wal, err := NewWAL(walFile, false) @@ -336,7 +338,7 @@ func testHandshakeReplay(t *testing.T, nBlocks int, mode uint) { latestAppHash := buildTMStateFromChain(config, state, chain, mode) // make a new client creator - dummyApp := dummy.NewPersistentDummyApplication(path.Join(config.DBDir, "2")) + dummyApp := dummy.NewPersistentDummyApplication(path.Join(config.DBDir(), "2")) clientCreator2 := proxy.NewLocalClientCreator(dummyApp) if nBlocks > 0 { // run nBlocks against a new client to build up the app state. @@ -415,9 +417,9 @@ func buildAppStateFromChain(proxyApp proxy.AppConns, } -func buildTMStateFromChain(config *NodeConfig, state *sm.State, chain []*types.Block, mode uint) []byte { +func buildTMStateFromChain(config *cfg.Config, state *sm.State, chain []*types.Block, mode uint) []byte { // run the whole chain against this client to build up the tendermint state - clientCreator := proxy.NewLocalClientCreator(dummy.NewPersistentDummyApplication(path.Join(config.DBDir, "1"))) + clientCreator := proxy.NewLocalClientCreator(dummy.NewPersistentDummyApplication(path.Join(config.DBDir(), "1"))) proxyApp := proxy.NewAppConns(clientCreator, nil) // sm.NewHandshaker(config, state, store, ReplayLastBlock)) if _, err := proxyApp.Start(); err != nil { panic(err) @@ -612,7 +614,7 @@ func makeBlockchain(t *testing.T, chainID string, nBlocks int, privVal *types.Pr } // fresh state and mock store -func stateAndStore(config *NodeConfig, pubKey crypto.PubKey) (*sm.State, *mockBlockStore) { +func stateAndStore(config *cfg.Config, pubKey crypto.PubKey) (*sm.State, *mockBlockStore) { stateDB := dbm.NewMemDB() state := sm.MakeGenesisStateFromFile(stateDB, config.GenesisFile()) store := NewMockBlockStore(config) @@ -623,13 +625,13 @@ func stateAndStore(config *NodeConfig, pubKey crypto.PubKey) (*sm.State, *mockBl // mock block store type mockBlockStore struct { - config *NodeConfig + config *cfg.Config chain []*types.Block commits []*types.Commit } // TODO: NewBlockStore(db.NewMemDB) ... -func NewMockBlockStore(config *NodeConfig) *mockBlockStore { +func NewMockBlockStore(config *cfg.Config) *mockBlockStore { return &mockBlockStore{config, nil, nil} } diff --git a/consensus/state_test.go b/consensus/state_test.go index 990622738..f4eed5732 100644 --- a/consensus/state_test.go +++ b/consensus/state_test.go @@ -14,8 +14,8 @@ func init() { config = ResetConfig("consensus_state_test") } -func (config *Config) ensureProposeTimeout() time.Duration { - return time.Duration(config.TimeoutPropose*2) * time.Millisecond +func ensureProposeTimeout(timeoutPropose int) time.Duration { + return time.Duration(timeoutPropose*2) * time.Millisecond } /* @@ -125,7 +125,7 @@ func TestEnterProposeNoPrivValidator(t *testing.T) { startTestRound(cs, height, round) // if we're not a validator, EnterPropose should timeout - ticker := time.NewTicker(cs.config.ensureProposeTimeout()) + ticker := time.NewTicker(ensureProposeTimeout(cs.config.TimeoutPropose)) select { case <-timeoutCh: case <-ticker.C: @@ -166,7 +166,7 @@ func TestEnterProposeYesPrivValidator(t *testing.T) { } // if we're a validator, enterPropose should not timeout - ticker := time.NewTicker(cs.config.ensureProposeTimeout()) + ticker := time.NewTicker(ensureProposeTimeout(cs.config.TimeoutPropose)) select { case <-timeoutCh: panic("Expected EnterPropose not to timeout") diff --git a/mempool/mempool_test.go b/mempool/mempool_test.go index a1bd188d4..dd15493d0 100644 --- a/mempool/mempool_test.go +++ b/mempool/mempool_test.go @@ -6,33 +6,19 @@ import ( "github.com/tendermint/abci/example/counter" cfg "github.com/tendermint/tendermint/config" - "github.com/tendermint/tendermint/config/tendermint_test" "github.com/tendermint/tendermint/proxy" "github.com/tendermint/tendermint/types" ) -func ResetConfig(name string) *Config { - viperConfig := tendermint_test.ResetConfig(name) - config := new(struct { - cfg.Config `mapstructure:",squash"` - Mempool *Config `mapstructure:"mempool"` - }) - if err := viperConfig.Unmarshal(config); err != nil { - panic(err) - } - return config.Mempool - -} - func TestSerialReap(t *testing.T) { - config := ResetConfig("mempool_test") + config := cfg.ResetTestRoot("mempool_test") app := counter.NewCounterApplication(true) app.SetOption("serial", "on") cc := proxy.NewLocalClientCreator(app) appConnMem, _ := cc.NewABCIClient() appConnCon, _ := cc.NewABCIClient() - mempool := NewMempool(config, appConnMem) + mempool := NewMempool(config.Mempool, appConnMem) deliverTxsRange := func(start, end int) { // Deliver some txs. diff --git a/node/node_test.go b/node/node_test.go index ba9ac7234..e00a67ecb 100644 --- a/node/node_test.go +++ b/node/node_test.go @@ -4,12 +4,11 @@ import ( "testing" "time" - "github.com/tendermint/tendermint/config/tendermint_test" + cfg "github.com/tendermint/tendermint/config" ) func TestNodeStartStop(t *testing.T) { - viperConfig := tendermint_test.ResetConfig("node_node_test") - config := ConfigFromViper(viperConfig) + config := cfg.ResetTestRoot("node_node_test") // Create & start node n := NewNodeDefault(config) diff --git a/p2p/switch_test.go b/p2p/switch_test.go index d2f616922..da5f43aa3 100644 --- a/p2p/switch_test.go +++ b/p2p/switch_test.go @@ -13,14 +13,16 @@ import ( crypto "github.com/tendermint/go-crypto" wire "github.com/tendermint/go-wire" cmn "github.com/tendermint/tmlibs/common" + + cfg "github.com/tendermint/tendermint/config" ) var ( - config *Config + config *cfg.P2PConfig ) func init() { - config = NewDefaultConfig("") + config = cfg.DefaultP2PConfig() config.PexReactor = true } diff --git a/rpc/test/helpers.go b/rpc/test/helpers.go index c5542fa7c..034816cd1 100644 --- a/rpc/test/helpers.go +++ b/rpc/test/helpers.go @@ -14,7 +14,7 @@ import ( logger "github.com/tendermint/tmlibs/logger" abci "github.com/tendermint/abci/types" - "github.com/tendermint/tendermint/config/tendermint_test" + cfg "github.com/tendermint/tendermint/config" nm "github.com/tendermint/tendermint/node" "github.com/tendermint/tendermint/proxy" ctypes "github.com/tendermint/tendermint/rpc/core/types" @@ -23,7 +23,7 @@ import ( "github.com/tendermint/tendermint/types" ) -var config *nm.Config +var config *cfg.Config const tmLogLevel = "error" @@ -53,19 +53,19 @@ func makeAddrs() (string, string, string) { } // GetConfig returns a config for the test cases as a singleton -func GetConfig() *nm.Config { +func GetConfig() *cfg.Config { if config == nil { pathname := makePathname() - viperConfig := tendermint_test.ResetConfig(pathname) - // Shut up the logging - logger.SetLogLevel(tmLogLevel) + config = cfg.ResetTestRoot(pathname) + // and we use random ports to run in parallel tm, rpc, grpc := makeAddrs() - viperConfig.Set("p2p.laddr", tm) - viperConfig.Set("rpc_laddr", rpc) - viperConfig.Set("grpc_laddr", grpc) + config.P2P.ListenAddress = tm + config.RPCListenAddress = rpc + config.GRPCListenAddress = grpc - config = nm.ConfigFromViper(viperConfig) + // Shut up the logging + logger.SetLogLevel(tmLogLevel) } return config } @@ -108,7 +108,7 @@ func StartTendermint(app abci.Application) *nm.Node { func NewTendermint(app abci.Application) *nm.Node { // Create & start node config := GetConfig() - privValidatorFile := config.PrivValidatorFile + privValidatorFile := config.PrivValidatorFile() privValidator := types.LoadOrGenPrivValidator(privValidatorFile) papp := proxy.NewLocalClientCreator(app) node := nm.NewNode(config, privValidator, papp) diff --git a/state/state_test.go b/state/state_test.go index 97df5cab4..10bf07938 100644 --- a/state/state_test.go +++ b/state/state_test.go @@ -8,26 +8,15 @@ import ( abci "github.com/tendermint/abci/types" "github.com/tendermint/go-crypto" cfg "github.com/tendermint/tendermint/config" - "github.com/tendermint/tendermint/config/tendermint_test" dbm "github.com/tendermint/tmlibs/db" ) -func ResetConfig(name string) *cfg.Config { - viperConfig := tendermint_test.ResetConfig(name) - config := new(struct { - cfg.Config `mapstructure:",squash"` - }) - if err := viperConfig.Unmarshal(config); err != nil { - panic(err) - } - return &config.Config -} - func TestStateCopyEquals(t *testing.T) { - config := ResetConfig("state_") + config := cfg.ResetTestRoot("state_") + // Get State db - stateDB := dbm.NewDB("state", config.DBBackend, config.DBDir) - state := GetState(stateDB, config.GenesisFile) + stateDB := dbm.NewDB("state", config.DBBackend, config.DBDir()) + state := GetState(stateDB, config.GenesisFile()) stateCopy := state.Copy() @@ -43,10 +32,10 @@ func TestStateCopyEquals(t *testing.T) { } func TestStateSaveLoad(t *testing.T) { - config := ResetConfig("state_") + config := cfg.ResetTestRoot("state_") // Get State db - stateDB := dbm.NewDB("state", config.DBBackend, config.DBDir) - state := GetState(stateDB, config.GenesisFile) + stateDB := dbm.NewDB("state", config.DBBackend, config.DBDir()) + state := GetState(stateDB, config.GenesisFile()) state.LastBlockHeight += 1 state.Save() @@ -60,9 +49,9 @@ func TestStateSaveLoad(t *testing.T) { func TestABCIResponsesSaveLoad(t *testing.T) { assert := assert.New(t) - config := ResetConfig("state_") - stateDB := dbm.NewDB("state", config.DBBackend, config.DBDir) - state := GetState(stateDB, config.GenesisFile) + config := cfg.ResetTestRoot("state_") + stateDB := dbm.NewDB("state", config.DBBackend, config.DBDir()) + state := GetState(stateDB, config.GenesisFile()) state.LastBlockHeight += 1