github.com/osdi23p228/fabric@v0.0.0-20221218062954-77808885f5db/internal/peer/node/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 node 8 9 import ( 10 "io/ioutil" 11 "os" 12 "path" 13 "path/filepath" 14 "testing" 15 16 "github.com/osdi23p228/fabric/common/ledger/util" 17 "github.com/osdi23p228/fabric/core/config" 18 "github.com/osdi23p228/fabric/core/ledger/kvledger" 19 "github.com/spf13/viper" 20 "github.com/stretchr/testify/assert" 21 "github.com/stretchr/testify/require" 22 ) 23 24 func TestRebuildDBsCmd(t *testing.T) { 25 testPath := "/tmp/hyperledger/test" 26 os.RemoveAll(testPath) 27 viper.Set("peer.fileSystemPath", testPath) 28 defer os.RemoveAll(testPath) 29 30 viper.Set("logging.ledger", "INFO") 31 rootFSPath := filepath.Join(config.GetPath("peer.fileSystemPath"), "ledgersData") 32 bookkeeperDBPath := kvledger.BookkeeperDBPath(rootFSPath) 33 configHistoryDBPath := kvledger.ConfigHistoryDBPath(rootFSPath) 34 historyDBPath := kvledger.HistoryDBPath(rootFSPath) 35 stateDBPath := kvledger.StateDBPath(rootFSPath) 36 blockstoreIndexDBPath := filepath.Join(kvledger.BlockStorePath(rootFSPath), "index") 37 38 dbPaths := []string{bookkeeperDBPath, configHistoryDBPath, historyDBPath, stateDBPath, blockstoreIndexDBPath} 39 for _, dbPath := range dbPaths { 40 assert.NoError(t, os.MkdirAll(dbPath, 0755)) 41 assert.NoError(t, ioutil.WriteFile(path.Join(dbPath, "dummyfile.txt"), []byte("this is a dummy file for test"), 0644)) 42 } 43 44 // check dbs exist before upgrade 45 for _, dbPath := range dbPaths { 46 _, err := os.Stat(dbPath) 47 assert.False(t, os.IsNotExist(err)) 48 } 49 50 cmd := rebuildDBsCmd() 51 assert.NoError(t, cmd.Execute()) 52 53 // check dbs do not exist after upgrade 54 for _, dbPath := range dbPaths { 55 empty, err := util.DirEmpty(dbPath) 56 require.NoError(t, err) 57 require.True(t, empty) 58 } 59 }