Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[FAB-17339] Refactor couchDB unit test (#482)
- Fixed couchDB stopExternalResource in ./core/ledger/kvledger/txmgmt/privacyenabledstate. Only stop external
couchDB container in this function. Move all DB cleanups in cleanup function.
- For package statecouchdb and couchdd TestMain, check package level global variable couchAddress before starting
external couchDB container, which makes sure couchDB external resource only initialize once per package.

Signed-off-by: Chongxin Luo <Chongxin.Luo@ibm.com>
  • Loading branch information
DereckLuo authored and manish-sethi committed Jan 15, 2020
1 parent c491d69 commit 21fc6f6
Show file tree
Hide file tree
Showing 12 changed files with 198 additions and 168 deletions.
6 changes: 5 additions & 1 deletion core/ledger/kvledger/tests/v1x_test.go
Expand Up @@ -72,7 +72,11 @@ 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 := couchdbtest.CouchDBSetup(couchdbDataUnzipDir, localdHostDir)
couchdbBinds := []string{
fmt.Sprintf("%s:%s", couchdbDataUnzipDir, "/opt/couchdb/data"),
fmt.Sprintf("%s:%s", localdHostDir, "/opt/couchdb/etc/local.d"),
}
couchAddress, cleanup := couchdbtest.CouchDBSetup(couchdbBinds)
defer cleanup()

// set required config data to use state couchdb
Expand Down
31 changes: 7 additions & 24 deletions core/ledger/kvledger/txmgmt/privacyenabledstate/test_exports.go
Expand Up @@ -7,7 +7,6 @@ SPDX-License-Identifier: Apache-2.0
package privacyenabledstate

import (
"fmt"
"io/ioutil"
"os"
"testing"
Expand All @@ -19,7 +18,7 @@ import (
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/statedb/statecouchdb"
"github.com/hyperledger/fabric/core/ledger/mock"
"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"
)

Expand Down Expand Up @@ -115,35 +114,15 @@ type CouchDBCommonStorageTestEnv struct {
// StartExternalResource starts external couchDB resources.
func (env *CouchDBCommonStorageTestEnv) StartExternalResource() {
if env.couchAddress == "" {
env.couchAddress = env.setupCouch()
env.couchAddress, env.couchCleanup = couchdbtest.CouchDBSetup(nil)
}
}

// StopExternalResource stops external couchDB resources.
func (env *CouchDBCommonStorageTestEnv) StopExternalResource() {
csdbProvider, _ := env.provider.(*CommonStorageDBProvider)
if csdbProvider != nil {
statecouchdb.CleanupDB(env.t, csdbProvider.VersionedDBProvider)
if env.couchAddress != "" {
env.couchCleanup()
os.Unsetenv("COUCHDB_ADDR")
}
}

func (env *CouchDBCommonStorageTestEnv) setupCouch() string {
externalCouch, set := os.LookupEnv("COUCHDB_ADDR")
if set {
env.couchCleanup = func() {}
return externalCouch
}

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

// Init implements corresponding function from interface TestEnv
Expand Down Expand Up @@ -201,6 +180,10 @@ func (env *CouchDBCommonStorageTestEnv) GetName() string {

// Cleanup implements corresponding function from interface TestEnv
func (env *CouchDBCommonStorageTestEnv) Cleanup() {
csdbProvider := env.provider.(*CommonStorageDBProvider)
if csdbProvider != nil {
statecouchdb.CleanupDB(env.t, csdbProvider.VersionedDBProvider)
}
os.RemoveAll(env.redoPath)
env.bookkeeperTestEnv.Cleanup()
env.provider.Close()
Expand Down
23 changes: 12 additions & 11 deletions core/ledger/kvledger/txmgmt/pvtstatepurgemgmt/purge_mgr_test.go
Expand Up @@ -21,11 +21,16 @@ import (
"github.com/stretchr/testify/assert"
)

const (
levelDBtestEnvName = "levelDB_TestEnv"
couchDBtestEnvName = "couchDB_TestEnv"
)

// 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{},
var testEnvs = map[string]privacyenabledstate.TestEnv{
levelDBtestEnvName: &privacyenabledstate.LevelDBCommonStorageTestEnv{},
couchDBtestEnvName: &privacyenabledstate.CouchDBCommonStorageTestEnv{},
}

func TestMain(m *testing.M) {
Expand Down Expand Up @@ -106,11 +111,7 @@ func testPurgeMgr(t *testing.T, dbEnv privacyenabledstate.TestEnv) {
}

func TestPurgeMgrForCommittingPvtDataOfOldBlocks(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) { testPurgeMgrForCommittingPvtDataOfOldBlocks(t, dbEnv) })
}
}
Expand Down Expand Up @@ -163,7 +164,7 @@ func testPurgeMgrForCommittingPvtDataOfOldBlocks(t *testing.T, dbEnv privacyenab
}

func TestKeyUpdateBeforeExpiryBlock(t *testing.T) {
dbEnv := &privacyenabledstate.LevelDBCommonStorageTestEnv{}
dbEnv := testEnvs[levelDBtestEnvName]
ledgerid := "testledger-perge-mgr"
btlPolicy := btltestutil.SampleBTLPolicy(
map[[2]string]uint64{
Expand Down Expand Up @@ -203,7 +204,7 @@ func TestKeyUpdateBeforeExpiryBlock(t *testing.T) {
}

func TestOnlyHashUpdateInExpiryBlock(t *testing.T) {
dbEnv := &privacyenabledstate.LevelDBCommonStorageTestEnv{}
dbEnv := testEnvs[levelDBtestEnvName]
ledgerid := "testledger-perge-mgr"
btlPolicy := btltestutil.SampleBTLPolicy(
map[[2]string]uint64{
Expand Down Expand Up @@ -250,7 +251,7 @@ func TestOnlyHashUpdateInExpiryBlock(t *testing.T) {
}

func TestOnlyHashDeleteBeforeExpiryBlock(t *testing.T) {
dbEnv := &privacyenabledstate.LevelDBCommonStorageTestEnv{}
dbEnv := testEnvs[levelDBtestEnvName]
ledgerid := "testledger-perge-mgr"
btlPolicy := btltestutil.SampleBTLPolicy(
map[[2]string]uint64{
Expand Down
Expand Up @@ -62,8 +62,8 @@ func TestRedoLogger(t *testing.T) {
}

func TestCouchdbRedoLogger(t *testing.T) {
testEnv := NewTestVDBEnv(t)
defer testEnv.Cleanup()
testEnv.init(t, &statedb.Cache{})
defer testEnv.cleanup()

// commitToRedologAndRestart - a helper function that commits directly to redologs and restart the statedb
commitToRedologAndRestart := func(newVal string, version *version.Height) {
Expand All @@ -80,7 +80,7 @@ func TestCouchdbRedoLogger(t *testing.T) {
},
),
)
testEnv.CloseAndReopen()
testEnv.closeAndReopen()
}
// verifyExpectedVal - a helper function that verifies the statedb contents
verifyExpectedVal := func(expectedVal string, expectedSavepoint *version.Height) {
Expand Down

0 comments on commit 21fc6f6

Please sign in to comment.