github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/core/ledger/kvledger/rebuild_dbs_test.go (about) 1 /* 2 Copyright hechain. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package kvledger 8 9 import ( 10 "path/filepath" 11 "testing" 12 13 configtxtest "github.com/hechain20/hechain/common/configtx/test" 14 "github.com/hechain20/hechain/core/ledger/mock" 15 "github.com/hechain20/hechain/internal/fileutil" 16 "github.com/stretchr/testify/require" 17 ) 18 19 func TestRebuildDBs(t *testing.T) { 20 conf, cleanup := testConfig(t) 21 defer cleanup() 22 provider := testutilNewProvider(conf, t, &mock.DeployedChaincodeInfoProvider{}) 23 24 numLedgers := 3 25 for i := 0; i < numLedgers; i++ { 26 genesisBlock, _ := configtxtest.MakeGenesisBlock(constructTestLedgerID(i)) 27 _, err := provider.CreateFromGenesisBlock(genesisBlock) 28 require.NoError(t, err) 29 } 30 31 // rebuild should fail when provider is still open 32 err := RebuildDBs(conf) 33 require.Error(t, err, "as another peer node command is executing, wait for that command to complete its execution or terminate it before retrying") 34 provider.Close() 35 36 err = RebuildDBs(conf) 37 require.NoError(t, err) 38 39 // verify blockstoreIndex, configHistory, history, state, bookkeeper dbs are deleted 40 rootFSPath := conf.RootFSPath 41 empty, err := fileutil.DirEmpty(filepath.Join(BlockStorePath(rootFSPath), "index")) 42 require.NoError(t, err) 43 require.True(t, empty) 44 empty, err = fileutil.DirEmpty(ConfigHistoryDBPath(rootFSPath)) 45 require.NoError(t, err) 46 require.True(t, empty) 47 empty, err = fileutil.DirEmpty(HistoryDBPath(rootFSPath)) 48 require.NoError(t, err) 49 require.True(t, empty) 50 empty, err = fileutil.DirEmpty(StateDBPath(rootFSPath)) 51 require.NoError(t, err) 52 require.True(t, empty) 53 empty, err = fileutil.DirEmpty(BookkeeperDBPath(rootFSPath)) 54 require.NoError(t, err) 55 require.True(t, empty) 56 57 // rebuild again should be successful 58 err = RebuildDBs(conf) 59 require.NoError(t, err) 60 }