github.com/osdi23p228/fabric@v0.0.0-20221218062954-77808885f5db/internal/peer/node/reset_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/spf13/viper"
    19  	"github.com/stretchr/testify/assert"
    20  	"github.com/stretchr/testify/require"
    21  )
    22  
    23  func TestResetCmd(t *testing.T) {
    24  	testPath := "/tmp/hyperledger/test"
    25  	os.RemoveAll(testPath)
    26  	viper.Set("peer.fileSystemPath", testPath)
    27  	defer os.RemoveAll(testPath)
    28  
    29  	viper.Set("logging.ledger", "INFO")
    30  	rootFSPath := filepath.Join(config.GetPath("peer.fileSystemPath"), "ledgersData")
    31  	historyDBPath := filepath.Join(rootFSPath, "historyLeveldb")
    32  	assert.NoError(t,
    33  		os.MkdirAll(historyDBPath, 0755),
    34  	)
    35  	assert.NoError(t,
    36  		ioutil.WriteFile(path.Join(historyDBPath, "dummyfile.txt"), []byte("this is a dummy file for test"), 0644),
    37  	)
    38  	cmd := resetCmd()
    39  
    40  	_, err := os.Stat(historyDBPath)
    41  	require.False(t, os.IsNotExist(err))
    42  	require.NoError(t, cmd.Execute())
    43  	empty, err := util.DirEmpty(historyDBPath)
    44  	require.NoError(t, err)
    45  	require.True(t, empty)
    46  }