From 02a2ce6b106231dc718e98e365f0dad861b35cc6 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Sun, 29 Nov 2020 12:07:15 +0000 Subject: [PATCH] WIP --- blockchain/v0/reactor_test.go | 19 ++--- blockchain/v1/reactor_test.go | 20 ++--- blockchain/v2/reactor_test.go | 7 +- config/toml.go | 56 +++++++++++++- config/toml_test.go | 19 +++++ state/state_test.go | 56 +++++--------- store/store_test.go | 96 ++++++++++++++---------- test/maverick/consensus/wal_generator.go | 2 +- 8 files changed, 166 insertions(+), 109 deletions(-) diff --git a/blockchain/v0/reactor_test.go b/blockchain/v0/reactor_test.go index a88b499f4..b3a8bbda3 100644 --- a/blockchain/v0/reactor_test.go +++ b/blockchain/v0/reactor_test.go @@ -2,7 +2,6 @@ package v0 import ( "fmt" - "os" "sort" "testing" "time" @@ -132,8 +131,7 @@ func newBlockchainReactor( } func TestNoBlockResponse(t *testing.T) { - config = cfg.ResetTestRoot("blockchain_reactor_test") - defer os.RemoveAll(config.RootDir) + config = cfg.SetupTestConfiguration(t) genDoc, privVals := randGenesisDoc(1, false, 30) maxBlockHeight := int64(65) @@ -149,14 +147,14 @@ func TestNoBlockResponse(t *testing.T) { }, p2p.Connect2Switches) - defer func() { + t.Cleanup(func() { for _, r := range reactorPairs { err := r.reactor.Stop() require.NoError(t, err) err = r.app.Stop() require.NoError(t, err) } - }() + }) tests := []struct { height int64 @@ -194,8 +192,7 @@ func TestNoBlockResponse(t *testing.T) { // Alternatively we could actually dial a TCP conn but // that seems extreme. func TestBadBlockStopsPeer(t *testing.T) { - config = cfg.ResetTestRoot("blockchain_reactor_test") - defer os.RemoveAll(config.RootDir) + config = cfg.SetupTestConfiguration(t) genDoc, privVals := randGenesisDoc(1, false, 30) maxBlockHeight := int64(148) @@ -204,12 +201,12 @@ func TestBadBlockStopsPeer(t *testing.T) { otherGenDoc, otherPrivVals := randGenesisDoc(1, false, 30) otherChain := newBlockchainReactor(log.TestingLogger(), otherGenDoc, otherPrivVals, maxBlockHeight) - defer func() { + t.Cleanup(func() { err := otherChain.reactor.Stop() require.Error(t, err) err = otherChain.app.Stop() require.NoError(t, err) - }() + }) reactorPairs := make([]BlockchainReactorPair, 4) @@ -224,7 +221,7 @@ func TestBadBlockStopsPeer(t *testing.T) { }, p2p.Connect2Switches) - defer func() { + t.Cleanup(func() { for _, r := range reactorPairs { err := r.reactor.Stop() require.NoError(t, err) @@ -232,7 +229,7 @@ func TestBadBlockStopsPeer(t *testing.T) { err = r.app.Stop() require.NoError(t, err) } - }() + }) for { time.Sleep(1 * time.Second) diff --git a/blockchain/v1/reactor_test.go b/blockchain/v1/reactor_test.go index c0f371905..5e0893f9d 100644 --- a/blockchain/v1/reactor_test.go +++ b/blockchain/v1/reactor_test.go @@ -2,7 +2,6 @@ package v1 import ( "fmt" - "os" "sort" "sync" "testing" @@ -180,9 +179,7 @@ func (conR *consensusReactorTest) SwitchToConsensus(state sm.State, blocksSynced } func TestFastSyncNoBlockResponse(t *testing.T) { - - config = cfg.ResetTestRoot("blockchain_new_reactor_test") - defer os.RemoveAll(config.RootDir) + config = cfg.SetupTestConfiguration(t) genDoc, privVals := randGenesisDoc(1, false, 30) maxBlockHeight := int64(65) @@ -203,12 +200,12 @@ func TestFastSyncNoBlockResponse(t *testing.T) { }, p2p.Connect2Switches) - defer func() { + t.Cleanup(func() { for _, r := range reactorPairs { _ = r.bcR.Stop() _ = r.conR.Stop() } - }() + }) tests := []struct { height int64 @@ -251,15 +248,14 @@ func TestFastSyncBadBlockStopsPeer(t *testing.T) { numNodes := 4 maxBlockHeight := int64(148) - config = cfg.ResetTestRoot("blockchain_reactor_test") - defer os.RemoveAll(config.RootDir) + config = cfg.SetupTestConfiguration(t) genDoc, privVals := randGenesisDoc(1, false, 30) otherChain := newBlockchainReactorPair(t, log.TestingLogger(), genDoc, privVals, maxBlockHeight) - defer func() { + t.Cleanup(func() { _ = otherChain.bcR.Stop() _ = otherChain.conR.Stop() - }() + }) reactorPairs := make([]BlockchainReactorPair, numNodes) logger := make([]log.Logger, numNodes) @@ -284,12 +280,12 @@ func TestFastSyncBadBlockStopsPeer(t *testing.T) { }, p2p.Connect2Switches) - defer func() { + t.Cleanup(func() { for _, r := range reactorPairs { _ = r.bcR.Stop() _ = r.conR.Stop() } - }() + }) outerFor: for { diff --git a/blockchain/v2/reactor_test.go b/blockchain/v2/reactor_test.go index 35cedf178..81238e94c 100644 --- a/blockchain/v2/reactor_test.go +++ b/blockchain/v2/reactor_test.go @@ -3,7 +3,6 @@ package v2 import ( "fmt" "net" - "os" "sort" "sync" "testing" @@ -351,8 +350,7 @@ func TestReactorHelperMode(t *testing.T) { channelID = byte(0x40) ) - config := cfg.ResetTestRoot("blockchain_reactor_v2_test") - defer os.RemoveAll(config.RootDir) + config := cfg.SetupTestConfiguration(t) genDoc, privVals := randGenesisDoc(config.ChainID(), 1, false, 30) params := testReactorParams{ @@ -427,8 +425,7 @@ func TestReactorHelperMode(t *testing.T) { } func TestReactorSetSwitchNil(t *testing.T) { - config := cfg.ResetTestRoot("blockchain_reactor_v2_test") - defer os.RemoveAll(config.RootDir) + config := cfg.SetupTestConfiguration(t) genDoc, privVals := randGenesisDoc(config.ChainID(), 1, false, 30) reactor := newTestReactor(testReactorParams{ diff --git a/config/toml.go b/config/toml.go index 19973a3fb..3df7051ee 100644 --- a/config/toml.go +++ b/config/toml.go @@ -3,13 +3,12 @@ package config import ( "bytes" "fmt" + tmos "github.com/tendermint/tendermint/libs/os" "io/ioutil" "os" "path/filepath" "strings" "text/template" - - tmos "github.com/tendermint/tendermint/libs/os" ) // DefaultDirPerm is the default permissions used when creating directories. @@ -458,6 +457,59 @@ max_open_connections = {{ .Instrumentation.MaxOpenConnections }} namespace = "{{ .Instrumentation.Namespace }}" ` +type T interface { + TempDir() string + Fatal(...interface{}) + Fatalf(string, ...interface{}) +} + +func SetupTestConfiguration(t T) *Config { + return SetupTestConfigurationWithChainID(t, "") +} + +func SetupTestConfigurationWithChainID(t T, chainID string) *Config { + // create a unique, concurrency-safe test directory under os.TempDir() + rootDir := t.TempDir() + // ensure config and data subdirs are created + if err := tmos.EnsureDir(filepath.Join(rootDir, defaultConfigDir), DefaultDirPerm); err != nil { + t.Fatal(err) + } + if err := tmos.EnsureDir(filepath.Join(rootDir, defaultDataDir), DefaultDirPerm); err != nil { + t.Fatal(err) + } + + baseConfig := DefaultBaseConfig() + configFilePath := filepath.Join(rootDir, defaultConfigFilePath) + genesisFilePath := filepath.Join(rootDir, baseConfig.Genesis) + privKeyFilePath := filepath.Join(rootDir, baseConfig.PrivValidatorKey) + privStateFilePath := filepath.Join(rootDir, baseConfig.PrivValidatorState) + + // Write default config file if missing. + if !tmos.FileExists(configFilePath) { + writeDefaultConfigFile(configFilePath) + } + if !tmos.FileExists(genesisFilePath) { + if chainID == "" { + chainID = "tendermint_test" + } + testGenesis := fmt.Sprintf(testGenesisFmt, chainID) + mustWriteFileHelper(t, genesisFilePath, []byte(testGenesis), 0644) + } + // we always overwrite the priv val + mustWriteFileHelper(t, privKeyFilePath, []byte(testPrivValidatorKey), 0644) + mustWriteFileHelper(t, privStateFilePath, []byte(testPrivValidatorState), 0644) + + config := TestConfig().SetRoot(rootDir) + return config +} + +func mustWriteFileHelper(t T, filePath string, contents []byte, mode os.FileMode) { + if err := ioutil.WriteFile(filePath, contents, mode); err != nil { + t.Fatalf("failed to write file: %v", err) + } +} + + /****** these are for test settings ***********/ func ResetTestRoot(testName string) *Config { diff --git a/config/toml_test.go b/config/toml_test.go index f19710687..e18195f6b 100644 --- a/config/toml_test.go +++ b/config/toml_test.go @@ -41,6 +41,25 @@ func TestEnsureRoot(t *testing.T) { ensureFiles(t, tmpDir, "data") } +func TestSetupConfiguration(t *testing.T) { + require := require.New(t) + // create root dir + cfg := SetupTestConfiguration(t) + rootDir := cfg.RootDir + + // make sure config is set properly + data, err := ioutil.ReadFile(filepath.Join(rootDir, defaultConfigFilePath)) + require.Nil(err) + + if !checkConfig(string(data)) { + t.Fatalf("config file missing some information") + } + + // TODO: make sure the cfg returned and testconfig are the same! + baseConfig := DefaultBaseConfig() + ensureFiles(t, rootDir, defaultDataDir, baseConfig.Genesis, baseConfig.PrivValidatorKey, baseConfig.PrivValidatorState) +} + func TestEnsureTestRoot(t *testing.T) { require := require.New(t) diff --git a/state/state_test.go b/state/state_test.go index 1632f4304..3acc7308a 100644 --- a/state/state_test.go +++ b/state/state_test.go @@ -5,7 +5,6 @@ import ( "fmt" "math" "math/big" - "os" "testing" "github.com/stretchr/testify/assert" @@ -25,8 +24,8 @@ import ( ) // setupTestCase does setup common to all test cases. -func setupTestCase(t *testing.T) (func(t *testing.T), dbm.DB, sm.State) { - config := cfg.ResetTestRoot("state_") +func setupTestCase(t *testing.T) (dbm.DB, sm.State) { + config := cfg.SetupTestConfiguration(t) dbType := dbm.BackendType(config.DBBackend) stateDB, err := dbm.NewDB("state", dbType, config.DBDir()) stateStore := sm.NewStore(stateDB) @@ -36,15 +35,12 @@ func setupTestCase(t *testing.T) (func(t *testing.T), dbm.DB, sm.State) { err = stateStore.Save(state) require.NoError(t, err) - tearDown := func(t *testing.T) { os.RemoveAll(config.RootDir) } - - return tearDown, stateDB, state + return stateDB, state } // TestStateCopy tests the correct copying behaviour of State. func TestStateCopy(t *testing.T) { - tearDown, _, state := setupTestCase(t) - defer tearDown(t) + _, state := setupTestCase(t) assert := assert.New(t) stateCopy := state.Copy() @@ -74,8 +70,7 @@ func TestMakeGenesisStateNilValidators(t *testing.T) { // TestStateSaveLoad tests saving and loading State from a db. func TestStateSaveLoad(t *testing.T) { - tearDown, stateDB, state := setupTestCase(t) - defer tearDown(t) + stateDB, state := setupTestCase(t) stateStore := sm.NewStore(stateDB) assert := assert.New(t) @@ -93,8 +88,7 @@ func TestStateSaveLoad(t *testing.T) { // TestABCIResponsesSaveLoad tests saving and loading ABCIResponses. func TestABCIResponsesSaveLoad1(t *testing.T) { - tearDown, stateDB, state := setupTestCase(t) - defer tearDown(t) + stateDB, state := setupTestCase(t) stateStore := sm.NewStore(stateDB) assert := assert.New(t) @@ -124,8 +118,7 @@ func TestABCIResponsesSaveLoad1(t *testing.T) { // TestResultsSaveLoad tests saving and loading ABCI results. func TestABCIResponsesSaveLoad2(t *testing.T) { - tearDown, stateDB, _ := setupTestCase(t) - defer tearDown(t) + stateDB, _ := setupTestCase(t) assert := assert.New(t) stateStore := sm.NewStore(stateDB) @@ -212,8 +205,7 @@ func TestABCIResponsesSaveLoad2(t *testing.T) { // TestValidatorSimpleSaveLoad tests saving and loading validators. func TestValidatorSimpleSaveLoad(t *testing.T) { - tearDown, stateDB, state := setupTestCase(t) - defer tearDown(t) + stateDB, state := setupTestCase(t) assert := assert.New(t) statestore := sm.NewStore(stateDB) @@ -247,8 +239,7 @@ func TestValidatorSimpleSaveLoad(t *testing.T) { // TestValidatorChangesSaveLoad tests saving and loading a validator set with changes. func TestOneValidatorChangesSaveLoad(t *testing.T) { - tearDown, stateDB, state := setupTestCase(t) - defer tearDown(t) + stateDB, state := setupTestCase(t) stateStore := sm.NewStore(stateDB) // Change vals at these heights. @@ -304,7 +295,6 @@ func TestOneValidatorChangesSaveLoad(t *testing.T) { } func TestProposerFrequency(t *testing.T) { - // some explicit test cases testCases := []struct { powers []int64 @@ -431,8 +421,7 @@ func testProposerFreq(t *testing.T, caseNum int, valSet *types.ValidatorSet) { // TestProposerPriorityDoesNotGetResetToZero assert that we preserve accum when calling updateState // see https://github.com/tendermint/tendermint/issues/2718 func TestProposerPriorityDoesNotGetResetToZero(t *testing.T) { - tearDown, _, state := setupTestCase(t) - defer tearDown(t) + _, state := setupTestCase(t) val1VotingPower := int64(10) val1PubKey := ed25519.GenPrivKey().PubKey() val1 := &types.Validator{Address: val1PubKey.Address(), PubKey: val1PubKey, VotingPower: val1VotingPower} @@ -545,8 +534,7 @@ func TestProposerPriorityProposerAlternates(t *testing.T) { // IncrementProposerPriority change. // Additionally, make sure that same power validators alternate if both // have the same voting power (and the 2nd was added later). - tearDown, _, state := setupTestCase(t) - defer tearDown(t) + _, state := setupTestCase(t) val1VotingPower := int64(10) val1PubKey := ed25519.GenPrivKey().PubKey() val1 := &types.Validator{Address: val1PubKey.Address(), PubKey: val1PubKey, VotingPower: val1VotingPower} @@ -716,8 +704,7 @@ func TestProposerPriorityProposerAlternates(t *testing.T) { } func TestLargeGenesisValidator(t *testing.T) { - tearDown, _, state := setupTestCase(t) - defer tearDown(t) + _, state := setupTestCase(t) genesisVotingPower := types.MaxTotalVotingPower / 1000 genesisPubKey := ed25519.GenPrivKey().PubKey() @@ -899,8 +886,7 @@ func TestLargeGenesisValidator(t *testing.T) { func TestStoreLoadValidatorsIncrementsProposerPriority(t *testing.T) { const valSetSize = 2 - tearDown, stateDB, state := setupTestCase(t) - t.Cleanup(func() { tearDown(t) }) + stateDB, state := setupTestCase(t) stateStore := sm.NewStore(stateDB) state.Validators = genValSet(valSetSize) state.NextValidators = state.Validators.CopyIncrementProposerPriority(1) @@ -924,8 +910,7 @@ func TestStoreLoadValidatorsIncrementsProposerPriority(t *testing.T) { // changes. func TestManyValidatorChangesSaveLoad(t *testing.T) { const valSetSize = 7 - tearDown, stateDB, state := setupTestCase(t) - defer tearDown(t) + stateDB, state := setupTestCase(t) stateStore := sm.NewStore(stateDB) require.Equal(t, int64(0), state.LastBlockHeight) state.Validators = genValSet(valSetSize) @@ -972,9 +957,7 @@ func TestManyValidatorChangesSaveLoad(t *testing.T) { } func TestStateMakeBlock(t *testing.T) { - tearDown, _, state := setupTestCase(t) - defer tearDown(t) - + _, state := setupTestCase(t) proposerAddress := state.Validators.GetProposer().Address stateVersion := state.Version.Consensus block := makeBlock(state, 2) @@ -987,11 +970,8 @@ func TestStateMakeBlock(t *testing.T) { // TestConsensusParamsChangesSaveLoad tests saving and loading consensus params // with changes. func TestConsensusParamsChangesSaveLoad(t *testing.T) { - tearDown, stateDB, state := setupTestCase(t) - defer tearDown(t) - + stateDB, state := setupTestCase(t) stateStore := sm.NewStore(stateDB) - // Change vals at these heights. changeHeights := []int64{1, 2, 4, 5, 10, 15, 16, 17, 20} N := len(changeHeights) @@ -1052,9 +1032,7 @@ func TestConsensusParamsChangesSaveLoad(t *testing.T) { } func TestStateProto(t *testing.T) { - tearDown, _, state := setupTestCase(t) - defer tearDown(t) - + _, state := setupTestCase(t) tc := []struct { testName string state *sm.State diff --git a/store/store_test.go b/store/store_test.go index ea07c73e6..029f71f41 100644 --- a/store/store_test.go +++ b/store/store_test.go @@ -3,7 +3,6 @@ package store import ( "bytes" "fmt" - "os" "runtime/debug" "strings" "testing" @@ -26,10 +25,6 @@ import ( "github.com/tendermint/tendermint/version" ) -// A cleanupFunc cleans up any config / test files created for a particular -// test. -type cleanupFunc func() - // make a Commit with a single vote containing just the height and a timestamp func makeTestCommit(height int64, timestamp time.Time) *types.Commit { commitSigs := []types.CommitSig{{ @@ -54,8 +49,8 @@ func makeBlock(height int64, state sm.State, lastCommit *types.Commit) *types.Bl return block } -func makeStateAndBlockStore(logger log.Logger) (sm.State, *BlockStore, cleanupFunc) { - config := cfg.ResetTestRoot("blockchain_reactor_test") +func makeStateAndBlockStore(t *testing.T) (sm.State, *BlockStore) { + config := cfg.SetupTestConfiguration(t) // blockDB := dbm.NewDebugDB("blockDB", dbm.NewMemDB()) // stateDB := dbm.NewDebugDB("stateDB", dbm.NewMemDB()) blockDB := dbm.NewMemDB() @@ -65,7 +60,22 @@ func makeStateAndBlockStore(logger log.Logger) (sm.State, *BlockStore, cleanupFu if err != nil { panic(fmt.Errorf("error constructing state from genesis file: %w", err)) } - return state, NewBlockStore(blockDB), func() { os.RemoveAll(config.RootDir) } + return state, NewBlockStore(blockDB) +} + +func makeTestStateAndBlockStore(t *testing.T, logger log.Logger) (sm.State, *BlockStore) { + config := cfg.SetupTestConfiguration(t) + // blockDB := dbm.NewDebugDB("blockDB", dbm.NewMemDB()) + // stateDB := dbm.NewDebugDB("stateDB", dbm.NewMemDB()) + blockDB := dbm.NewMemDB() + stateDB := dbm.NewMemDB() + stateStore := sm.NewStore(stateDB) + state, err := stateStore.LoadFromDBOrGenesisFile(config.GenesisFile()) + if err != nil { + panic(fmt.Errorf("error constructing state from genesis file: %w", err)) + } + + return state, NewBlockStore(blockDB) } func TestLoadBlockStoreState(t *testing.T) { @@ -133,33 +143,43 @@ func freshBlockStore() (*BlockStore, dbm.DB) { return NewBlockStore(db), db } -var ( +type testFixture struct { state sm.State block *types.Block partSet *types.PartSet part1 *types.Part part2 *types.Part seenCommit1 *types.Commit -) + store *BlockStore -func TestMain(m *testing.M) { - var cleanup cleanupFunc - state, _, cleanup = makeStateAndBlockStore(log.NewTMLogger(new(bytes.Buffer))) - block = makeBlock(1, state, new(types.Commit)) - partSet = block.MakePartSet(2) - part1 = partSet.GetPart(0) - part2 = partSet.GetPart(1) - seenCommit1 = makeTestCommit(10, tmtime.Now()) - code := m.Run() - cleanup() - os.Exit(code) + t *testing.T +} + +func newTestFixture(t *testing.T) *testFixture { + state, store := makeStateAndBlockStore(t) + block := makeBlock(1, state, new(types.Commit)) + partSet := block.MakePartSet(2) + part1 := partSet.GetPart(0) + part2 := partSet.GetPart(1) + seenCommit1 := makeTestCommit(10, tmtime.Now()) + + return &testFixture{ + state: state, + block: block, + partSet: partSet, + part1: part1, + part2: part2, + seenCommit1: seenCommit1, + store: store, + t: t, + } } // TODO: This test should be simplified ... func TestBlockStoreSaveLoadBlock(t *testing.T) { - state, bs, cleanup := makeStateAndBlockStore(log.NewTMLogger(new(bytes.Buffer))) - defer cleanup() + fixture := newTestFixture(t) + state, bs := makeTestStateAndBlockStore(t, log.NewTMLogger(new(bytes.Buffer))) require.Equal(t, bs.Base(), int64(0), "initially the base should be zero") require.Equal(t, bs.Height(), int64(0), "initially the height should be zero") @@ -175,13 +195,13 @@ func TestBlockStoreSaveLoadBlock(t *testing.T) { block := makeBlock(bs.Height()+1, state, new(types.Commit)) validPartSet := block.MakePartSet(2) seenCommit := makeTestCommit(10, tmtime.Now()) - bs.SaveBlock(block, partSet, seenCommit) + bs.SaveBlock(block, fixture.partSet, seenCommit) require.EqualValues(t, 1, bs.Base(), "expecting the new height to be changed") require.EqualValues(t, block.Header.Height, bs.Height(), "expecting the new height to be changed") incompletePartSet := types.NewPartSetFromHeader(types.PartSetHeader{Total: 2}) uncontiguousPartSet := types.NewPartSetFromHeader(types.PartSetHeader{Total: 0}) - _, err := uncontiguousPartSet.AddPart(part2) + _, err := uncontiguousPartSet.AddPart(fixture.part2) require.Error(t, err) header1 := types.Header{ @@ -211,7 +231,7 @@ func TestBlockStoreSaveLoadBlock(t *testing.T) { { block: newBlock(header1, commitAtH10), parts: validPartSet, - seenCommit: seenCommit1, + seenCommit: fixture.seenCommit1, }, { @@ -242,7 +262,7 @@ func TestBlockStoreSaveLoadBlock(t *testing.T) { { block: newBlock(header1, commitAtH10), parts: validPartSet, - seenCommit: seenCommit1, + seenCommit: fixture.seenCommit1, corruptCommitInDB: true, // Corrupt the DB's commit entry wantPanic: "error reading block commit", }, @@ -250,7 +270,7 @@ func TestBlockStoreSaveLoadBlock(t *testing.T) { { block: newBlock(header1, commitAtH10), parts: validPartSet, - seenCommit: seenCommit1, + seenCommit: fixture.seenCommit1, wantPanic: "unmarshal to tmproto.BlockMeta", corruptBlockInDB: true, // Corrupt the DB's block entry }, @@ -258,7 +278,7 @@ func TestBlockStoreSaveLoadBlock(t *testing.T) { { block: newBlock(header1, commitAtH10), parts: validPartSet, - seenCommit: seenCommit1, + seenCommit: fixture.seenCommit1, // Expecting no error and we want a nil back eraseSeenCommitInDB: true, @@ -267,7 +287,7 @@ func TestBlockStoreSaveLoadBlock(t *testing.T) { { block: newBlock(header1, commitAtH10), parts: validPartSet, - seenCommit: seenCommit1, + seenCommit: fixture.seenCommit1, corruptSeenCommitInDB: true, wantPanic: "error reading block seen commit", @@ -276,7 +296,7 @@ func TestBlockStoreSaveLoadBlock(t *testing.T) { { block: newBlock(header1, commitAtH10), parts: validPartSet, - seenCommit: seenCommit1, + seenCommit: fixture.seenCommit1, // Expecting no error and we want a nil back eraseCommitInDB: true, @@ -367,8 +387,7 @@ func TestBlockStoreSaveLoadBlock(t *testing.T) { } func TestLoadBaseMeta(t *testing.T) { - config := cfg.ResetTestRoot("blockchain_reactor_test") - defer os.RemoveAll(config.RootDir) + config := cfg.SetupTestConfiguration(t) stateStore := sm.NewStore(dbm.NewMemDB()) state, err := stateStore.LoadFromDBOrGenesisFile(config.GenesisFile()) require.NoError(t, err) @@ -390,6 +409,7 @@ func TestLoadBaseMeta(t *testing.T) { } func TestLoadBlockPart(t *testing.T) { + fixture := newTestFixture(t) bs, db := freshBlockStore() height, index := int64(10), 1 loadPart := func() (interface{}, error) { @@ -411,20 +431,19 @@ func TestLoadBlockPart(t *testing.T) { require.Contains(t, panicErr.Error(), "unmarshal to tmproto.Part failed") // 3. A good block serialized and saved to the DB should be retrievable - pb1, err := part1.ToProto() + pb1, err := fixture.part1.ToProto() require.NoError(t, err) err = db.Set(calcBlockPartKey(height, index), mustEncode(pb1)) require.NoError(t, err) gotPart, _, panicErr := doFn(loadPart) require.Nil(t, panicErr, "an existent and proper block should not panic") require.Nil(t, res, "a properly saved block should return a proper block") - require.Equal(t, gotPart.(*types.Part), part1, + require.Equal(t, gotPart.(*types.Part), fixture.part1, "expecting successful retrieval of previously saved block") } func TestPruneBlocks(t *testing.T) { - config := cfg.ResetTestRoot("blockchain_reactor_test") - defer os.RemoveAll(config.RootDir) + config := cfg.SetupTestConfiguration(t) stateStore := sm.NewStore(dbm.NewMemDB()) state, err := stateStore.LoadFromDBOrGenesisFile(config.GenesisFile()) require.NoError(t, err) @@ -549,8 +568,7 @@ func TestLoadBlockMeta(t *testing.T) { } func TestBlockFetchAtHeight(t *testing.T) { - state, bs, cleanup := makeStateAndBlockStore(log.NewTMLogger(new(bytes.Buffer))) - defer cleanup() + state, bs := makeTestStateAndBlockStore(t, log.NewTMLogger(new(bytes.Buffer))) require.Equal(t, bs.Height(), int64(0), "initially the height should be zero") block := makeBlock(bs.Height()+1, state, new(types.Commit)) diff --git a/test/maverick/consensus/wal_generator.go b/test/maverick/consensus/wal_generator.go index c691f371d..2b72fdd8a 100644 --- a/test/maverick/consensus/wal_generator.go +++ b/test/maverick/consensus/wal_generator.go @@ -153,7 +153,7 @@ func makeAddrs() (string, string, string) { // getConfig returns a config for test cases func getConfig(t *testing.T) *cfg.Config { - c := cfg.ResetTestRoot(t.Name()) + c := cfg.SetupTestConfiguration(t) // and we use random ports to run in parallel tm, rpc, grpc := makeAddrs()