github.com/renegr87/renegr87@v2.1.1+incompatible/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  	"os"
    11  	"path/filepath"
    12  	"testing"
    13  
    14  	configtxtest "github.com/hyperledger/fabric/common/configtx/test"
    15  	"github.com/stretchr/testify/require"
    16  )
    17  
    18  func TestRebuildDBs(t *testing.T) {
    19  	conf, cleanup := testConfig(t)
    20  	defer cleanup()
    21  	provider := testutilNewProvider(conf, t)
    22  
    23  	numLedgers := 3
    24  	for i := 0; i < numLedgers; i++ {
    25  		genesisBlock, _ := configtxtest.MakeGenesisBlock(constructTestLedgerID(i))
    26  		provider.Create(genesisBlock)
    27  	}
    28  
    29  	// rebuild should fail when provider is still open
    30  	err := RebuildDBs(conf.RootFSPath)
    31  	require.Error(t, err, "as another peer node command is executing, wait for that command to complete its execution or terminate it before retrying")
    32  	provider.Close()
    33  
    34  	rootFSPath := conf.RootFSPath
    35  	err = RebuildDBs(rootFSPath)
    36  	require.NoError(t, err)
    37  
    38  	// verify blockstoreIndex, configHistory, history, state, bookkeeper dbs are deleted
    39  	_, err = os.Stat(filepath.Join(BlockStorePath(rootFSPath), "index"))
    40  	require.Equal(t, os.IsNotExist(err), true)
    41  	_, err = os.Stat(ConfigHistoryDBPath(rootFSPath))
    42  	require.Equal(t, os.IsNotExist(err), true)
    43  	_, err = os.Stat(HistoryDBPath(rootFSPath))
    44  	require.Equal(t, os.IsNotExist(err), true)
    45  	_, err = os.Stat(StateDBPath(rootFSPath))
    46  	require.Equal(t, os.IsNotExist(err), true)
    47  	_, err = os.Stat(BookkeeperDBPath(rootFSPath))
    48  	require.Equal(t, os.IsNotExist(err), true)
    49  
    50  	// rebuild again should be successful
    51  	err = RebuildDBs(rootFSPath)
    52  	require.NoError(t, err)
    53  }