Skip to content

Commit

Permalink
[FAB-16125] fix allow ledger test to succeed when no tests are run
Browse files Browse the repository at this point in the history
- fixed allow ledger test to succeed when no tests are run.
- extract out initialization and cleanup for couchDB.

Signed-off-by: Chongxin Luo <Chongxin.Luo@ibm.com>
  • Loading branch information
DereckLuo authored and denyeart committed Dec 10, 2019
1 parent 76765db commit b430f81
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
9 changes: 8 additions & 1 deletion core/ledger/kvledger/txmgmt/privacyenabledstate/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
func TestMain(m *testing.M) {
exitCode := m.Run()
for _, testEnv := range testEnvs {
testEnv.Cleanup()
testEnv.StopExternalResource()
}
os.Exit(exitCode)
}
Expand Down Expand Up @@ -84,6 +84,7 @@ func TestDB(t *testing.T) {

func testDB(t *testing.T, env TestEnv) {
env.Init(t)
defer env.Cleanup()
db := env.GetDBHandle(generateLedgerID(t))

updates := NewUpdateBatch()
Expand Down Expand Up @@ -149,6 +150,7 @@ func TestGetStateMultipleKeys(t *testing.T) {

func testGetStateMultipleKeys(t *testing.T, env TestEnv) {
env.Init(t)
defer env.Cleanup()
db := env.GetDBHandle(generateLedgerID(t))

updates := NewUpdateBatch()
Expand Down Expand Up @@ -191,6 +193,7 @@ func TestGetStateRangeScanIterator(t *testing.T) {

func testGetStateRangeScanIterator(t *testing.T, env TestEnv) {
env.Init(t)
defer env.Cleanup()
db := env.GetDBHandle(generateLedgerID(t))

updates := NewUpdateBatch()
Expand Down Expand Up @@ -251,6 +254,7 @@ func TestQueryOnCouchDB(t *testing.T) {

func testQueryOnCouchDB(t *testing.T, env TestEnv) {
env.Init(t)
defer env.Cleanup()
db := env.GetDBHandle(generateLedgerID(t))
updates := NewUpdateBatch()

Expand Down Expand Up @@ -331,6 +335,7 @@ func TestLongDBNameOnCouchDB(t *testing.T) {

func testLongDBNameOnCouchDB(t *testing.T, env TestEnv) {
env.Init(t)
defer env.Cleanup()

// Creates metadataDB (i.e., chainDB)
// Allowed pattern for chainName: [a-z][a-z0-9.-]
Expand Down Expand Up @@ -441,6 +446,7 @@ func createCollectionConfig(collectionName string) *peer.CollectionConfig {

func testHandleChainCodeDeploy(t *testing.T, env TestEnv) {
env.Init(t)
defer env.Cleanup()
db := env.GetDBHandle(generateLedgerID(t))

coll1 := createCollectionConfig("collectionMarbles")
Expand Down Expand Up @@ -545,6 +551,7 @@ func TestMetadataRetrieval(t *testing.T) {

func testMetadataRetrieval(t *testing.T, env TestEnv) {
env.Init(t)
defer env.Cleanup()
db := env.GetDBHandle(generateLedgerID(t))

updates := NewUpdateBatch()
Expand Down
35 changes: 29 additions & 6 deletions core/ledger/kvledger/txmgmt/privacyenabledstate/test_exports.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ import (

// TestEnv - an interface that a test environment implements
type TestEnv interface {
StartExternalResource()
Init(t testing.TB)
GetDBHandle(id string) DB
GetName() string
Cleanup()
StopExternalResource()
}

// Tests will be run against each environment in this array
Expand Down Expand Up @@ -69,6 +71,16 @@ func (env *LevelDBCommonStorageTestEnv) Init(t testing.TB) {
env.dbPath = dbPath
}

// StartExternalResource will be an empty implementation for levelDB test environment.
func (env *LevelDBCommonStorageTestEnv) StartExternalResource() {
// empty implementation
}

// StopExternalResource will be an empty implementation for levelDB test environment.
func (env *LevelDBCommonStorageTestEnv) StopExternalResource() {
// empty implementation
}

// GetDBHandle implements corresponding function from interface TestEnv
func (env *LevelDBCommonStorageTestEnv) GetDBHandle(id string) DB {
db, err := env.provider.GetDBHandle(id)
Expand Down Expand Up @@ -100,6 +112,22 @@ type CouchDBCommonStorageTestEnv struct {
couchCleanup func()
}

// StartExternalResource starts external couchDB resources.
func (env *CouchDBCommonStorageTestEnv) StartExternalResource() {
if env.couchAddress == "" {
env.couchAddress = env.setupCouch()
}
}

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

func (env *CouchDBCommonStorageTestEnv) setupCouch() string {
externalCouch, set := os.LookupEnv("COUCHDB_ADDR")
if set {
Expand All @@ -123,9 +151,7 @@ func (env *CouchDBCommonStorageTestEnv) Init(t testing.TB) {
t.Fatalf("Failed to create redo log directory: %s", err)
}

if env.couchAddress == "" {
env.couchAddress = env.setupCouch()
}
env.StartExternalResource()

stateDBConfig := &StateDBConfig{
StateDBConfig: &ledger.StateDBConfig{
Expand Down Expand Up @@ -173,10 +199,7 @@ func (env *CouchDBCommonStorageTestEnv) GetName() string {

// Cleanup implements corresponding function from interface TestEnv
func (env *CouchDBCommonStorageTestEnv) Cleanup() {
csdbProvider, _ := env.provider.(*CommonStorageDBProvider)
statecouchdb.CleanupDB(env.t, csdbProvider.VersionedDBProvider)
os.RemoveAll(env.redoPath)
env.bookkeeperTestEnv.Cleanup()
env.provider.Close()
env.couchCleanup()
}

0 comments on commit b430f81

Please sign in to comment.