github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/core/ledger/kvledger/txmgmt/txmgr/commontests/pkg_test.go (about)

     1  /*
     2  Copyright IBM Corp. 2016 All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  		 http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package commontests
    18  
    19  import (
    20  	"testing"
    21  	"time"
    22  
    23  	"github.com/hyperledger/fabric/common/ledger/testutil"
    24  	"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/statedb"
    25  	"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/statedb/statecouchdb"
    26  	"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/statedb/stateleveldb"
    27  	"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/txmgr"
    28  	"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/txmgr/lockbasedtxmgr"
    29  	"github.com/hyperledger/fabric/core/ledger/util"
    30  	"github.com/hyperledger/fabric/protos/common"
    31  	"github.com/spf13/viper"
    32  )
    33  
    34  const (
    35  	testFilesystemPath = "/tmp/fabric/ledgertests/kvledger/txmgmt/txmgr/commontests"
    36  )
    37  
    38  type testEnv interface {
    39  	init(t *testing.T, testLedgerID string)
    40  	getName() string
    41  	getTxMgr() txmgr.TxMgr
    42  	getVDB() statedb.VersionedDB
    43  	cleanup()
    44  }
    45  
    46  // Tests will be run against each environment in this array
    47  // For example, to skip CouchDB tests, remove &couchDBLockBasedEnv{}
    48  var testEnvs = []testEnv{&levelDBLockBasedEnv{}, &couchDBLockBasedEnv{}}
    49  
    50  ///////////// LevelDB Environment //////////////
    51  
    52  const levelDBtestEnvName = "levelDB_LockBasedTxMgr"
    53  
    54  type levelDBLockBasedEnv struct {
    55  	testLedgerID string
    56  	testDBEnv    *stateleveldb.TestVDBEnv
    57  	testDB       statedb.VersionedDB
    58  	txmgr        txmgr.TxMgr
    59  }
    60  
    61  func (env *levelDBLockBasedEnv) getName() string {
    62  	return levelDBtestEnvName
    63  }
    64  
    65  func (env *levelDBLockBasedEnv) init(t *testing.T, testLedgerID string) {
    66  	viper.Set("peer.fileSystemPath", testFilesystemPath)
    67  	testDBEnv := stateleveldb.NewTestVDBEnv(t)
    68  	testDB, err := testDBEnv.DBProvider.GetDBHandle(testLedgerID)
    69  	testutil.AssertNoError(t, err, "")
    70  
    71  	txMgr := lockbasedtxmgr.NewLockBasedTxMgr(testDB)
    72  	env.testLedgerID = testLedgerID
    73  	env.testDBEnv = testDBEnv
    74  	env.testDB = testDB
    75  	env.txmgr = txMgr
    76  }
    77  
    78  func (env *levelDBLockBasedEnv) getTxMgr() txmgr.TxMgr {
    79  	return env.txmgr
    80  }
    81  
    82  func (env *levelDBLockBasedEnv) getVDB() statedb.VersionedDB {
    83  	return env.testDB
    84  }
    85  
    86  func (env *levelDBLockBasedEnv) cleanup() {
    87  	defer env.txmgr.Shutdown()
    88  	defer env.testDBEnv.Cleanup()
    89  }
    90  
    91  ///////////// CouchDB Environment //////////////
    92  
    93  const couchDBtestEnvName = "couchDB_LockBasedTxMgr"
    94  
    95  type couchDBLockBasedEnv struct {
    96  	testLedgerID string
    97  	testDBEnv    *statecouchdb.TestVDBEnv
    98  	testDB       statedb.VersionedDB
    99  	txmgr        txmgr.TxMgr
   100  }
   101  
   102  func (env *couchDBLockBasedEnv) getName() string {
   103  	return couchDBtestEnvName
   104  }
   105  
   106  func (env *couchDBLockBasedEnv) init(t *testing.T, testLedgerID string) {
   107  	viper.Set("peer.fileSystemPath", testFilesystemPath)
   108  	// both vagrant and CI have couchdb configured at host "couchdb"
   109  	viper.Set("ledger.state.couchDBConfig.couchDBAddress", "couchdb:5984")
   110  	// Replace with correct username/password such as
   111  	// admin/admin if user security is enabled on couchdb.
   112  	viper.Set("ledger.state.couchDBConfig.username", "")
   113  	viper.Set("ledger.state.couchDBConfig.password", "")
   114  	viper.Set("ledger.state.couchDBConfig.maxRetries", 3)
   115  	viper.Set("ledger.state.couchDBConfig.maxRetriesOnStartup", 10)
   116  	viper.Set("ledger.state.couchDBConfig.requestTimeout", time.Second*35)
   117  	testDBEnv := statecouchdb.NewTestVDBEnv(t)
   118  	testDB, err := testDBEnv.DBProvider.GetDBHandle(testLedgerID)
   119  	testutil.AssertNoError(t, err, "")
   120  
   121  	txMgr := lockbasedtxmgr.NewLockBasedTxMgr(testDB)
   122  	env.testLedgerID = testLedgerID
   123  	env.testDBEnv = testDBEnv
   124  	env.testDB = testDB
   125  	env.txmgr = txMgr
   126  }
   127  
   128  func (env *couchDBLockBasedEnv) getTxMgr() txmgr.TxMgr {
   129  	return env.txmgr
   130  }
   131  
   132  func (env *couchDBLockBasedEnv) getVDB() statedb.VersionedDB {
   133  	return env.testDB
   134  }
   135  
   136  func (env *couchDBLockBasedEnv) cleanup() {
   137  	defer env.txmgr.Shutdown()
   138  	defer env.testDBEnv.Cleanup(env.testLedgerID)
   139  }
   140  
   141  //////////// txMgrTestHelper /////////////
   142  
   143  type txMgrTestHelper struct {
   144  	t     *testing.T
   145  	txMgr txmgr.TxMgr
   146  	bg    *testutil.BlockGenerator
   147  }
   148  
   149  func newTxMgrTestHelper(t *testing.T, txMgr txmgr.TxMgr) *txMgrTestHelper {
   150  	bg, _ := testutil.NewBlockGenerator(t, "testLedger", false)
   151  	return &txMgrTestHelper{t, txMgr, bg}
   152  }
   153  
   154  func (h *txMgrTestHelper) validateAndCommitRWSet(txRWSet []byte) {
   155  	block := h.bg.NextBlock([][]byte{txRWSet})
   156  	err := h.txMgr.ValidateAndPrepare(block, true)
   157  	testutil.AssertNoError(h.t, err, "")
   158  	txsFltr := util.TxValidationFlags(block.Metadata.Metadata[common.BlockMetadataIndex_TRANSACTIONS_FILTER])
   159  	invalidTxNum := 0
   160  	for i := 0; i < len(block.Data.Data); i++ {
   161  		if txsFltr.IsInvalid(i) {
   162  			invalidTxNum++
   163  		}
   164  	}
   165  	testutil.AssertEquals(h.t, invalidTxNum, 0)
   166  	err = h.txMgr.Commit()
   167  	testutil.AssertNoError(h.t, err, "")
   168  }
   169  
   170  func (h *txMgrTestHelper) checkRWsetInvalid(txRWSet []byte) {
   171  	block := h.bg.NextBlock([][]byte{txRWSet})
   172  	err := h.txMgr.ValidateAndPrepare(block, true)
   173  	testutil.AssertNoError(h.t, err, "")
   174  	txsFltr := util.TxValidationFlags(block.Metadata.Metadata[common.BlockMetadataIndex_TRANSACTIONS_FILTER])
   175  	invalidTxNum := 0
   176  	for i := 0; i < len(block.Data.Data); i++ {
   177  		if txsFltr.IsInvalid(i) {
   178  			invalidTxNum++
   179  		}
   180  	}
   181  	testutil.AssertEquals(h.t, invalidTxNum, 1)
   182  }