github.com/tenywen/fabric@v1.0.0-beta.0.20170620030522-a5b1ed380643/core/ledger/kvledger/txmgmt/statedb/statecouchdb/statecouchdb_test_export.go (about)

     1  /*
     2  Copyright IBM Corp. 2016 All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  		 http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package statecouchdb
    18  
    19  import (
    20  	"strings"
    21  	"testing"
    22  
    23  	"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/statedb"
    24  	"github.com/hyperledger/fabric/core/ledger/util/couchdb"
    25  )
    26  
    27  // TestVDBEnv provides a couch db backed versioned db for testing
    28  type TestVDBEnv struct {
    29  	t          testing.TB
    30  	DBProvider statedb.VersionedDBProvider
    31  }
    32  
    33  // NewTestVDBEnv instantiates and new couch db backed TestVDB
    34  func NewTestVDBEnv(t testing.TB) *TestVDBEnv {
    35  	t.Logf("Creating new TestVDBEnv")
    36  
    37  	dbProvider, _ := NewVersionedDBProvider()
    38  	testVDBEnv := &TestVDBEnv{t, dbProvider}
    39  	// No cleanup for new test environment.  Need to cleanup per test for each DB used in the test.
    40  	return testVDBEnv
    41  }
    42  
    43  // Cleanup drops the test couch databases and closes the db provider
    44  func (env *TestVDBEnv) Cleanup(dbName string) {
    45  	env.t.Logf("Cleaningup TestVDBEnv")
    46  	cleanupDB(strings.ToLower(dbName))
    47  	env.DBProvider.Close()
    48  
    49  }
    50  func cleanupDB(dbName string) {
    51  	//create a new connection
    52  	couchDBDef := couchdb.GetCouchDBDefinition()
    53  	couchInstance, _ := couchdb.CreateCouchInstance(couchDBDef.URL, couchDBDef.Username, couchDBDef.Password,
    54  		couchDBDef.MaxRetries, couchDBDef.MaxRetriesOnStartup, couchDBDef.RequestTimeout)
    55  	db := couchdb.CouchDatabase{CouchInstance: *couchInstance, DBName: dbName}
    56  	//drop the test database
    57  	db.DropDatabase()
    58  }