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