Skip to content

Commit

Permalink
[FAB-8658] peer mock functions to separate file
Browse files Browse the repository at this point in the history
Production and test should not be co-mingled in the same files.

The mock initialization functions are currently referenced by code
outside of the peer package and can't be moved to an _test.go file
right now without introducing compilation errors.

Change-Id: I179c21f9268d464b4a8d583066e5f122b2141dc4
Signed-off-by: Matthew Sykes <sykesmat@us.ibm.com>
  • Loading branch information
sykesm committed Mar 5, 2018
1 parent 368bfc2 commit 35f32a7
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 45 deletions.
55 changes: 55 additions & 0 deletions core/peer/mock_helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package peer

import (
configtxtest "github.com/hyperledger/fabric/common/configtx/test"
mockchannelconfig "github.com/hyperledger/fabric/common/mocks/config"
mockconfigtx "github.com/hyperledger/fabric/common/mocks/configtx"
mockpolicies "github.com/hyperledger/fabric/common/mocks/policies"
"github.com/hyperledger/fabric/core/ledger"
"github.com/hyperledger/fabric/core/ledger/ledgermgmt"
)

//MockInitialize resets chains for test env
func MockInitialize() {
ledgermgmt.InitializeTestEnvWithCustomProcessors(ConfigTxProcessors)
chains.list = nil
chains.list = make(map[string]*chain)
chainInitializer = func(string) { return }
}

// MockCreateChain used for creating a ledger for a chain for tests
// without having to join
func MockCreateChain(cid string) error {
var ledger ledger.PeerLedger
var err error

if ledger = GetLedger(cid); ledger == nil {
gb, _ := configtxtest.MakeGenesisBlock(cid)
if ledger, err = ledgermgmt.CreateLedger(gb); err != nil {
return err
}
}

chains.Lock()
defer chains.Unlock()

chains.list[cid] = &chain{
cs: &chainSupport{
Resources: &mockchannelconfig.Resources{
PolicyManagerVal: &mockpolicies.Manager{
Policy: &mockpolicies.Policy{},
},
ConfigtxValidatorVal: &mockconfigtx.Validator{},
},
ledger: ledger,
},
}

return nil
}
44 changes: 1 addition & 43 deletions core/peer/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,11 @@ import (
"github.com/hyperledger/fabric/common/channelconfig"
cc "github.com/hyperledger/fabric/common/config"
"github.com/hyperledger/fabric/common/configtx"
configtxtest "github.com/hyperledger/fabric/common/configtx/test"
"github.com/hyperledger/fabric/common/deliver"
"github.com/hyperledger/fabric/common/flogging"
commonledger "github.com/hyperledger/fabric/common/ledger"
"github.com/hyperledger/fabric/common/ledger/blockledger"
"github.com/hyperledger/fabric/common/ledger/blockledger/file"
mockchannelconfig "github.com/hyperledger/fabric/common/mocks/config"
mockconfigtx "github.com/hyperledger/fabric/common/mocks/configtx"
mockpolicies "github.com/hyperledger/fabric/common/mocks/policies"
fileledger "github.com/hyperledger/fabric/common/ledger/blockledger/file"
"github.com/hyperledger/fabric/common/policies"
"github.com/hyperledger/fabric/common/resourcesconfig"
"github.com/hyperledger/fabric/core/comm"
Expand Down Expand Up @@ -169,14 +165,6 @@ var chains = struct {
list map[string]*chain
}{list: make(map[string]*chain)}

//MockInitialize resets chains for test env
func MockInitialize() {
ledgermgmt.InitializeTestEnvWithCustomProcessors(ConfigTxProcessors)
chains.list = nil
chains.list = make(map[string]*chain)
chainInitializer = func(string) { return }
}

var chainInitializer func(string)

var mockMSPIDGetter func(string) []string
Expand Down Expand Up @@ -436,36 +424,6 @@ func CreateChainFromBlock(cb *common.Block) error {
return createChain(cid, l, cb)
}

// MockCreateChain used for creating a ledger for a chain for tests
// without having to join
func MockCreateChain(cid string) error {
var ledger ledger.PeerLedger
var err error

if ledger = GetLedger(cid); ledger == nil {
gb, _ := configtxtest.MakeGenesisBlock(cid)
if ledger, err = ledgermgmt.CreateLedger(gb); err != nil {
return err
}
}

chains.Lock()
defer chains.Unlock()

chains.list[cid] = &chain{
cs: &chainSupport{
Resources: &mockchannelconfig.Resources{
PolicyManagerVal: &mockpolicies.Manager{
Policy: &mockpolicies.Policy{},
},
ConfigtxValidatorVal: &mockconfigtx.Validator{},
},
ledger: ledger},
}

return nil
}

// GetLedger returns the ledger of the chain with chain ID. Note that this
// call returns nil if chain cid has not been created.
func GetLedger(cid string) ledger.PeerLedger {
Expand Down
2 changes: 0 additions & 2 deletions core/peer/pkg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ func (tss *testServiceServer) EmptyCall(context.Context, *testpb.Empty) (*testpb

// createCertPool creates an x509.CertPool from an array of PEM-encoded certificates
func createCertPool(rootCAs [][]byte) (*x509.CertPool, error) {

certPool := x509.NewCertPool()
for _, rootCA := range rootCAs {
if !certPool.AppendCertsFromPEM(rootCA) {
Expand All @@ -58,7 +57,6 @@ func createCertPool(rootCAs [][]byte) (*x509.CertPool, error) {

// helper function to invoke the EmptyCall againt the test service
func invokeEmptyCall(address string, dialOptions []grpc.DialOption) (*testpb.Empty, error) {

//add DialOptions
dialOptions = append(dialOptions, grpc.WithBlock())
ctx := context.Background()
Expand Down

0 comments on commit 35f32a7

Please sign in to comment.