Skip to content

Commit

Permalink
[FAB-3352] No need to initialize grpc in CSCC tests
Browse files Browse the repository at this point in the history
CSCC configure_test.go leverages mocking framewrk to test CSCC
invokations, while each test initializes grpc server while not real need
for this. Current commit takes care to make tests stop initializing grpc
server and clean up the test from non relevant code.

Change-Id: Iec29046f644603c8412a8ea0da648f2d448e5d2f
Signed-off-by: Artem Barger <[email protected]>
  • Loading branch information
C0rWin committed Apr 23, 2017
1 parent d7af6e8 commit 00eb7c9
Showing 1 changed file with 10 additions and 35 deletions.
45 changes: 10 additions & 35 deletions core/scc/cscc/configure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@ package cscc

import (
"fmt"
"net"
"os"
"strings"
"testing"
"time"

"strings"

"github.com/golang/protobuf/proto"
configtxtest "github.com/hyperledger/fabric/common/configtx/test"
"github.com/hyperledger/fabric/common/localmsp"
Expand All @@ -45,7 +43,6 @@ import (
"github.com/hyperledger/fabric/protos/utils"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc"
)

type mockDeliveryClient struct {
Expand Down Expand Up @@ -91,22 +88,6 @@ func TestConfigerInit(t *testing.T) {
}
}

func setupEndpoint(t *testing.T) {
peerAddress := peer.GetLocalIP()
if peerAddress == "" {
peerAddress = "0.0.0.0"
}
peerAddress = peerAddress + ":21213"
t.Logf("Local peer IP address: %s", peerAddress)
var opts []grpc.ServerOption
grpcServer := grpc.NewServer(opts...)
getPeerEndpoint := func() (*pb.PeerEndpoint, error) {
return &pb.PeerEndpoint{Id: &pb.PeerID{Name: "cscctestpeer"}, Address: peerAddress}, nil
}
ccStartupTimeout := time.Duration(30000) * time.Millisecond
pb.RegisterChaincodeSupportServer(grpcServer, chaincode.NewChaincodeSupport(getPeerEndpoint, false, ccStartupTimeout))
}

func TestConfigerInvokeJoinChainMissingParams(t *testing.T) {
viper.Set("peer.fileSystemPath", "/tmp/hyperledgertest/")
os.Mkdir("/tmp/hyperledgertest", 0755)
Expand All @@ -120,7 +101,6 @@ func TestConfigerInvokeJoinChainMissingParams(t *testing.T) {
t.FailNow()
}

setupEndpoint(t)
// Failed path: Not enough parameters
args := [][]byte{[]byte("JoinChain")}
if res := stub.MockInvoke("2", args); res.Status == shim.OK {
Expand All @@ -141,8 +121,6 @@ func TestConfigerInvokeJoinChainWrongParams(t *testing.T) {
t.FailNow()
}

setupEndpoint(t)

// Failed path: wrong parameter type
args := [][]byte{[]byte("JoinChain"), []byte("action")}
if res := stub.MockInvoke("2", args); res.Status == shim.OK {
Expand All @@ -152,6 +130,7 @@ func TestConfigerInvokeJoinChainWrongParams(t *testing.T) {

func TestConfigerInvokeJoinChainCorrectParams(t *testing.T) {
viper.Set("peer.fileSystemPath", "/tmp/hyperledgertest/")
viper.Set("chaincode.executetimeout", "3000")
os.Mkdir("/tmp/hyperledgertest", 0755)

peer.MockInitialize()
Expand All @@ -162,6 +141,13 @@ func TestConfigerInvokeJoinChainCorrectParams(t *testing.T) {
e := new(PeerConfiger)
stub := shim.NewMockStub("PeerConfiger", e)

peerEndpoint := "localhost:13611"
getPeerEndpoint := func() (*pb.PeerEndpoint, error) {
return &pb.PeerEndpoint{Id: &pb.PeerID{Name: "cscctestpeer"}, Address: peerEndpoint}, nil
}
ccStartupTimeout := time.Duration(30000) * time.Millisecond
chaincode.NewChaincodeSupport(getPeerEndpoint, false, ccStartupTimeout)

// Init the policy checker
policyManagerGetter := &policy.MockChannelPolicyManagerGetter{
Managers: map[string]policies.Manager{
Expand All @@ -177,18 +163,9 @@ func TestConfigerInvokeJoinChainCorrectParams(t *testing.T) {
&policy.MockMSPPrincipalGetter{Principal: []byte("Alice")},
)

setupEndpoint(t)

// Initialize gossip service
grpcServer := grpc.NewServer()
socket, err := net.Listen("tcp", fmt.Sprintf("%s:%d", "", 13611))
assert.NoError(t, err)
go grpcServer.Serve(socket)
defer grpcServer.Stop()

identity, _ := mgmt.GetLocalSigningIdentityOrPanic().Serialize()
messageCryptoService := mcs.New(&mcs.MockChannelPolicyManagerGetter{}, localmsp.NewSigner(), mgmt.NewDeserializersManager())
service.InitGossipServiceCustomDeliveryFactory(identity, "localhost:13611", grpcServer, &mockDeliveryClientFactory{}, messageCryptoService)
service.InitGossipServiceCustomDeliveryFactory(identity, peerEndpoint, nil, &mockDeliveryClientFactory{}, messageCryptoService)

// Successful path for JoinChain
blockBytes := mockConfigBlock()
Expand Down Expand Up @@ -262,8 +239,6 @@ func TestConfigerInvokeUpdateConfigBlock(t *testing.T) {
&policy.MockMSPPrincipalGetter{Principal: []byte("Alice")},
)

setupEndpoint(t)

sProp, _ := utils.MockSignedEndorserProposalOrPanic("", &pb.ChaincodeSpec{}, []byte("Alice"), []byte("msg1"))
identityDeserializer.Msg = sProp.ProposalBytes
sProp.Signature = sProp.ProposalBytes
Expand Down

0 comments on commit 00eb7c9

Please sign in to comment.