github.com/tenywen/fabric@v1.0.0-beta.0.20170620030522-a5b1ed380643/core/ledger/kvledger/txmgmt/txmgr/lockbasedtxmgr/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 lockbasedtxmgr 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/util" 29 "github.com/hyperledger/fabric/protos/common" 30 "github.com/spf13/viper" 31 ) 32 33 const ( 34 testFilesystemPath = "/tmp/fabric/ledgertests/kvledger/txmgmt/txmgr/lockbasedtxmgr" 35 ) 36 37 type testEnv interface { 38 init(t *testing.T, testLedgerID string) 39 getName() string 40 getTxMgr() txmgr.TxMgr 41 getVDB() statedb.VersionedDB 42 cleanup() 43 } 44 45 // Tests will be run against each environment in this array 46 // For example, to skip CouchDB tests, remove &couchDBLockBasedEnv{} 47 var testEnvs = []testEnv{&levelDBLockBasedEnv{}, &couchDBLockBasedEnv{}} 48 49 ///////////// LevelDB Environment ////////////// 50 51 const levelDBtestEnvName = "levelDB_LockBasedTxMgr" 52 53 type levelDBLockBasedEnv struct { 54 testLedgerID string 55 testDBEnv *stateleveldb.TestVDBEnv 56 testDB statedb.VersionedDB 57 txmgr txmgr.TxMgr 58 } 59 60 func (env *levelDBLockBasedEnv) getName() string { 61 return levelDBtestEnvName 62 } 63 64 func (env *levelDBLockBasedEnv) init(t *testing.T, testLedgerID string) { 65 viper.Set("peer.fileSystemPath", testFilesystemPath) 66 testDBEnv := stateleveldb.NewTestVDBEnv(t) 67 testDB, err := testDBEnv.DBProvider.GetDBHandle(testLedgerID) 68 testutil.AssertNoError(t, err, "") 69 70 txMgr := NewLockBasedTxMgr(testDB) 71 env.testLedgerID = testLedgerID 72 env.testDBEnv = testDBEnv 73 env.testDB = testDB 74 env.txmgr = txMgr 75 } 76 77 func (env *levelDBLockBasedEnv) getTxMgr() txmgr.TxMgr { 78 return env.txmgr 79 } 80 81 func (env *levelDBLockBasedEnv) getVDB() statedb.VersionedDB { 82 return env.testDB 83 } 84 85 func (env *levelDBLockBasedEnv) cleanup() { 86 defer env.txmgr.Shutdown() 87 defer env.testDBEnv.Cleanup() 88 } 89 90 ///////////// CouchDB Environment ////////////// 91 92 const couchDBtestEnvName = "couchDB_LockBasedTxMgr" 93 94 type couchDBLockBasedEnv struct { 95 testLedgerID string 96 testDBEnv *statecouchdb.TestVDBEnv 97 testDB statedb.VersionedDB 98 txmgr txmgr.TxMgr 99 } 100 101 func (env *couchDBLockBasedEnv) getName() string { 102 return couchDBtestEnvName 103 } 104 105 func (env *couchDBLockBasedEnv) init(t *testing.T, testLedgerID string) { 106 viper.Set("peer.fileSystemPath", testFilesystemPath) 107 // both vagrant and CI have couchdb configured at host "couchdb" 108 viper.Set("ledger.state.couchDBConfig.couchDBAddress", "couchdb:5984") 109 // Replace with correct username/password such as 110 // admin/admin if user security is enabled on couchdb. 111 viper.Set("ledger.state.couchDBConfig.username", "") 112 viper.Set("ledger.state.couchDBConfig.password", "") 113 viper.Set("ledger.state.couchDBConfig.maxRetries", 3) 114 viper.Set("ledger.state.couchDBConfig.maxRetriesOnStartup", 10) 115 viper.Set("ledger.state.couchDBConfig.requestTimeout", time.Second*35) 116 testDBEnv := statecouchdb.NewTestVDBEnv(t) 117 testDB, err := testDBEnv.DBProvider.GetDBHandle(testLedgerID) 118 testutil.AssertNoError(t, err, "") 119 120 txMgr := NewLockBasedTxMgr(testDB) 121 env.testLedgerID = testLedgerID 122 env.testDBEnv = testDBEnv 123 env.testDB = testDB 124 env.txmgr = txMgr 125 } 126 127 func (env *couchDBLockBasedEnv) getTxMgr() txmgr.TxMgr { 128 return env.txmgr 129 } 130 131 func (env *couchDBLockBasedEnv) getVDB() statedb.VersionedDB { 132 return env.testDB 133 } 134 135 func (env *couchDBLockBasedEnv) cleanup() { 136 defer env.txmgr.Shutdown() 137 defer env.testDBEnv.Cleanup(env.testLedgerID) 138 } 139 140 //////////// txMgrTestHelper ///////////// 141 142 type txMgrTestHelper struct { 143 t *testing.T 144 txMgr txmgr.TxMgr 145 bg *testutil.BlockGenerator 146 } 147 148 func newTxMgrTestHelper(t *testing.T, txMgr txmgr.TxMgr) *txMgrTestHelper { 149 bg, _ := testutil.NewBlockGenerator(t, "testLedger", false) 150 return &txMgrTestHelper{t, txMgr, bg} 151 } 152 153 func (h *txMgrTestHelper) validateAndCommitRWSet(txRWSet []byte) { 154 block := h.bg.NextBlock([][]byte{txRWSet}) 155 err := h.txMgr.ValidateAndPrepare(block, true) 156 testutil.AssertNoError(h.t, err, "") 157 txsFltr := util.TxValidationFlags(block.Metadata.Metadata[common.BlockMetadataIndex_TRANSACTIONS_FILTER]) 158 invalidTxNum := 0 159 for i := 0; i < len(block.Data.Data); i++ { 160 if txsFltr.IsInvalid(i) { 161 invalidTxNum++ 162 } 163 } 164 testutil.AssertEquals(h.t, invalidTxNum, 0) 165 err = h.txMgr.Commit() 166 testutil.AssertNoError(h.t, err, "") 167 } 168 169 func (h *txMgrTestHelper) checkRWsetInvalid(txRWSet []byte) { 170 block := h.bg.NextBlock([][]byte{txRWSet}) 171 err := h.txMgr.ValidateAndPrepare(block, true) 172 testutil.AssertNoError(h.t, err, "") 173 txsFltr := util.TxValidationFlags(block.Metadata.Metadata[common.BlockMetadataIndex_TRANSACTIONS_FILTER]) 174 invalidTxNum := 0 175 for i := 0; i < len(block.Data.Data); i++ { 176 if txsFltr.IsInvalid(i) { 177 invalidTxNum++ 178 } 179 } 180 testutil.AssertEquals(h.t, invalidTxNum, 1) 181 }