Skip to content

Commit ef0bf29

Browse files
committed
[FAB-10025] Simplify test env clean up for statecouchdb
Change-Id: Ica96783947f3fb6b7e695b818e78d63b021fb6b6 Signed-off-by: ping40 <norberthu30@gmail.com>
1 parent bbe6df6 commit ef0bf29

File tree

4 files changed

+51
-110
lines changed

4 files changed

+51
-110
lines changed

core/ledger/kvledger/txmgmt/privacyenabledstate/db_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,22 @@ import (
1313
"testing"
1414

1515
"github.com/golang/protobuf/proto"
16+
"github.com/spf13/viper"
17+
"github.com/stretchr/testify/assert"
18+
1619
"github.com/hyperledger/fabric/common/ledger/testutil"
1720
"github.com/hyperledger/fabric/core/common/ccprovider"
1821
"github.com/hyperledger/fabric/core/ledger/cceventmgmt"
1922
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/statedb"
2023
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/version"
2124
"github.com/hyperledger/fabric/core/ledger/util"
2225
"github.com/hyperledger/fabric/protos/common"
23-
"github.com/spf13/viper"
24-
"github.com/stretchr/testify/assert"
2526
)
2627

2728
func TestMain(m *testing.M) {
2829
viper.Set("peer.fileSystemPath", "/tmp/fabric/ledgertests/kvledger/txmgmt/privacyenabledstate")
30+
// Disable auto warm to avoid error logs when the couchdb database has been dropped
31+
viper.Set("ledger.state.couchDBConfig.autoWarmIndexes", false)
2932
os.Exit(m.Run())
3033
}
3134

@@ -179,7 +182,7 @@ func testGetStateMultipleKeys(t *testing.T, env TestEnv) {
179182
func TestGetStateRangeScanIterator(t *testing.T) {
180183
for _, env := range testEnvs {
181184
t.Run(env.GetName(), func(t *testing.T) {
182-
testGetStateMultipleKeys(t, env)
185+
testGetStateRangeScanIterator(t, env)
183186
})
184187
}
185188
}

core/ledger/kvledger/txmgmt/privacyenabledstate/test_exports.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ import (
1212
"testing"
1313
"time"
1414

15-
"github.com/hyperledger/fabric/core/ledger/kvledger/bookkeeping"
15+
"github.com/spf13/viper"
16+
"github.com/stretchr/testify/assert"
1617

18+
"github.com/hyperledger/fabric/core/ledger/kvledger/bookkeeping"
1719
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/statedb/statecouchdb"
1820
"github.com/hyperledger/fabric/core/ledger/ledgerconfig"
1921
"github.com/hyperledger/fabric/integration/runner"
20-
"github.com/spf13/viper"
21-
"github.com/stretchr/testify/assert"
2222
)
2323

2424
// TestEnv - an interface that a test environment implements
@@ -80,7 +80,6 @@ type CouchDBCommonStorageTestEnv struct {
8080
t testing.TB
8181
provider DBProvider
8282
bookkeeperTestEnv *bookkeeping.TestEnv
83-
openDbIds map[string]bool
8483
couchCleanup func()
8584
}
8685

@@ -118,14 +117,12 @@ func (env *CouchDBCommonStorageTestEnv) Init(t testing.TB) {
118117
assert.NoError(t, err)
119118
env.t = t
120119
env.provider = dbProvider
121-
env.openDbIds = make(map[string]bool)
122120
}
123121

124122
// GetDBHandle implements corresponding function from interface TestEnv
125123
func (env *CouchDBCommonStorageTestEnv) GetDBHandle(id string) DB {
126124
db, err := env.provider.GetDBHandle(id)
127125
assert.NoError(env.t, err)
128-
env.openDbIds[id] = true
129126
return db
130127
}
131128

@@ -136,9 +133,9 @@ func (env *CouchDBCommonStorageTestEnv) GetName() string {
136133

137134
// Cleanup implements corresponding function from interface TestEnv
138135
func (env *CouchDBCommonStorageTestEnv) Cleanup() {
139-
for id := range env.openDbIds {
140-
statecouchdb.CleanupDB(id)
141-
}
136+
csdbProvider, _ := env.provider.(*CommonStorageDBProvider)
137+
statecouchdb.CleanupDB(env.t, csdbProvider.VersionedDBProvider)
138+
142139
env.bookkeeperTestEnv.Cleanup()
143140
env.provider.Close()
144141
env.couchCleanup()

core/ledger/kvledger/txmgmt/statedb/statecouchdb/statecouchdb_test.go

Lines changed: 21 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
"testing"
1414
"time"
1515

16+
"github.com/spf13/viper"
17+
1618
"github.com/hyperledger/fabric/common/flogging"
1719
"github.com/hyperledger/fabric/common/ledger/testutil"
1820
"github.com/hyperledger/fabric/core/common/ccprovider"
@@ -21,7 +23,6 @@ import (
2123
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/version"
2224
ledgertestutil "github.com/hyperledger/fabric/core/ledger/testutil"
2325
"github.com/hyperledger/fabric/integration/runner"
24-
"github.com/spf13/viper"
2526
)
2627

2728
func TestMain(m *testing.M) {
@@ -49,6 +50,9 @@ func testMain(m *testing.M) int {
4950
viper.Set("ledger.state.couchDBConfig.maxRetries", 3)
5051
viper.Set("ledger.state.couchDBConfig.maxRetriesOnStartup", 10)
5152
viper.Set("ledger.state.couchDBConfig.requestTimeout", time.Second*35)
53+
// Disable auto warm to avoid error logs when the couchdb database has been dropped
54+
viper.Set("ledger.state.couchDBConfig.autoWarmIndexes", false)
55+
5256
flogging.SetModuleLevel("statecouchdb", "debug")
5357
//run the actual test
5458
return m.Run()
@@ -70,140 +74,82 @@ func couchDBSetup() (addr string, cleanup func()) {
7074

7175
func TestBasicRW(t *testing.T) {
7276
env := NewTestVDBEnv(t)
73-
env.Cleanup("testbasicrw_")
74-
env.Cleanup("testbasicrw_ns")
75-
env.Cleanup("testbasicrw_ns1")
76-
env.Cleanup("testbasicrw_ns2")
77-
defer env.Cleanup("testbasicrw_")
78-
defer env.Cleanup("testbasicrw_ns")
79-
defer env.Cleanup("testbasicrw_ns1")
80-
defer env.Cleanup("testbasicrw_ns2")
77+
defer env.Cleanup()
8178
commontests.TestBasicRW(t, env.DBProvider)
8279

8380
}
8481

8582
func TestMultiDBBasicRW(t *testing.T) {
8683
env := NewTestVDBEnv(t)
87-
env.Cleanup("testmultidbbasicrw_")
88-
env.Cleanup("testmultidbbasicrw_ns1")
89-
env.Cleanup("testmultidbbasicrw2_")
90-
env.Cleanup("testmultidbbasicrw2_ns1")
91-
defer env.Cleanup("testmultidbbasicrw_")
92-
defer env.Cleanup("testmultidbbasicrw_ns1")
93-
defer env.Cleanup("testmultidbbasicrw2_")
94-
defer env.Cleanup("testmultidbbasicrw2_ns1")
84+
defer env.Cleanup()
9585
commontests.TestMultiDBBasicRW(t, env.DBProvider)
9686

9787
}
9888

9989
func TestDeletes(t *testing.T) {
10090
env := NewTestVDBEnv(t)
101-
env.Cleanup("testdeletes_")
102-
env.Cleanup("testdeletes_ns")
103-
defer env.Cleanup("testdeletes_")
104-
defer env.Cleanup("testdeletes_ns")
91+
defer env.Cleanup()
10592
commontests.TestDeletes(t, env.DBProvider)
10693
}
10794

10895
func TestIterator(t *testing.T) {
10996
env := NewTestVDBEnv(t)
110-
env.Cleanup("testiterator_")
111-
env.Cleanup("testiterator_ns1")
112-
env.Cleanup("testiterator_ns2")
113-
env.Cleanup("testiterator_ns3")
114-
defer env.Cleanup("testiterator_")
115-
defer env.Cleanup("testiterator_ns1")
116-
defer env.Cleanup("testiterator_ns2")
117-
defer env.Cleanup("testiterator_ns3")
97+
defer env.Cleanup()
11898
commontests.TestIterator(t, env.DBProvider)
11999
}
120100

121101
// The following tests are unique to couchdb, they are not used in leveldb
122102
// query test
123103
func TestQuery(t *testing.T) {
124104
env := NewTestVDBEnv(t)
125-
env.Cleanup("testquery_")
126-
env.Cleanup("testquery_ns1")
127-
env.Cleanup("testquery_ns2")
128-
env.Cleanup("testquery_ns3")
129-
defer env.Cleanup("testquery_")
130-
defer env.Cleanup("testquery_ns1")
131-
defer env.Cleanup("testquery_ns2")
132-
defer env.Cleanup("testquery_ns3")
105+
defer env.Cleanup()
133106
commontests.TestQuery(t, env.DBProvider)
134107
}
135108

136109
func TestGetStateMultipleKeys(t *testing.T) {
137110

138111
env := NewTestVDBEnv(t)
139-
env.Cleanup("testgetmultiplekeys_")
140-
env.Cleanup("testgetmultiplekeys_ns1")
141-
env.Cleanup("testgetmultiplekeys_ns2")
142-
defer env.Cleanup("testgetmultiplekeys_")
143-
defer env.Cleanup("testgetmultiplekeys_ns1")
144-
defer env.Cleanup("testgetmultiplekeys_ns2")
112+
defer env.Cleanup()
145113
commontests.TestGetStateMultipleKeys(t, env.DBProvider)
146114
}
147115

148116
func TestGetVersion(t *testing.T) {
149117
env := NewTestVDBEnv(t)
150-
env.Cleanup("testgetversion_")
151-
env.Cleanup("testgetversion_ns")
152-
env.Cleanup("testgetversion_ns2")
153-
defer env.Cleanup("testgetversion_")
154-
defer env.Cleanup("testgetversion_ns")
155-
defer env.Cleanup("testgetversion_ns2")
118+
defer env.Cleanup()
156119
commontests.TestGetVersion(t, env.DBProvider)
157120
}
158121

159122
func TestSmallBatchSize(t *testing.T) {
160123
viper.Set("ledger.state.couchDBConfig.maxBatchUpdateSize", 2)
161124
env := NewTestVDBEnv(t)
162-
env.Cleanup("testsmallbatchsize_")
163-
env.Cleanup("testsmallbatchsize_ns1")
164-
defer env.Cleanup("testsmallbatchsize_")
165-
defer env.Cleanup("testsmallbatchsize_ns1")
125+
defer env.Cleanup()
166126
defer viper.Set("ledger.state.couchDBConfig.maxBatchUpdateSize", 1000)
167127
commontests.TestSmallBatchSize(t, env.DBProvider)
168128
}
169129

170130
func TestBatchRetry(t *testing.T) {
171131
env := NewTestVDBEnv(t)
172-
env.Cleanup("testbatchretry_")
173-
env.Cleanup("testbatchretry_ns")
174-
env.Cleanup("testbatchretry_ns1")
175-
defer env.Cleanup("testbatchretry_")
176-
defer env.Cleanup("testbatchretry_ns")
177-
defer env.Cleanup("testbatchretry_ns1")
132+
defer env.Cleanup()
178133
commontests.TestBatchWithIndividualRetry(t, env.DBProvider)
179134
}
180135

181136
func TestValueAndMetadataWrites(t *testing.T) {
182137
env := NewTestVDBEnv(t)
183-
env.Cleanup("testvalueandmetadata_")
184-
env.Cleanup("testvalueandmetadata_ns1")
185-
env.Cleanup("testvalueandmetadata_ns2")
186-
defer env.Cleanup("testvalueandmetadata_")
187-
defer env.Cleanup("testvalueandmetadata_ns1")
188-
defer env.Cleanup("testvalueandmetadata_ns2")
138+
defer env.Cleanup()
189139
commontests.TestValueAndMetadataWrites(t, env.DBProvider)
190140
}
191141

192142
func TestPaginatedRangeQuery(t *testing.T) {
193143
env := NewTestVDBEnv(t)
194-
env.Cleanup("testpaginatedrangequery_")
195-
env.Cleanup("testpaginatedrangequery_ns1")
196-
defer env.Cleanup("testpaginatedrangequery_")
197-
defer env.Cleanup("testpaginatedrangequery_ns1")
144+
defer env.Cleanup()
198145
commontests.TestPaginatedRangeQuery(t, env.DBProvider)
199146
}
200147

201148
// TestUtilityFunctions tests utility functions
202149
func TestUtilityFunctions(t *testing.T) {
203150

204151
env := NewTestVDBEnv(t)
205-
env.Cleanup("testutilityfunctions_")
206-
defer env.Cleanup("testutilityfunctions_")
152+
defer env.Cleanup()
207153

208154
db, err := env.DBProvider.GetDBHandle("testutilityfunctions")
209155
testutil.AssertNoError(t, err, "")
@@ -250,8 +196,7 @@ func TestUtilityFunctions(t *testing.T) {
250196
func TestInvalidJSONFields(t *testing.T) {
251197

252198
env := NewTestVDBEnv(t)
253-
env.Cleanup("testinvalidfields_")
254-
defer env.Cleanup("testinvalidfields_")
199+
defer env.Cleanup()
255200

256201
db, err := env.DBProvider.GetDBHandle("testinvalidfields")
257202
testutil.AssertNoError(t, err, "")
@@ -309,12 +254,7 @@ func TestDebugFunctions(t *testing.T) {
309254
func TestHandleChaincodeDeploy(t *testing.T) {
310255

311256
env := NewTestVDBEnv(t)
312-
env.Cleanup("testinit_")
313-
env.Cleanup("testinit_ns1")
314-
env.Cleanup("testinit_ns2")
315-
defer env.Cleanup("testinit_")
316-
defer env.Cleanup("testinit_ns1")
317-
defer env.Cleanup("testinit_ns2")
257+
defer env.Cleanup()
318258

319259
db, err := env.DBProvider.GetDBHandle("testinit")
320260
testutil.AssertNoError(t, err, "")
@@ -422,8 +362,7 @@ func TestTryCastingToJSON(t *testing.T) {
422362
func TestHandleChaincodeDeployErroneousIndexFile(t *testing.T) {
423363
channelName := "ch1"
424364
env := NewTestVDBEnv(t)
425-
env.Cleanup(channelName)
426-
defer env.Cleanup(channelName)
365+
defer env.Cleanup()
427366
db, err := env.DBProvider.GetDBHandle(channelName)
428367
testutil.AssertNoError(t, err, "")
429368
db.Open()
@@ -480,10 +419,7 @@ func printCompositeKeys(keys []*statedb.CompositeKey) string {
480419
func TestPaginatedQuery(t *testing.T) {
481420

482421
env := NewTestVDBEnv(t)
483-
env.Cleanup("testpaginatedquery_")
484-
env.Cleanup("testpaginatedquery_ns1")
485-
defer env.Cleanup("testpaginatedquery_")
486-
defer env.Cleanup("testpaginatedquery_ns1")
422+
defer env.Cleanup()
487423

488424
db, err := env.DBProvider.GetDBHandle("testpaginatedquery")
489425
testutil.AssertNoError(t, err, "")

core/ledger/kvledger/txmgmt/statedb/statecouchdb/statecouchdb_test_export.go

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ SPDX-License-Identifier: Apache-2.0
66
package statecouchdb
77

88
import (
9-
"strings"
109
"testing"
1110

11+
"github.com/stretchr/testify/assert"
12+
1213
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/statedb"
13-
"github.com/hyperledger/fabric/core/ledger/util/couchdb"
1414
)
1515

1616
// TestVDBEnv provides a couch db backed versioned db for testing
@@ -30,19 +30,24 @@ func NewTestVDBEnv(t testing.TB) *TestVDBEnv {
3030
}
3131

3232
// Cleanup drops the test couch databases and closes the db provider
33-
func (env *TestVDBEnv) Cleanup(dbName string) {
33+
func (env *TestVDBEnv) Cleanup() {
3434
env.t.Logf("Cleaningup TestVDBEnv")
35+
CleanupDB(env.t, env.DBProvider)
36+
3537
env.DBProvider.Close()
36-
CleanupDB(dbName)
3738
}
3839

39-
// CleanupDB drops the test couch databases
40-
func CleanupDB(dbName string) {
41-
//create a new connection
42-
couchDBDef := couchdb.GetCouchDBDefinition()
43-
couchInstance, _ := couchdb.CreateCouchInstance(couchDBDef.URL, couchDBDef.Username, couchDBDef.Password,
44-
couchDBDef.MaxRetries, couchDBDef.MaxRetriesOnStartup, couchDBDef.RequestTimeout, couchDBDef.CreateGlobalChangesDB)
45-
db := couchdb.CouchDatabase{CouchInstance: couchInstance, DBName: strings.ToLower(dbName)}
46-
//drop the test database
47-
db.DropDatabase()
40+
func CleanupDB(t testing.TB, dbProvider statedb.VersionedDBProvider) {
41+
couchdbProvider, _ := dbProvider.(*VersionedDBProvider)
42+
for _, v := range couchdbProvider.databases {
43+
if _, err := v.metadataDB.DropDatabase(); err != nil {
44+
assert.Failf(t, "DropDatabase %s fails. err: %v", v.metadataDB.DBName, err)
45+
}
46+
47+
for _, db := range v.namespaceDBs {
48+
if _, err := db.DropDatabase(); err != nil {
49+
assert.Failf(t, "DropDatabase %s fails. err: %v", db.DBName, err)
50+
}
51+
}
52+
}
4853
}

0 commit comments

Comments
 (0)