github.com/renegr87/renegr87@v2.1.1+incompatible/core/ledger/kvledger/txmgmt/statedb/statecouchdb/statecouchdb_test_export.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package statecouchdb
     8  
     9  import (
    10  	"io/ioutil"
    11  	"os"
    12  	"testing"
    13  	"time"
    14  
    15  	"github.com/hyperledger/fabric/common/metrics/disabled"
    16  	"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/statedb"
    17  	"github.com/hyperledger/fabric/core/ledger/util/couchdb"
    18  	"github.com/hyperledger/fabric/core/ledger/util/couchdbtest"
    19  	"github.com/stretchr/testify/require"
    20  )
    21  
    22  // testVDBEnv provides a couch db backed versioned db for testing
    23  type testVDBEnv struct {
    24  	t              testing.TB
    25  	couchAddress   string
    26  	DBProvider     statedb.VersionedDBProvider
    27  	config         *couchdb.Config
    28  	cleanupCouchDB func()
    29  }
    30  
    31  func (env *testVDBEnv) init(t testing.TB, cache *statedb.Cache) {
    32  	t.Logf("Initializing TestVDBEnv")
    33  	redoPath, err := ioutil.TempDir("", "cvdbenv")
    34  	if err != nil {
    35  		t.Fatalf("Failed to create redo log directory: %s", err)
    36  	}
    37  
    38  	env.startExternalResource()
    39  
    40  	config := &couchdb.Config{
    41  		Address:             env.couchAddress,
    42  		Username:            "",
    43  		Password:            "",
    44  		InternalQueryLimit:  1000,
    45  		MaxBatchUpdateSize:  1000,
    46  		MaxRetries:          3,
    47  		MaxRetriesOnStartup: 20,
    48  		RequestTimeout:      35 * time.Second,
    49  		RedoLogPath:         redoPath,
    50  	}
    51  	dbProvider, err := NewVersionedDBProvider(config, &disabled.Provider{}, cache)
    52  	if err != nil {
    53  		t.Fatalf("Error creating CouchDB Provider: %s", err)
    54  	}
    55  
    56  	env.t = t
    57  	env.DBProvider = dbProvider
    58  	env.config = config
    59  }
    60  
    61  // startExternalResource sstarts external couchDB resources for testVDBEnv.
    62  func (env *testVDBEnv) startExternalResource() {
    63  	if env.couchAddress == "" {
    64  		env.couchAddress, env.cleanupCouchDB = couchdbtest.CouchDBSetup(nil)
    65  	}
    66  }
    67  
    68  // stopExternalResource stops external couchDB resources.
    69  func (env *testVDBEnv) stopExternalResource() {
    70  	if env.couchAddress != "" {
    71  		env.cleanupCouchDB()
    72  	}
    73  }
    74  
    75  func (env *testVDBEnv) closeAndReopen() {
    76  	env.DBProvider.Close()
    77  	dbProvider, _ := NewVersionedDBProvider(env.config, &disabled.Provider{}, &statedb.Cache{})
    78  	env.DBProvider = dbProvider
    79  }
    80  
    81  // Cleanup drops the test couch databases and closes the db provider
    82  func (env *testVDBEnv) cleanup() {
    83  	env.t.Logf("Cleaningup TestVDBEnv")
    84  	cleanupDB(env.t, env.DBProvider.(*VersionedDBProvider).couchInstance)
    85  	env.DBProvider.Close()
    86  	os.RemoveAll(env.config.RedoLogPath)
    87  }
    88  
    89  // CleanupDB deletes all the databases other than fabric internal database
    90  func CleanupDB(t testing.TB, dbProvider statedb.VersionedDBProvider) {
    91  	cleanupDB(t, dbProvider.(*VersionedDBProvider).couchInstance)
    92  }
    93  
    94  func cleanupDB(t testing.TB, couchInstance *couchdb.CouchInstance) {
    95  	dbNames, err := couchInstance.RetrieveApplicationDBNames()
    96  	require.NoError(t, err)
    97  	for _, dbName := range dbNames {
    98  		if dbName != fabricInternalDBName {
    99  			testutilDropDB(t, couchInstance, dbName)
   100  		}
   101  	}
   102  }
   103  
   104  func testutilDropDB(t testing.TB, couchInstance *couchdb.CouchInstance, dbName string) {
   105  	db := &couchdb.CouchDatabase{
   106  		CouchInstance: couchInstance,
   107  		DBName:        dbName,
   108  	}
   109  	response, err := db.DropDatabase()
   110  	require.NoError(t, err)
   111  	require.True(t, response.Ok)
   112  }