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

     1  /*
     2  Copyright IBM Corp. 2016, 2017 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  	"os"
    21  	"testing"
    22  	"time"
    23  
    24  	"github.com/hyperledger/fabric/common/ledger/testutil"
    25  	"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/statedb"
    26  	"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/statedb/commontests"
    27  	"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/version"
    28  	"github.com/hyperledger/fabric/core/ledger/ledgerconfig"
    29  	ledgertestutil "github.com/hyperledger/fabric/core/ledger/testutil"
    30  	"github.com/spf13/viper"
    31  )
    32  
    33  func TestMain(m *testing.M) {
    34  
    35  	// Read the core.yaml file for default config.
    36  	ledgertestutil.SetupCoreYAMLConfig()
    37  	viper.Set("peer.fileSystemPath", "/tmp/fabric/ledgertests/kvledger/txmgmt/statedb/statecouchdb")
    38  
    39  	// Switch to CouchDB
    40  	viper.Set("ledger.state.stateDatabase", "CouchDB")
    41  
    42  	// both vagrant and CI have couchdb configured at host "couchdb"
    43  	viper.Set("ledger.state.couchDBConfig.couchDBAddress", "couchdb:5984")
    44  	// Replace with correct username/password such as
    45  	// admin/admin if user security is enabled on couchdb.
    46  	viper.Set("ledger.state.couchDBConfig.username", "")
    47  	viper.Set("ledger.state.couchDBConfig.password", "")
    48  	viper.Set("ledger.state.couchDBConfig.maxRetries", 3)
    49  	viper.Set("ledger.state.couchDBConfig.maxRetriesOnStartup", 10)
    50  	viper.Set("ledger.state.couchDBConfig.requestTimeout", time.Second*35)
    51  
    52  	//run the actual test
    53  	result := m.Run()
    54  
    55  	//revert to default goleveldb
    56  	viper.Set("ledger.state.stateDatabase", "goleveldb")
    57  	os.Exit(result)
    58  }
    59  
    60  func TestBasicRW(t *testing.T) {
    61  	if ledgerconfig.IsCouchDBEnabled() == true {
    62  
    63  		env := NewTestVDBEnv(t)
    64  		env.Cleanup("testbasicrw")
    65  		defer env.Cleanup("testbasicrw")
    66  		commontests.TestBasicRW(t, env.DBProvider)
    67  
    68  	}
    69  }
    70  
    71  func TestMultiDBBasicRW(t *testing.T) {
    72  	if ledgerconfig.IsCouchDBEnabled() == true {
    73  
    74  		env := NewTestVDBEnv(t)
    75  		env.Cleanup("testmultidbbasicrw")
    76  		env.Cleanup("testmultidbbasicrw2")
    77  		defer env.Cleanup("testmultidbbasicrw")
    78  		defer env.Cleanup("testmultidbbasicrw2")
    79  		commontests.TestMultiDBBasicRW(t, env.DBProvider)
    80  
    81  	}
    82  }
    83  
    84  func TestDeletes(t *testing.T) {
    85  	if ledgerconfig.IsCouchDBEnabled() == true {
    86  		env := NewTestVDBEnv(t)
    87  		env.Cleanup("testdeletes")
    88  		defer env.Cleanup("testdeletes")
    89  		commontests.TestDeletes(t, env.DBProvider)
    90  	}
    91  }
    92  
    93  func TestIterator(t *testing.T) {
    94  	if ledgerconfig.IsCouchDBEnabled() == true {
    95  
    96  		env := NewTestVDBEnv(t)
    97  		env.Cleanup("testiterator")
    98  		defer env.Cleanup("testiterator")
    99  		commontests.TestIterator(t, env.DBProvider)
   100  
   101  	}
   102  }
   103  
   104  func TestEncodeDecodeValueAndVersion(t *testing.T) {
   105  	testValueAndVersionEncoding(t, []byte("value1"), version.NewHeight(1, 2))
   106  	testValueAndVersionEncoding(t, []byte{}, version.NewHeight(50, 50))
   107  }
   108  
   109  func testValueAndVersionEncoding(t *testing.T, value []byte, version *version.Height) {
   110  	encodedValue := statedb.EncodeValue(value, version)
   111  	val, ver := statedb.DecodeValue(encodedValue)
   112  	testutil.AssertEquals(t, val, value)
   113  	testutil.AssertEquals(t, ver, version)
   114  }
   115  
   116  func TestCompositeKey(t *testing.T) {
   117  	if ledgerconfig.IsCouchDBEnabled() == true {
   118  
   119  		testCompositeKey(t, "ns", "key")
   120  		testCompositeKey(t, "ns", "")
   121  
   122  	}
   123  }
   124  
   125  func testCompositeKey(t *testing.T, ns string, key string) {
   126  	compositeKey := constructCompositeKey(ns, key)
   127  	t.Logf("compositeKey=%#v", compositeKey)
   128  	ns1, key1 := splitCompositeKey(compositeKey)
   129  	testutil.AssertEquals(t, ns1, ns)
   130  	testutil.AssertEquals(t, key1, key)
   131  }
   132  
   133  // The following tests are unique to couchdb, they are not used in leveldb
   134  //  query test
   135  func TestQuery(t *testing.T) {
   136  	if ledgerconfig.IsCouchDBEnabled() == true {
   137  
   138  		env := NewTestVDBEnv(t)
   139  		env.Cleanup("testquery")
   140  		defer env.Cleanup("testquery")
   141  		commontests.TestQuery(t, env.DBProvider)
   142  
   143  	}
   144  }
   145  
   146  func TestGetStateMultipleKeys(t *testing.T) {
   147  	if ledgerconfig.IsCouchDBEnabled() == true {
   148  		env := NewTestVDBEnv(t)
   149  		env.Cleanup("testgetmultiplekeys")
   150  		defer env.Cleanup("testgetmultiplekeys")
   151  		commontests.TestGetStateMultipleKeys(t, env.DBProvider)
   152  	}
   153  }