Skip to content

Commit

Permalink
[FAB-3238] Move CouchDB config to couchdb
Browse files Browse the repository at this point in the history
CouchDB configuration should be moved from the ledgerconfig
package to couchdb package since the config options are specific
to CouchDB.

Move corresponding unit tests to couchdb package.

Change-Id: I438f1ec2aed167b0b3e96304682ffe29d3641ab5
Signed-off-by: Chris Elder <chris.elder@us.ibm.com>
  • Loading branch information
Chris Elder committed Apr 24, 2017
1 parent 8ce1073 commit ac5846c
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type VersionedDBProvider struct {
// NewVersionedDBProvider instantiates VersionedDBProvider
func NewVersionedDBProvider() (*VersionedDBProvider, error) {
logger.Debugf("constructing CouchDB VersionedDBProvider")
couchDBDef := ledgerconfig.GetCouchDBDefinition()
couchDBDef := couchdb.GetCouchDBDefinition()
couchInstance, err := couchdb.CreateCouchInstance(couchDBDef.URL, couchDBDef.Username, couchDBDef.Password,
couchDBDef.MaxRetries, couchDBDef.MaxRetriesOnStartup, couchDBDef.RequestTimeout)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"testing"

"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/statedb"
"github.com/hyperledger/fabric/core/ledger/ledgerconfig"
"github.com/hyperledger/fabric/core/ledger/util/couchdb"
)

Expand Down Expand Up @@ -52,7 +51,7 @@ func (env *TestVDBEnv) Cleanup(dbName string) {
}
func cleanupDB(dbName string) {
//create a new connection
couchDBDef := ledgerconfig.GetCouchDBDefinition()
couchDBDef := couchdb.GetCouchDBDefinition()
couchInstance, _ := couchdb.CreateCouchInstance(couchDBDef.URL, couchDBDef.Username, couchDBDef.Password,
couchDBDef.MaxRetries, couchDBDef.MaxRetriesOnStartup, couchDBDef.RequestTimeout)
db := couchdb.CouchDatabase{CouchInstance: *couchInstance, DBName: dbName}
Expand Down
24 changes: 0 additions & 24 deletions core/ledger/ledgerconfig/ledger_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,11 @@ package ledgerconfig

import (
"path/filepath"
"time"

"github.com/hyperledger/fabric/core/config"
"github.com/spf13/viper"
)

// CouchDBDef contains parameters
type CouchDBDef struct {
URL string
Username string
Password string
MaxRetries int
MaxRetriesOnStartup int
RequestTimeout time.Duration
}

//IsCouchDBEnabled exposes the useCouchDB variable
func IsCouchDBEnabled() bool {
stateDatabase := viper.GetString("ledger.state.stateDatabase")
Expand Down Expand Up @@ -75,19 +64,6 @@ func GetMaxBlockfileSize() int {
return 64 * 1024 * 1024
}

//GetCouchDBDefinition exposes the useCouchDB variable
func GetCouchDBDefinition() *CouchDBDef {

couchDBAddress := viper.GetString("ledger.state.couchDBConfig.couchDBAddress")
username := viper.GetString("ledger.state.couchDBConfig.username")
password := viper.GetString("ledger.state.couchDBConfig.password")
maxRetries := viper.GetInt("ledger.state.couchDBConfig.maxRetries")
maxRetriesOnStartup := viper.GetInt("ledger.state.couchDBConfig.maxRetriesOnStartup")
requestTimeout := viper.GetDuration("ledger.state.couchDBConfig.requestTimeout")

return &CouchDBDef{couchDBAddress, username, password, maxRetries, maxRetriesOnStartup, requestTimeout}
}

//GetQueryLimit exposes the queryLimit variable
func GetQueryLimit() int {
queryLimit := viper.GetInt("ledger.state.queryLimit")
Expand Down
14 changes: 0 additions & 14 deletions core/ledger/ledgerconfig/ledger_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package ledgerconfig

import (
"testing"
"time"

"github.com/hyperledger/fabric/common/ledger/testutil"
ledgertestutil "github.com/hyperledger/fabric/core/ledger/testutil"
Expand Down Expand Up @@ -47,19 +46,6 @@ func TestIsCouchDBEnabled(t *testing.T) {
testutil.AssertEquals(t, updatedValue, true) //test config returns true
}

func TestGetCouchDBDefinition(t *testing.T) {
setUpCoreYAMLConfig()
defer ledgertestutil.ResetConfigToDefaultValues()
viper.Set("ledger.state.stateDatabase", "CouchDB")
couchDBDef := GetCouchDBDefinition()
testutil.AssertEquals(t, couchDBDef.URL, "127.0.0.1:5984")
testutil.AssertEquals(t, couchDBDef.Username, "")
testutil.AssertEquals(t, couchDBDef.Password, "")
testutil.AssertEquals(t, couchDBDef.MaxRetries, 3)
testutil.AssertEquals(t, couchDBDef.MaxRetriesOnStartup, 10)
testutil.AssertEquals(t, couchDBDef.RequestTimeout, time.Second*35)
}

func TestIsHistoryDBEnabledDefault(t *testing.T) {
setUpCoreYAMLConfig()
defaultValue := IsHistoryDBEnabled()
Expand Down
46 changes: 46 additions & 0 deletions core/ledger/util/couchdb/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
Copyright IBM Corp. 2016 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package couchdb

import (
"time"

"github.com/spf13/viper"
)

// CouchDBDef contains parameters
type CouchDBDef struct {
URL string
Username string
Password string
MaxRetries int
MaxRetriesOnStartup int
RequestTimeout time.Duration
}

//GetCouchDBDefinition exposes the useCouchDB variable
func GetCouchDBDefinition() *CouchDBDef {

couchDBAddress := viper.GetString("ledger.state.couchDBConfig.couchDBAddress")
username := viper.GetString("ledger.state.couchDBConfig.username")
password := viper.GetString("ledger.state.couchDBConfig.password")
maxRetries := viper.GetInt("ledger.state.couchDBConfig.maxRetries")
maxRetriesOnStartup := viper.GetInt("ledger.state.couchDBConfig.maxRetriesOnStartup")
requestTimeout := viper.GetDuration("ledger.state.couchDBConfig.requestTimeout")

return &CouchDBDef{couchDBAddress, username, password, maxRetries, maxRetriesOnStartup, requestTimeout}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package statecouchdb
package couchdb

//TODO - Move couch config from ledger_config to here
import (
"testing"
"time"

"github.com/hyperledger/fabric/common/ledger/testutil"
)

func TestGetCouchDBDefinition(t *testing.T) {
couchDBDef := GetCouchDBDefinition()
testutil.AssertEquals(t, couchDBDef.URL, "couchdb:5984")
testutil.AssertEquals(t, couchDBDef.Username, "")
testutil.AssertEquals(t, couchDBDef.Password, "")
testutil.AssertEquals(t, couchDBDef.MaxRetries, 3)
testutil.AssertEquals(t, couchDBDef.MaxRetriesOnStartup, 10)
testutil.AssertEquals(t, couchDBDef.RequestTimeout, time.Second*35)
}
4 changes: 2 additions & 2 deletions core/ledger/util/couchdb/couchdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const badConnectURL = "couchdb:5990"
const updateDocumentConflictError = "conflict"
const updateDocumentConflictReason = "Document update conflict."

var couchDBDef *ledgerconfig.CouchDBDef
var couchDBDef *CouchDBDef

func cleanup(database string) error {
//create a new connection
Expand Down Expand Up @@ -81,7 +81,7 @@ func TestMain(m *testing.M) {
viper.Set("ledger.state.couchDBConfig.requestTimeout", time.Second*35)

// Create CouchDB definition from config parameters
couchDBDef = ledgerconfig.GetCouchDBDefinition()
couchDBDef = GetCouchDBDefinition()

//run the tests
result := m.Run()
Expand Down

0 comments on commit ac5846c

Please sign in to comment.