github.com/true-sqn/fabric@v2.1.1+incompatible/internal/peer/node/upgrade_dbs_test.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package node
     8  
     9  import (
    10  	"io/ioutil"
    11  	"os"
    12  	"path"
    13  	"path/filepath"
    14  	"testing"
    15  
    16  	"github.com/hyperledger/fabric/core/config"
    17  	"github.com/hyperledger/fabric/core/ledger/kvledger"
    18  	"github.com/spf13/viper"
    19  	"github.com/stretchr/testify/assert"
    20  )
    21  
    22  func TestUpgradeDBsCmd(t *testing.T) {
    23  	testPath := "/tmp/hyperledger/test"
    24  	os.RemoveAll(testPath)
    25  	viper.Set("peer.fileSystemPath", testPath)
    26  	defer os.RemoveAll(testPath)
    27  
    28  	viper.Set("logging.ledger", "INFO")
    29  	rootFSPath := filepath.Join(config.GetPath("peer.fileSystemPath"), "ledgersData")
    30  	bookkeeperDBPath := kvledger.BookkeeperDBPath(rootFSPath)
    31  	configHistoryDBPath := kvledger.ConfigHistoryDBPath(rootFSPath)
    32  	historyDBPath := kvledger.HistoryDBPath(rootFSPath)
    33  	stateDBPath := kvledger.StateDBPath(rootFSPath)
    34  	blockstoreIndexDBPath := filepath.Join(kvledger.BlockStorePath(rootFSPath), "index")
    35  
    36  	dbPaths := []string{bookkeeperDBPath, configHistoryDBPath, historyDBPath, stateDBPath, blockstoreIndexDBPath}
    37  	for _, dbPath := range dbPaths {
    38  		assert.NoError(t, os.MkdirAll(dbPath, 0755))
    39  		assert.NoError(t, ioutil.WriteFile(path.Join(dbPath, "dummyfile.txt"), []byte("this is a dummy file for test"), 0644))
    40  	}
    41  
    42  	// check dbs exist before upgrade
    43  	for _, dbPath := range dbPaths {
    44  		_, err := os.Stat(dbPath)
    45  		assert.False(t, os.IsNotExist(err))
    46  	}
    47  
    48  	cmd := upgradeDBsCmd()
    49  	assert.NoError(t, cmd.Execute())
    50  
    51  	// check dbs do not exist after upgrade
    52  	for _, dbPath := range dbPaths {
    53  		_, err := os.Stat(dbPath)
    54  		assert.True(t, os.IsNotExist(err))
    55  	}
    56  }