Skip to content

Commit

Permalink
[FAB-17238] Refactor couchdb test in ./core/ledger/
Browse files Browse the repository at this point in the history
- Refactor couchdb tests in ./core/ledger/... Only starting couchDB container
once for all tests.
- Fix couchdb containers not being cleaned up after tests.

Signed-off-by: Chongxin Luo <Chongxin.Luo@ibm.com>
  • Loading branch information
DereckLuo authored and mastersingh24 committed Dec 18, 2019
1 parent de14789 commit 0b5923b
Show file tree
Hide file tree
Showing 13 changed files with 122 additions and 66 deletions.
17 changes: 0 additions & 17 deletions core/ledger/kvledger/tests/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ SPDX-License-Identifier: Apache-2.0
package tests

import (
"fmt"
"testing"

"github.com/golang/protobuf/proto"
Expand All @@ -23,7 +22,6 @@ import (
"github.com/hyperledger/fabric/core/ledger/kvledger/tests/fakes"
lutils "github.com/hyperledger/fabric/core/ledger/util"
"github.com/hyperledger/fabric/core/ledger/util/couchdb"
"github.com/hyperledger/fabric/integration/runner"
"github.com/hyperledger/fabric/protoutil"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -233,21 +231,6 @@ func setBlockFlagsToValid(block *common.Block) {
lutils.NewTxValidationFlagsSetValue(len(block.Data.Data), protopeer.TxValidationCode_VALID)
}

func couchDBSetup(t *testing.T, couchdbMountDir string, localdHostDir string) (addr string, cleanup func()) {
couchDB := &runner.CouchDB{
Name: "ledger13_upgrade_test",
Binds: []string{
fmt.Sprintf("%s:%s", couchdbMountDir, "/opt/couchdb/data"),
fmt.Sprintf("%s:%s", localdHostDir, "/opt/couchdb/etc/local.d"),
},
}
err := couchDB.Start()
require.NoError(t, err)
return couchDB.Address(), func() {
couchDB.Stop()
}
}

func dropCouchDBs(t *testing.T, couchdbConfig *couchdb.Config) {
couchInstance, err := couchdb.CreateCouchInstance(couchdbConfig, &disabled.Provider{})
require.NoError(t, err)
Expand Down
3 changes: 2 additions & 1 deletion core/ledger/kvledger/tests/v1x_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/hyperledger/fabric/core/ledger/kvledger"
"github.com/hyperledger/fabric/core/ledger/mock"
"github.com/hyperledger/fabric/core/ledger/util/couchdb"
"github.com/hyperledger/fabric/core/ledger/util/couchdbtest"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -71,7 +72,7 @@ func TestV13WithStateCouchdb(t *testing.T) {
testutil.CopyDir("testdata/v13_statecouchdb/couchdb_etc/local.d", localdHostDir, true)

// start couchdb using couchdbDataUnzipDir and localdHostDir as mount dirs
couchAddress, cleanup := couchDBSetup(t, couchdbDataUnzipDir, localdHostDir)
couchAddress, cleanup := couchdbtest.CouchDBSetup(couchdbDataUnzipDir, localdHostDir)
defer cleanup()

// set required config data to use state couchdb
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ func (env *CouchDBCommonStorageTestEnv) StopExternalResource() {
if csdbProvider != nil {
statecouchdb.CleanupDB(env.t, csdbProvider.VersionedDBProvider)
env.couchCleanup()
os.Unsetenv("COUCHDB_ADDR")
}
}

Expand All @@ -141,6 +142,7 @@ func (env *CouchDBCommonStorageTestEnv) setupCouch() string {
panic(err)
}
env.couchCleanup = func() { couchDB.Stop() }
os.Setenv("COUCHDB_ADDR", couchDB.Address())
return couchDB.Address()
}

Expand Down
19 changes: 13 additions & 6 deletions core/ledger/kvledger/txmgmt/pvtstatepurgemgmt/purge_mgr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,24 @@ import (
"github.com/stretchr/testify/assert"
)

// Tests will be run against each environment in this array
// For example, to skip CouchDB tests, remove &couchDBLockBasedEnv{}
var testEnvs = []privacyenabledstate.TestEnv{
&privacyenabledstate.LevelDBCommonStorageTestEnv{},
&privacyenabledstate.CouchDBCommonStorageTestEnv{},
}

func TestMain(m *testing.M) {
flogging.ActivateSpec("pvtstatepurgemgmt,privacyenabledstate=debug")
os.Exit(m.Run())
exitCode := m.Run()
for _, testEnv := range testEnvs {
testEnv.StopExternalResource()
}
os.Exit(exitCode)
}

func TestPurgeMgr(t *testing.T) {
dbEnvs := []privacyenabledstate.TestEnv{
&privacyenabledstate.LevelDBCommonStorageTestEnv{},
&privacyenabledstate.CouchDBCommonStorageTestEnv{},
}
for _, dbEnv := range dbEnvs {
for _, dbEnv := range testEnvs {
t.Run(dbEnv.GetName(), func(t *testing.T) { testPurgeMgr(t, dbEnv) })
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,36 +23,22 @@ import (
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/statedb/commontests"
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/version"
"github.com/hyperledger/fabric/core/ledger/util/couchdb"
"github.com/hyperledger/fabric/integration/runner"
"github.com/hyperledger/fabric/core/ledger/util/couchdbtest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestMain(m *testing.M) {
flogging.ActivateSpec("statecouchdb=debug")

address, cleanup := couchDBSetup()
address, cleanup := couchdbtest.CouchDBSetup("", "")
couchAddress = address

rc := m.Run()
cleanup()
os.Exit(rc)
}

func couchDBSetup() (addr string, cleanup func()) {
externalCouch, set := os.LookupEnv("COUCHDB_ADDR")
if set {
return externalCouch, func() {}
}

couchDB := &runner.CouchDB{}
if err := couchDB.Start(); err != nil {
err := fmt.Errorf("failed to start couchDB: %s", err)
panic(err)
}
return couchDB.Address(), func() { couchDB.Stop() }
}

func TestBasicRW(t *testing.T) {
env := NewTestVDBEnv(t)
defer env.Cleanup()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
func TestCollectionValidation(t *testing.T) {
testEnv := testEnvsMap[levelDBtestEnvName]
testEnv.init(t, "testLedger", nil)
defer testEnv.cleanup()
txMgr := testEnv.getTxMgr()
populateCollConfigForTest(t, txMgr.(*LockBasedTxMgr),
[]collConfigkey{
Expand Down Expand Up @@ -54,6 +55,7 @@ func TestCollectionValidation(t *testing.T) {
func TestPvtGetNoCollection(t *testing.T) {
testEnv := testEnvs[0]
testEnv.init(t, "test-pvtdata-get-no-collection", nil)
defer testEnv.cleanup()
txMgr := testEnv.getTxMgr().(*LockBasedTxMgr)
cryptoProvider, err := sw.NewDefaultSecurityLevelWithKeystore(sw.NewDummyKeyStore())
assert.NoError(t, err)
Expand All @@ -67,6 +69,7 @@ func TestPvtGetNoCollection(t *testing.T) {
func TestPvtPutNoCollection(t *testing.T) {
testEnv := testEnvs[0]
testEnv.init(t, "test-pvtdata-put-no-collection", nil)
defer testEnv.cleanup()
txMgr := testEnv.getTxMgr().(*LockBasedTxMgr)
txsim, err := txMgr.NewTxSimulator("txid")
assert.NoError(t, err)
Expand All @@ -78,6 +81,7 @@ func TestPvtPutNoCollection(t *testing.T) {
func TestNoCollectionValidationCheck(t *testing.T) {
testEnv := testEnvs[0]
testEnv.init(t, "test-no-collection-validation-check", nil)
defer testEnv.cleanup()
txMgr := testEnv.getTxMgr().(*LockBasedTxMgr)
qe, err := txMgr.NewQueryExecutorNoCollChecks()
assert.NoError(t, err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func TestPvtdataResultsItr(t *testing.T) {
},
)
testEnv.init(t, "test-pvtdata-range-queries", btlPolicy)
defer testEnv.cleanup()

txMgr := testEnv.getTxMgr().(*LockBasedTxMgr)
populateCollConfigForTest(t, txMgr, []collConfigkey{
Expand Down Expand Up @@ -90,6 +91,7 @@ func testPrivateDataMetadataRetrievalByHash(t *testing.T, env testEnv) {
},
)
env.init(t, ledgerid, btlPolicy)
defer env.cleanup()

txMgr := env.getTxMgr()
bg, _ := testutil.NewBlockGenerator(t, ledgerid, false)
Expand Down Expand Up @@ -137,6 +139,7 @@ func testGetPvtdataHash(t *testing.T, env testEnv) {
},
)
env.init(t, ledgerid, btlPolicy)
defer env.cleanup()
txMgr := env.getTxMgr().(*LockBasedTxMgr)
populateCollConfigForTest(t, txMgr, []collConfigkey{{"ns", "coll"}}, version.NewHeight(1, 1))

Expand Down
8 changes: 7 additions & 1 deletion core/ledger/kvledger/txmgmt/txmgr/lockbasedtxmgr/pkg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestMain(m *testing.M) {
)
exitCode := m.Run()
for _, testEnv := range testEnvs {
testEnv.cleanup()
testEnv.stopExternalResource()
}
os.Exit(exitCode)
}
Expand All @@ -47,6 +47,7 @@ type testEnv interface {
getTxMgr() txmgr.TxMgr
getVDB() privacyenabledstate.DB
init(t *testing.T, testLedgerID string, btlPolicy pvtdatapolicy.BTLPolicy)
stopExternalResource()
}

const (
Expand Down Expand Up @@ -124,9 +125,14 @@ func (env *lockBasedEnv) cleanup() {
env.txmgr.Shutdown()
env.testDBEnv.Cleanup()
env.testBookkeepingEnv.Cleanup()
env.dbInitialized = false
}
}

func (env *lockBasedEnv) stopExternalResource() {
env.testDBEnv.StopExternalResource()
}

//////////// txMgrTestHelper /////////////

type txMgrTestHelper struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func TestStateListener(t *testing.T) {

testEnv := testEnvsMap[levelDBtestEnvName]
testEnv.init(t, testLedgerid, nil)
defer testEnv.cleanup()
txmgr := testEnv.getTxMgr().(*LockBasedTxMgr)
txmgr.stateListeners = []ledger.StateListener{ml1, ml2, ml3}

Expand Down Expand Up @@ -134,6 +135,7 @@ func TestStateListener(t *testing.T) {
func TestStateListenerQueryExecutor(t *testing.T) {
testEnv := testEnvsMap[levelDBtestEnvName]
testEnv.init(t, "testLedger", nil)
defer testEnv.cleanup()
txMgr := testEnv.getTxMgr().(*LockBasedTxMgr)

namespace := "ns"
Expand Down

0 comments on commit 0b5923b

Please sign in to comment.