Skip to content

Commit

Permalink
[FAB-9102] Start CouchDB as needed in ledger tests
Browse files Browse the repository at this point in the history
If the COUCHDB_ADDR environment variable is set, it should point to a
running instance of CouchdB to by used by tests. When the environment
variable is not set, an containerized instance of CouchDB will be
launched on an ephemeral port of the loopback adapter.

Change-Id: If9c7f7e2dc5fcc493f8409aee96667382be95353
Signed-off-by: Matthew Sykes <sykesmat@us.ibm.com>
Signed-off-by: Will Lahti <wtlahti@us.ibm.com>
  • Loading branch information
wlahti authored and sykesm committed Apr 13, 2018
1 parent b1d761c commit eafd975
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 55 deletions.
30 changes: 25 additions & 5 deletions core/ledger/kvledger/txmgmt/privacyenabledstate/test_exports.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ SPDX-License-Identifier: Apache-2.0
package privacyenabledstate

import (
"fmt"
"os"
"testing"
"time"

"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/statedb/statecouchdb"
"github.com/hyperledger/fabric/core/ledger/ledgerconfig"
"github.com/hyperledger/fabric/integration/runner"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -70,16 +72,33 @@ func (env *LevelDBCommonStorageTestEnv) Cleanup() {

// CouchDBCommonStorageTestEnv implements TestEnv interface for couchdb based storage
type CouchDBCommonStorageTestEnv struct {
t testing.TB
provider DBProvider
openDbIds map[string]bool
t testing.TB
provider DBProvider
openDbIds map[string]bool
couchCleanup func()
}

func (env *CouchDBCommonStorageTestEnv) setupCouch() string {
externalCouch, set := os.LookupEnv("COUCHDB_ADDR")
if set {
env.couchCleanup = func() {}
return externalCouch
}

couchDB := &runner.CouchDB{}
if err := couchDB.Start(); err != nil {
err := fmt.Errorf("failed to start couchDB: %s", err)
panic(err)
}
env.couchCleanup = func() { couchDB.Stop() }
return couchDB.Address()
}

// Init implements corresponding function from interface TestEnv
func (env *CouchDBCommonStorageTestEnv) Init(t testing.TB) {
viper.Set("ledger.state.stateDatabase", "CouchDB")
// both vagrant and CI have couchdb configured at host "couchdb"
viper.Set("ledger.state.couchDBConfig.couchDBAddress", "couchdb:5984")
couchAddr := env.setupCouch()
viper.Set("ledger.state.couchDBConfig.couchDBAddress", couchAddr)
// Replace with correct username/password such as
// admin/admin if user security is enabled on couchdb.
viper.Set("ledger.state.couchDBConfig.username", "")
Expand Down Expand Up @@ -113,6 +132,7 @@ func (env *CouchDBCommonStorageTestEnv) Cleanup() {
statecouchdb.CleanupDB(id)
}
env.provider.Close()
env.couchCleanup()
}

func removeDBPath(t testing.TB) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
/*
Copyright IBM Corp. 2016, 2017 All Rights Reserved.
Copyright IBM Corp. 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.
SPDX-License-Identifier: Apache-2.0
*/

package statecouchdb
Expand All @@ -30,20 +20,26 @@ import (
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/statedb/commontests"
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/version"
ledgertestutil "github.com/hyperledger/fabric/core/ledger/testutil"
"github.com/hyperledger/fabric/integration/runner"
"github.com/spf13/viper"
)

func TestMain(m *testing.M) {
os.Exit(testMain(m))
}

func testMain(m *testing.M) int {
// Read the core.yaml file for default config.
ledgertestutil.SetupCoreYAMLConfig()
viper.Set("peer.fileSystemPath", "/tmp/fabric/ledgertests/kvledger/txmgmt/statedb/statecouchdb")

// Switch to CouchDB
couchAddress, cleanup := couchDBSetup()
defer cleanup()
viper.Set("ledger.state.stateDatabase", "CouchDB")
defer viper.Set("ledger.state.stateDatabase", "goleveldb")

// both vagrant and CI have couchdb configured at host "couchdb"
viper.Set("ledger.state.couchDBConfig.couchDBAddress", "couchdb:5984")
viper.Set("ledger.state.couchDBConfig.couchDBAddress", couchAddress)
// Replace with correct username/password such as
// admin/admin if user security is enabled on couchdb.
viper.Set("ledger.state.couchDBConfig.username", "")
Expand All @@ -53,11 +49,21 @@ func TestMain(m *testing.M) {
viper.Set("ledger.state.couchDBConfig.requestTimeout", time.Second*35)

//run the actual test
result := m.Run()
return m.Run()
}

//revert to default goleveldb
viper.Set("ledger.state.stateDatabase", "goleveldb")
os.Exit(result)
func couchDBSetup() (addr string, cleanup func()) {
externalCouch, set := os.LookupEnv("COUCHDB_ADDR")
if set {
return externalCouch, func() {}
}

couchDB := &runner.CouchDB{}
if err := couchDB.Start(); err != nil {
err := fmt.Errorf("failed to start couchDB: %s", err)
panic(err)
}
return couchDB.Address(), func() { couchDB.Stop() }
}

func TestBasicRW(t *testing.T) {
Expand Down
19 changes: 6 additions & 13 deletions core/ledger/util/couchdb/config_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
/*
Copyright IBM Corp. 2016 All Rights Reserved.
Copyright IBM Corp. 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.
SPDX-License-Identifier: Apache-2.0
*/

package couchdb
Expand All @@ -21,11 +11,14 @@ import (
"time"

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

func TestGetCouchDBDefinition(t *testing.T) {
expectedAddress := viper.GetString("ledger.state.couchDBConfig.couchDBAddress")

couchDBDef := GetCouchDBDefinition()
testutil.AssertEquals(t, couchDBDef.URL, "couchdb:5984")
testutil.AssertEquals(t, couchDBDef.URL, expectedAddress)
testutil.AssertEquals(t, couchDBDef.Username, "")
testutil.AssertEquals(t, couchDBDef.Password, "")
testutil.AssertEquals(t, couchDBDef.MaxRetries, 3)
Expand Down
45 changes: 26 additions & 19 deletions core/ledger/util/couchdb/couchdb_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
/*
Copyright IBM Corp. 2016, 2017 All Rights Reserved.
Copyright IBM Corp. 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.
SPDX-License-Identifier: Apache-2.0
*/

package couchdb
Expand All @@ -29,6 +19,7 @@ import (
"github.com/hyperledger/fabric/common/ledger/testutil"
"github.com/hyperledger/fabric/core/ledger/ledgerconfig"
ledgertestutil "github.com/hyperledger/fabric/core/ledger/testutil"
"github.com/hyperledger/fabric/integration/runner"
logging "github.com/op/go-logging"
"github.com/spf13/viper"
)
Expand Down Expand Up @@ -67,14 +58,20 @@ type Asset struct {
var assetJSON = []byte(`{"asset_name":"marble1","color":"blue","size":"35","owner":"jerry"}`)

func TestMain(m *testing.M) {
os.Exit(testMain(m))
}

func testMain(m *testing.M) int {
// Read the core.yaml file for default config.
ledgertestutil.SetupCoreYAMLConfig()

// Switch to CouchDB
couchAddress, cleanup := couchDBSetup()
defer cleanup()
viper.Set("ledger.state.stateDatabase", "CouchDB")
defer viper.Set("ledger.state.stateDatabase", "goleveldb")

// both vagrant and CI have couchdb configured at host "couchdb"
viper.Set("ledger.state.couchDBConfig.couchDBAddress", "couchdb:5984")
viper.Set("ledger.state.couchDBConfig.couchDBAddress", couchAddress)
// Replace with correct username/password such as
// admin/admin if user security is enabled on couchdb.
viper.Set("ledger.state.couchDBConfig.username", "")
Expand All @@ -92,11 +89,21 @@ func TestMain(m *testing.M) {
couchDBDef = GetCouchDBDefinition()

//run the tests
result := m.Run()
return m.Run()
}

func couchDBSetup() (addr string, cleanup func()) {
externalCouch, set := os.LookupEnv("COUCHDB_ADDR")
if set {
return externalCouch, func() {}
}

//revert to default goleveldb
viper.Set("ledger.state.stateDatabase", "goleveldb")
os.Exit(result)
couchDB := &runner.CouchDB{}
if err := couchDB.Start(); err != nil {
err := fmt.Errorf("failed to start couchDB: %s", err)
panic(err)
}
return couchDB.Address(), func() { couchDB.Stop() }
}

func TestDBConnectionDef(t *testing.T) {
Expand Down Expand Up @@ -980,7 +987,7 @@ func TestIndexOperations(t *testing.T) {
for _, elem := range *listResult {
testutil.AssertEquals(t, elem.DesignDocument, "indexSizeSortDoc")
testutil.AssertEquals(t, elem.Name, "indexSizeSortName")
//ensure the index defintion is correct, CouchDB 2.1.1 will also return "partial_filter_selector":{}
//ensure the index definition is correct, CouchDB 2.1.1 will also return "partial_filter_selector":{}
testutil.AssertEquals(t, strings.Contains(elem.Definition, `"fields":[{"size":"desc"}]`), true)
}

Expand Down
1 change: 1 addition & 0 deletions unit-test/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ unit-tests:
- CORE_VM_DOCKER_ATTACHSTDOUT=true
- VERBOSE
- JOB_TYPE=${JOB_TYPE}
- COUCHDB_ADDR=couchdb:5984
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- ${GOPATH}/src/github.com/hyperledger/fabric:/opt/gopath/src/github.com/hyperledger/fabric
Expand Down

0 comments on commit eafd975

Please sign in to comment.