pkg_test.go 5.19 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
Copyright IBM Corp. 2016 All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

		 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package commontests

import (
	"testing"

22
	"github.com/hyperledger/fabric/common/ledger/testutil"
23
	"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/statedb"
24
	"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/statedb/statecouchdb"
25
26
27
	"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/statedb/stateleveldb"
	"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/txmgr"
	"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/txmgr/lockbasedtxmgr"
28
	"github.com/hyperledger/fabric/core/ledger/ledgerconfig"
29
	ledgertestutil "github.com/hyperledger/fabric/core/ledger/testutil"
30
31
	"github.com/hyperledger/fabric/core/ledger/util"
	"github.com/hyperledger/fabric/protos/common"
32
	"github.com/spf13/viper"
33
34
)

35
36
37
38
const (
	testFilesystemPath = "/tmp/fabric/ledgertests/kvledger/txmgmt/txmgr/commontests"
)

39
40
41
42
43
44
45
46
type testEnv interface {
	init(t *testing.T)
	getName() string
	getTxMgr() txmgr.TxMgr
	getVDB() statedb.VersionedDB
	cleanup()
}

47
48
49
50
var testEnvs = []testEnv{}

func init() {
	//call a helper method to load the core.yaml so that we can detect whether couch is configured
51
	ledgertestutil.SetupCoreYAMLConfig("./../../../../../../peer")
52
53
54
55
56
57
58
59
60
61

	//Only run the tests if CouchDB is explitily enabled in the code,
	//otherwise CouchDB may not be installed and all the tests would fail
	if ledgerconfig.IsCouchDBEnabled() == true {
		testEnvs = []testEnv{&levelDBLockBasedEnv{}, &couchDBLockBasedEnv{}}
	} else {
		testEnvs = []testEnv{&levelDBLockBasedEnv{}}
	}

}
62

63
64
///////////// LevelDB Environment //////////////

65
66
type levelDBLockBasedEnv struct {
	testDBEnv *stateleveldb.TestVDBEnv
67
	testDB    statedb.VersionedDB
68
69
70
71
72
73
74
75
	txmgr     txmgr.TxMgr
}

func (env *levelDBLockBasedEnv) getName() string {
	return "levelDB_LockBasedTxMgr"
}

func (env *levelDBLockBasedEnv) init(t *testing.T) {
76
	viper.Set("peer.fileSystemPath", testFilesystemPath)
77
	testDBEnv := stateleveldb.NewTestVDBEnv(t)
78
79
80
	testDB, err := testDBEnv.DBProvider.GetDBHandle("TestDB")
	testutil.AssertNoError(t, err, "")

81
	txMgr := lockbasedtxmgr.NewLockBasedTxMgr(testDB)
82
	env.testDBEnv = testDBEnv
83
	env.testDB = testDB
84
85
86
87
88
89
90
91
	env.txmgr = txMgr
}

func (env *levelDBLockBasedEnv) getTxMgr() txmgr.TxMgr {
	return env.txmgr
}

func (env *levelDBLockBasedEnv) getVDB() statedb.VersionedDB {
92
	return env.testDB
93
94
95
96
97
98
99
}

func (env *levelDBLockBasedEnv) cleanup() {
	defer env.txmgr.Shutdown()
	defer env.testDBEnv.Cleanup()
}

100
101
102
103
///////////// CouchDB Environment //////////////

var couchTestChainID = "TxmgrTestDB"

104
105
106
107
108
109
110
111
112
113
114
type couchDBLockBasedEnv struct {
	testDBEnv *statecouchdb.TestVDBEnv
	testDB    statedb.VersionedDB
	txmgr     txmgr.TxMgr
}

func (env *couchDBLockBasedEnv) getName() string {
	return "couchDB_LockBasedTxMgr"
}

func (env *couchDBLockBasedEnv) init(t *testing.T) {
115
	viper.Set("peer.fileSystemPath", testFilesystemPath)
116
117
	viper.Set("ledger.state.couchDBConfig.couchDBAddress", "127.0.0.1:5984")
	testDBEnv := statecouchdb.NewTestVDBEnv(t)
118
	testDB, err := testDBEnv.DBProvider.GetDBHandle(couchTestChainID)
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
	testutil.AssertNoError(t, err, "")

	txMgr := lockbasedtxmgr.NewLockBasedTxMgr(testDB)
	env.testDBEnv = testDBEnv
	env.testDB = testDB
	env.txmgr = txMgr
}

func (env *couchDBLockBasedEnv) getTxMgr() txmgr.TxMgr {
	return env.txmgr
}

func (env *couchDBLockBasedEnv) getVDB() statedb.VersionedDB {
	return env.testDB
}

func (env *couchDBLockBasedEnv) cleanup() {
	defer env.txmgr.Shutdown()
137
	defer env.testDBEnv.Cleanup(couchTestChainID)
138
139
}

140
141
//////////// txMgrTestHelper /////////////

142
143
144
145
146
147
148
149
150
151
152
153
type txMgrTestHelper struct {
	t     *testing.T
	txMgr txmgr.TxMgr
	bg    *testutil.BlockGenerator
}

func newTxMgrTestHelper(t *testing.T, txMgr txmgr.TxMgr) *txMgrTestHelper {
	return &txMgrTestHelper{t, txMgr, testutil.NewBlockGenerator(t)}
}

func (h *txMgrTestHelper) validateAndCommitRWSet(txRWSet []byte) {
	block := h.bg.NextBlock([][]byte{txRWSet}, false)
154
	err := h.txMgr.ValidateAndPrepare(block, true)
155
	testutil.AssertNoError(h.t, err, "")
156
157
158
159
160
161
162
163
	txsFltr := util.NewFilterBitArrayFromBytes(block.Metadata.Metadata[common.BlockMetadataIndex_TRANSACTIONS_FILTER])
	invalidTxNum := 0
	for i := 0; i < len(block.Data.Data); i++ {
		if txsFltr.IsSet(uint(i)) {
			invalidTxNum++
		}
	}
	testutil.AssertEquals(h.t, invalidTxNum, 0)
164
165
166
167
168
169
	err = h.txMgr.Commit()
	testutil.AssertNoError(h.t, err, "")
}

func (h *txMgrTestHelper) checkRWsetInvalid(txRWSet []byte) {
	block := h.bg.NextBlock([][]byte{txRWSet}, false)
170
	err := h.txMgr.ValidateAndPrepare(block, true)
171
	testutil.AssertNoError(h.t, err, "")
172
173
174
175
176
177
178
179
	txsFltr := util.NewFilterBitArrayFromBytes(block.Metadata.Metadata[common.BlockMetadataIndex_TRANSACTIONS_FILTER])
	invalidTxNum := 0
	for i := 0; i < len(block.Data.Data); i++ {
		if txsFltr.IsSet(uint(i)) {
			invalidTxNum++
		}
	}
	testutil.AssertEquals(h.t, invalidTxNum, 1)
180
}