From ac5846cc327f57a93577b80d9bdb76c24cdc1ed3 Mon Sep 17 00:00:00 2001 From: Chris Elder Date: Tue, 18 Apr 2017 20:36:50 -0400 Subject: [PATCH] [FAB-3238] Move CouchDB config to couchdb 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 --- .../statedb/statecouchdb/statecouchdb.go | 2 +- .../statecouchdb/statecouchdb_test_export.go | 3 +- core/ledger/ledgerconfig/ledger_config.go | 24 ---------- .../ledger/ledgerconfig/ledger_config_test.go | 14 ------ core/ledger/util/couchdb/config.go | 46 +++++++++++++++++++ .../config.go => util/couchdb/config_test.go} | 19 +++++++- core/ledger/util/couchdb/couchdb_test.go | 4 +- 7 files changed, 67 insertions(+), 45 deletions(-) create mode 100644 core/ledger/util/couchdb/config.go rename core/ledger/{kvledger/txmgmt/statedb/statecouchdb/config.go => util/couchdb/config_test.go} (52%) diff --git a/core/ledger/kvledger/txmgmt/statedb/statecouchdb/statecouchdb.go b/core/ledger/kvledger/txmgmt/statedb/statecouchdb/statecouchdb.go index 866d4d6f3e7..d6dc724d1ab 100644 --- a/core/ledger/kvledger/txmgmt/statedb/statecouchdb/statecouchdb.go +++ b/core/ledger/kvledger/txmgmt/statedb/statecouchdb/statecouchdb.go @@ -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 { diff --git a/core/ledger/kvledger/txmgmt/statedb/statecouchdb/statecouchdb_test_export.go b/core/ledger/kvledger/txmgmt/statedb/statecouchdb/statecouchdb_test_export.go index 54df05b0d2c..3b7c335c2e2 100644 --- a/core/ledger/kvledger/txmgmt/statedb/statecouchdb/statecouchdb_test_export.go +++ b/core/ledger/kvledger/txmgmt/statedb/statecouchdb/statecouchdb_test_export.go @@ -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" ) @@ -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} diff --git a/core/ledger/ledgerconfig/ledger_config.go b/core/ledger/ledgerconfig/ledger_config.go index 93e1309a979..adc070e1057 100644 --- a/core/ledger/ledgerconfig/ledger_config.go +++ b/core/ledger/ledgerconfig/ledger_config.go @@ -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") @@ -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") diff --git a/core/ledger/ledgerconfig/ledger_config_test.go b/core/ledger/ledgerconfig/ledger_config_test.go index a7ac39f6b29..30563d9aa37 100644 --- a/core/ledger/ledgerconfig/ledger_config_test.go +++ b/core/ledger/ledgerconfig/ledger_config_test.go @@ -18,7 +18,6 @@ package ledgerconfig import ( "testing" - "time" "github.com/hyperledger/fabric/common/ledger/testutil" ledgertestutil "github.com/hyperledger/fabric/core/ledger/testutil" @@ -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() diff --git a/core/ledger/util/couchdb/config.go b/core/ledger/util/couchdb/config.go new file mode 100644 index 00000000000..6da645f6c3b --- /dev/null +++ b/core/ledger/util/couchdb/config.go @@ -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} +} diff --git a/core/ledger/kvledger/txmgmt/statedb/statecouchdb/config.go b/core/ledger/util/couchdb/config_test.go similarity index 52% rename from core/ledger/kvledger/txmgmt/statedb/statecouchdb/config.go rename to core/ledger/util/couchdb/config_test.go index 3d7f7319cea..8f99cec4dde 100644 --- a/core/ledger/kvledger/txmgmt/statedb/statecouchdb/config.go +++ b/core/ledger/util/couchdb/config_test.go @@ -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) +} diff --git a/core/ledger/util/couchdb/couchdb_test.go b/core/ledger/util/couchdb/couchdb_test.go index a753a47e4d3..e3963581dad 100644 --- a/core/ledger/util/couchdb/couchdb_test.go +++ b/core/ledger/util/couchdb/couchdb_test.go @@ -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 @@ -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()