github.com/osdi23p228/fabric@v0.0.0-20221218062954-77808885f5db/core/ledger/kvledger/rebuild_dbs_test.go (about)

     1  /*
     2  Copyright IBM Corp. 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/osdi23p228/fabric/common/configtx/test"
    14  	"github.com/osdi23p228/fabric/common/ledger/util"
    15  	"github.com/osdi23p228/fabric/core/ledger/mock"
    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  		provider.Create(genesisBlock)
    28  	}
    29  
    30  	// rebuild should fail when provider is still open
    31  	err := RebuildDBs(conf)
    32  	require.Error(t, err, "as another peer node command is executing, wait for that command to complete its execution or terminate it before retrying")
    33  	provider.Close()
    34  
    35  	err = RebuildDBs(conf)
    36  	require.NoError(t, err)
    37  
    38  	// verify blockstoreIndex, configHistory, history, state, bookkeeper dbs are deleted
    39  	rootFSPath := conf.RootFSPath
    40  	empty, err := util.DirEmpty(filepath.Join(BlockStorePath(rootFSPath), "index"))
    41  	require.NoError(t, err)
    42  	require.True(t, empty)
    43  	empty, err = util.DirEmpty(ConfigHistoryDBPath(rootFSPath))
    44  	require.NoError(t, err)
    45  	require.True(t, empty)
    46  	empty, err = util.DirEmpty(HistoryDBPath(rootFSPath))
    47  	require.NoError(t, err)
    48  	require.True(t, empty)
    49  	empty, err = util.DirEmpty(StateDBPath(rootFSPath))
    50  	require.NoError(t, err)
    51  	require.True(t, empty)
    52  	empty, err = util.DirEmpty(BookkeeperDBPath(rootFSPath))
    53  	require.NoError(t, err)
    54  	require.True(t, empty)
    55  
    56  	// rebuild again should be successful
    57  	err = RebuildDBs(conf)
    58  	require.NoError(t, err)
    59  }