github.com/true-sqn/fabric@v2.1.1+incompatible/core/ledger/kvledger/history/pkg_test.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package history
     8  
     9  import (
    10  	"io/ioutil"
    11  	"os"
    12  	"testing"
    13  
    14  	"github.com/hyperledger/fabric/bccsp/sw"
    15  	"github.com/hyperledger/fabric/common/ledger/blkstorage"
    16  	"github.com/hyperledger/fabric/common/ledger/blkstorage/fsblkstorage"
    17  	"github.com/hyperledger/fabric/common/metrics/disabled"
    18  	"github.com/hyperledger/fabric/core/ledger/kvledger/bookkeeping"
    19  	"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/privacyenabledstate"
    20  	"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/txmgr"
    21  	"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/txmgr/lockbasedtxmgr"
    22  	"github.com/hyperledger/fabric/core/ledger/mock"
    23  	"github.com/stretchr/testify/assert"
    24  )
    25  
    26  /////// levelDBLockBasedHistoryEnv //////
    27  
    28  type levelDBLockBasedHistoryEnv struct {
    29  	t                     testing.TB
    30  	testBlockStorageEnv   *testBlockStoreEnv
    31  	testDBEnv             privacyenabledstate.TestEnv
    32  	testBookkeepingEnv    *bookkeeping.TestEnv
    33  	txmgr                 txmgr.TxMgr
    34  	testHistoryDBProvider *DBProvider
    35  	testHistoryDB         *DB
    36  	testHistoryDBPath     string
    37  }
    38  
    39  func newTestHistoryEnv(t *testing.T) *levelDBLockBasedHistoryEnv {
    40  	testLedgerID := "TestLedger"
    41  
    42  	blockStorageTestEnv := newBlockStorageTestEnv(t)
    43  
    44  	testDBEnv := &privacyenabledstate.LevelDBCommonStorageTestEnv{}
    45  	testDBEnv.Init(t)
    46  	testDB := testDBEnv.GetDBHandle(testLedgerID)
    47  	testBookkeepingEnv := bookkeeping.NewTestEnv(t)
    48  
    49  	testHistoryDBPath, err := ioutil.TempDir("", "historyldb")
    50  	if err != nil {
    51  		t.Fatalf("Failed to create history database directory: %s", err)
    52  	}
    53  
    54  	cryptoProvider, err := sw.NewDefaultSecurityLevelWithKeystore(sw.NewDummyKeyStore())
    55  	assert.NoError(t, err)
    56  	txMgr, err := lockbasedtxmgr.NewLockBasedTxMgr(
    57  		testLedgerID,
    58  		testDB,
    59  		nil,
    60  		nil,
    61  		testBookkeepingEnv.TestProvider,
    62  		&mock.DeployedChaincodeInfoProvider{},
    63  		nil,
    64  		cryptoProvider,
    65  	)
    66  
    67  	assert.NoError(t, err)
    68  	testHistoryDBProvider, err := NewDBProvider(testHistoryDBPath)
    69  	assert.NoError(t, err)
    70  	testHistoryDB, err := testHistoryDBProvider.GetDBHandle("TestHistoryDB")
    71  	assert.NoError(t, err)
    72  
    73  	return &levelDBLockBasedHistoryEnv{
    74  		t,
    75  		blockStorageTestEnv,
    76  		testDBEnv,
    77  		testBookkeepingEnv,
    78  		txMgr,
    79  		testHistoryDBProvider,
    80  		testHistoryDB,
    81  		testHistoryDBPath,
    82  	}
    83  }
    84  
    85  func (env *levelDBLockBasedHistoryEnv) cleanup() {
    86  	env.txmgr.Shutdown()
    87  	env.testDBEnv.Cleanup()
    88  	env.testBlockStorageEnv.cleanup()
    89  	env.testBookkeepingEnv.Cleanup()
    90  	// clean up history
    91  	env.testHistoryDBProvider.Close()
    92  	os.RemoveAll(env.testHistoryDBPath)
    93  }
    94  
    95  /////// testBlockStoreEnv//////
    96  
    97  type testBlockStoreEnv struct {
    98  	t               testing.TB
    99  	provider        *fsblkstorage.FsBlockstoreProvider
   100  	blockStorageDir string
   101  }
   102  
   103  func newBlockStorageTestEnv(t testing.TB) *testBlockStoreEnv {
   104  
   105  	testPath, err := ioutil.TempDir("", "historyleveldb-")
   106  	if err != nil {
   107  		panic(err)
   108  	}
   109  	conf := fsblkstorage.NewConf(testPath, 0)
   110  
   111  	attrsToIndex := []blkstorage.IndexableAttr{
   112  		blkstorage.IndexableAttrBlockHash,
   113  		blkstorage.IndexableAttrBlockNum,
   114  		blkstorage.IndexableAttrTxID,
   115  		blkstorage.IndexableAttrBlockNumTranNum,
   116  	}
   117  	indexConfig := &blkstorage.IndexConfig{AttrsToIndex: attrsToIndex}
   118  
   119  	p, err := fsblkstorage.NewProvider(conf, indexConfig, &disabled.Provider{})
   120  	assert.NoError(t, err)
   121  	return &testBlockStoreEnv{t, p.(*fsblkstorage.FsBlockstoreProvider), testPath}
   122  }
   123  
   124  func (env *testBlockStoreEnv) cleanup() {
   125  	env.provider.Close()
   126  	env.removeFSPath()
   127  }
   128  
   129  func (env *testBlockStoreEnv) removeFSPath() {
   130  	fsPath := env.blockStorageDir
   131  	os.RemoveAll(fsPath)
   132  }