Skip to content

Commit 35f32a7

Browse files
committedMar 5, 2018
[FAB-8658] peer mock functions to separate file
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>
1 parent 368bfc2 commit 35f32a7

File tree

3 files changed

+56
-45
lines changed

3 files changed

+56
-45
lines changed
 

‎core/peer/mock_helpers.go

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package peer
8+
9+
import (
10+
configtxtest "github.com/hyperledger/fabric/common/configtx/test"
11+
mockchannelconfig "github.com/hyperledger/fabric/common/mocks/config"
12+
mockconfigtx "github.com/hyperledger/fabric/common/mocks/configtx"
13+
mockpolicies "github.com/hyperledger/fabric/common/mocks/policies"
14+
"github.com/hyperledger/fabric/core/ledger"
15+
"github.com/hyperledger/fabric/core/ledger/ledgermgmt"
16+
)
17+
18+
//MockInitialize resets chains for test env
19+
func MockInitialize() {
20+
ledgermgmt.InitializeTestEnvWithCustomProcessors(ConfigTxProcessors)
21+
chains.list = nil
22+
chains.list = make(map[string]*chain)
23+
chainInitializer = func(string) { return }
24+
}
25+
26+
// MockCreateChain used for creating a ledger for a chain for tests
27+
// without having to join
28+
func MockCreateChain(cid string) error {
29+
var ledger ledger.PeerLedger
30+
var err error
31+
32+
if ledger = GetLedger(cid); ledger == nil {
33+
gb, _ := configtxtest.MakeGenesisBlock(cid)
34+
if ledger, err = ledgermgmt.CreateLedger(gb); err != nil {
35+
return err
36+
}
37+
}
38+
39+
chains.Lock()
40+
defer chains.Unlock()
41+
42+
chains.list[cid] = &chain{
43+
cs: &chainSupport{
44+
Resources: &mockchannelconfig.Resources{
45+
PolicyManagerVal: &mockpolicies.Manager{
46+
Policy: &mockpolicies.Policy{},
47+
},
48+
ConfigtxValidatorVal: &mockconfigtx.Validator{},
49+
},
50+
ledger: ledger,
51+
},
52+
}
53+
54+
return nil
55+
}

‎core/peer/peer.go

+1-43
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,11 @@ import (
1515
"github.com/hyperledger/fabric/common/channelconfig"
1616
cc "github.com/hyperledger/fabric/common/config"
1717
"github.com/hyperledger/fabric/common/configtx"
18-
configtxtest "github.com/hyperledger/fabric/common/configtx/test"
1918
"github.com/hyperledger/fabric/common/deliver"
2019
"github.com/hyperledger/fabric/common/flogging"
2120
commonledger "github.com/hyperledger/fabric/common/ledger"
2221
"github.com/hyperledger/fabric/common/ledger/blockledger"
23-
"github.com/hyperledger/fabric/common/ledger/blockledger/file"
24-
mockchannelconfig "github.com/hyperledger/fabric/common/mocks/config"
25-
mockconfigtx "github.com/hyperledger/fabric/common/mocks/configtx"
26-
mockpolicies "github.com/hyperledger/fabric/common/mocks/policies"
22+
fileledger "github.com/hyperledger/fabric/common/ledger/blockledger/file"
2723
"github.com/hyperledger/fabric/common/policies"
2824
"github.com/hyperledger/fabric/common/resourcesconfig"
2925
"github.com/hyperledger/fabric/core/comm"
@@ -169,14 +165,6 @@ var chains = struct {
169165
list map[string]*chain
170166
}{list: make(map[string]*chain)}
171167

172-
//MockInitialize resets chains for test env
173-
func MockInitialize() {
174-
ledgermgmt.InitializeTestEnvWithCustomProcessors(ConfigTxProcessors)
175-
chains.list = nil
176-
chains.list = make(map[string]*chain)
177-
chainInitializer = func(string) { return }
178-
}
179-
180168
var chainInitializer func(string)
181169

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

439-
// MockCreateChain used for creating a ledger for a chain for tests
440-
// without having to join
441-
func MockCreateChain(cid string) error {
442-
var ledger ledger.PeerLedger
443-
var err error
444-
445-
if ledger = GetLedger(cid); ledger == nil {
446-
gb, _ := configtxtest.MakeGenesisBlock(cid)
447-
if ledger, err = ledgermgmt.CreateLedger(gb); err != nil {
448-
return err
449-
}
450-
}
451-
452-
chains.Lock()
453-
defer chains.Unlock()
454-
455-
chains.list[cid] = &chain{
456-
cs: &chainSupport{
457-
Resources: &mockchannelconfig.Resources{
458-
PolicyManagerVal: &mockpolicies.Manager{
459-
Policy: &mockpolicies.Policy{},
460-
},
461-
ConfigtxValidatorVal: &mockconfigtx.Validator{},
462-
},
463-
ledger: ledger},
464-
}
465-
466-
return nil
467-
}
468-
469427
// GetLedger returns the ledger of the chain with chain ID. Note that this
470428
// call returns nil if chain cid has not been created.
471429
func GetLedger(cid string) ledger.PeerLedger {

‎core/peer/pkg_test.go

-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ func (tss *testServiceServer) EmptyCall(context.Context, *testpb.Empty) (*testpb
4646

4747
// createCertPool creates an x509.CertPool from an array of PEM-encoded certificates
4848
func createCertPool(rootCAs [][]byte) (*x509.CertPool, error) {
49-
5049
certPool := x509.NewCertPool()
5150
for _, rootCA := range rootCAs {
5251
if !certPool.AppendCertsFromPEM(rootCA) {
@@ -58,7 +57,6 @@ func createCertPool(rootCAs [][]byte) (*x509.CertPool, error) {
5857

5958
// helper function to invoke the EmptyCall againt the test service
6059
func invokeEmptyCall(address string, dialOptions []grpc.DialOption) (*testpb.Empty, error) {
61-
6260
//add DialOptions
6361
dialOptions = append(dialOptions, grpc.WithBlock())
6462
ctx := context.Background()

0 commit comments

Comments
 (0)
Please sign in to comment.