Skip to content

Commit

Permalink
[FAB-5193] Mock servers should accept port & cleanup
Browse files Browse the repository at this point in the history
Change-Id: I67621307f78ca8007282c47ecd0f40d430c90630
Signed-off-by: Troy Ronda <troy@troyronda.com>
  • Loading branch information
troyronda committed Jul 6, 2017
1 parent 358abfb commit f71ee06
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 56 deletions.
2 changes: 1 addition & 1 deletion pkg/fabric-client/channel/channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
mocks "github.com/hyperledger/fabric-sdk-go/pkg/fabric-client/mocks"
)

var testAddress = "0.0.0.0:5244"
var testAddress = "0.0.0.0:0"

var validRootCA = `-----BEGIN CERTIFICATE-----
MIICYjCCAgmgAwIBAgIUB3CTDOU47sUC5K4kn/Caqnh114YwCgYIKoZIzj0EAwIw
Expand Down
17 changes: 11 additions & 6 deletions pkg/fabric-client/channel/txnproposer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,13 @@ func TestCreateTransactionProposal(t *testing.T) {

func TestJoinChannel(t *testing.T) {
var peers []fab.Peer
endorserServer := startEndorserServer(t)

grpcServer := grpc.NewServer()
defer grpcServer.Stop()

endorserServer, addr := startEndorserServer(t, grpcServer)
channel, _ := setupTestChannel()
peer, _ := peer.NewPeer(testAddress, mocks.NewMockConfig())
peer, _ := peer.NewPeer(addr, mocks.NewMockConfig())
peers = append(peers, peer)
orderer := mocks.NewMockOrderer("", nil)
orderer.(mocks.MockOrderer).EnqueueForSendDeliver(mocks.NewSimpleMockBlock())
Expand Down Expand Up @@ -244,16 +248,17 @@ func TestConcurrentPeers(t *testing.T) {

}

func startEndorserServer(t *testing.T) *mocks.MockEndorserServer {
grpcServer := grpc.NewServer()
func startEndorserServer(t *testing.T, grpcServer *grpc.Server) (*mocks.MockEndorserServer, string) {
lis, err := net.Listen("tcp", testAddress)
addr := lis.Addr().String()

endorserServer := &mocks.MockEndorserServer{}
pb.RegisterEndorserServer(grpcServer, endorserServer)
if err != nil {
fmt.Printf("Error starting test server %s", err)
t.FailNow()
}
fmt.Printf("Starting test server\n")
fmt.Printf("Starting test server on %s\n", addr)
go grpcServer.Serve(lis)
return endorserServer
return endorserServer, addr
}
82 changes: 44 additions & 38 deletions pkg/fabric-client/orderer/orderer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,7 @@ import (
"google.golang.org/grpc"
)

var testOrdererURL = "0.0.0.0:4584"
var testOrdererURL2 = "0.0.0.0:4585"
var testOrdererURL3 = "0.0.0.0:4586"
var testOrdererURL4 = "0.0.0.0:4587"
var testOrdererURL5 = "0.0.0.0:4588"
var testOrdererURL6 = "0.0.0.0:4590"
var testOrdererURL = "0.0.0.0:0"

var validRootCA = `-----BEGIN CERTIFICATE-----
MIICYjCCAgmgAwIBAgIUB3CTDOU47sUC5K4kn/Caqnh114YwCgYIKoZIzj0EAwIw
Expand Down Expand Up @@ -141,8 +136,11 @@ func TestOrdererViaChainNilData(t *testing.T) {
}

func TestSendDeliver(t *testing.T) {
mockServer := startMockServer(t)
orderer, _ := NewOrderer(testOrdererURL, "", "", mocks.NewMockConfig())
grpcServer := grpc.NewServer()
defer grpcServer.Stop()
mockServer, addr := startMockServer(t, grpcServer)

orderer, _ := NewOrderer(addr, "", "", mocks.NewMockConfig())
// Test deliver happy path
blocks, errors := orderer.SendDeliver(&fab.SignedEnvelope{})
select {
Expand Down Expand Up @@ -198,33 +196,35 @@ func TestSendDeliver(t *testing.T) {

}

func startMockServer(t *testing.T) *mocks.MockBroadcastServer {
grpcServer := grpc.NewServer()
func startMockServer(t *testing.T, grpcServer *grpc.Server) (*mocks.MockBroadcastServer, string) {
lis, err := net.Listen("tcp", testOrdererURL)
addr := lis.Addr().String()

broadcastServer := new(mocks.MockBroadcastServer)
ab.RegisterAtomicBroadcastServer(grpcServer, broadcastServer)
if err != nil {
fmt.Printf("Error starting test server %s", err)
t.FailNow()
}
fmt.Printf("Starting test server\n")
fmt.Printf("Starting test server on %s\n", addr)
go grpcServer.Serve(lis)

return broadcastServer
return broadcastServer, addr
}

func startCustomizedMockServer(t *testing.T, serverURL string, grpcServer *grpc.Server, broadcastServer *mocks.MockBroadcastServer) *mocks.MockBroadcastServer {

func startCustomizedMockServer(t *testing.T, serverURL string, grpcServer *grpc.Server, broadcastServer *mocks.MockBroadcastServer) string {
lis, err := net.Listen("tcp", serverURL)
addr := lis.Addr().String()

ab.RegisterAtomicBroadcastServer(grpcServer, broadcastServer)
if err != nil {
fmt.Printf("Error starting test server %s", err)
t.FailNow()
}
fmt.Printf("Starting test customized server\n")
fmt.Printf("Starting test customized server on %s\n", addr)
go grpcServer.Serve(lis)

return broadcastServer
return addr
}

func TestCreateNewOrdererWithRootCAs(t *testing.T) {
Expand Down Expand Up @@ -269,10 +269,11 @@ func TestNewOrdererWithTLS(t *testing.T) {
}

func TestSendBroadcast(t *testing.T) {
grpcServer := grpc.NewServer()
defer grpcServer.Stop()
_, addr := startMockServer(t, grpcServer)

//startMockServer(t)

orderer, _ := NewOrderer(testOrdererURL, "", "", mocks.NewMockConfig())
orderer, _ := NewOrderer(addr, "", "", mocks.NewMockConfig())
_, err := orderer.SendBroadcast(&fab.SignedEnvelope{})

if err != nil {
Expand All @@ -290,7 +291,6 @@ func TestSendBroadcast(t *testing.T) {

func TestSendDeliverServerBadResponse(t *testing.T) {

grpcServer := grpc.NewServer()
broadcastServer := mocks.MockBroadcastServer{
DeliverResponse: &ab.DeliverResponse{
Type: &ab.DeliverResponse_Status{
Expand All @@ -299,8 +299,10 @@ func TestSendDeliverServerBadResponse(t *testing.T) {
},
}

startCustomizedMockServer(t, testOrdererURL2, grpcServer, &broadcastServer)
orderer, _ := NewOrderer(testOrdererURL2, "", "", mocks.NewMockConfig())
grpcServer := grpc.NewServer()
defer grpcServer.Stop()
addr := startCustomizedMockServer(t, testOrdererURL, grpcServer, &broadcastServer)
orderer, _ := NewOrderer(addr, "", "", mocks.NewMockConfig())

blocks, errors := orderer.SendDeliver(&fab.SignedEnvelope{})

Expand All @@ -318,7 +320,6 @@ func TestSendDeliverServerBadResponse(t *testing.T) {

func TestSendDeliverServerSuccessResponse(t *testing.T) {

grpcServer := grpc.NewServer()
broadcastServer := mocks.MockBroadcastServer{
DeliverResponse: &ab.DeliverResponse{
Type: &ab.DeliverResponse_Status{
Expand All @@ -327,8 +328,11 @@ func TestSendDeliverServerSuccessResponse(t *testing.T) {
},
}

startCustomizedMockServer(t, testOrdererURL3, grpcServer, &broadcastServer)
orderer, _ := NewOrderer(testOrdererURL3, "", "", mocks.NewMockConfig())
grpcServer := grpc.NewServer()
defer grpcServer.Stop()
addr := startCustomizedMockServer(t, testOrdererURL, grpcServer, &broadcastServer)

orderer, _ := NewOrderer(addr, "", "", mocks.NewMockConfig())

blocks, errors := orderer.SendDeliver(&fab.SignedEnvelope{})

Expand All @@ -346,13 +350,14 @@ func TestSendDeliverServerSuccessResponse(t *testing.T) {

func TestSendDeliverFailure(t *testing.T) {

grpcServer := grpc.NewServer()
broadcastServer := mocks.MockBroadcastServer{
DeliverResponse: &ab.DeliverResponse{},
}

startCustomizedMockServer(t, testOrdererURL6, grpcServer, &broadcastServer)
orderer, _ := NewOrderer(testOrdererURL6, "", "", mocks.NewMockConfig())
grpcServer := grpc.NewServer()
defer grpcServer.Stop()
addr := startCustomizedMockServer(t, testOrdererURL, grpcServer, &broadcastServer)
orderer, _ := NewOrderer(addr, "", "", mocks.NewMockConfig())

blocks, errors := orderer.SendDeliver(&fab.SignedEnvelope{})

Expand All @@ -370,34 +375,35 @@ func TestSendDeliverFailure(t *testing.T) {

func TestSendBroadcastServerBadResponse(t *testing.T) {

grpcServer := grpc.NewServer()
broadcastServer := mocks.MockBroadcastServer{
BroadcastInternalServerError: true,
}

startCustomizedMockServer(t, testOrdererURL4, grpcServer, &broadcastServer)
orderer, _ := NewOrderer(testOrdererURL4, "", "", mocks.NewMockConfig())
grpcServer := grpc.NewServer()
defer grpcServer.Stop()
addr := startCustomizedMockServer(t, testOrdererURL, grpcServer, &broadcastServer)
orderer, _ := NewOrderer(addr, "", "", mocks.NewMockConfig())

status, err := orderer.SendBroadcast(&fab.SignedEnvelope{})

if status.String() != "INTERNAL_SERVER_ERROR" {
t.Fatalf("Expected internal server error, but got %v", status)
}
if err == nil || err.Error() != "broadcast response is not success : INTERNAL_SERVER_ERROR" {
t.Fatalf("Expected internal server error, but got %s", err)
}

if status.String() != "INTERNAL_SERVER_ERROR" {
t.Fatalf("Expected internal server error, but got %v", status)
}
}

func TestSendBroadcastError(t *testing.T) {

grpcServer := grpc.NewServer()
broadcastServer := mocks.MockBroadcastServer{
BroadcastError: fmt.Errorf("just to test error scenario"),
}

startCustomizedMockServer(t, testOrdererURL5, grpcServer, &broadcastServer)
orderer, _ := NewOrderer(testOrdererURL5, "", "", mocks.NewMockConfig())
grpcServer := grpc.NewServer()
defer grpcServer.Stop()
addr := startCustomizedMockServer(t, testOrdererURL, grpcServer, &broadcastServer)
orderer, _ := NewOrderer(addr, "", "", mocks.NewMockConfig())

status, err := orderer.SendBroadcast(&fab.SignedEnvelope{})

Expand Down
23 changes: 12 additions & 11 deletions pkg/fabric-client/peer/peerendorser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const (
peer1URL = "localhost:7050"
peer2URL = "localhost:7054"
peerURLBad = "localhost:9999"
testAddress = "0.0.0.0:5244"
testAddress = "0.0.0.0:0"
)

// TestNewPeerEndorserTLS validates that a client configured with TLS
Expand Down Expand Up @@ -196,7 +196,7 @@ func TestNewPeerEndorserTLSBad(t *testing.T) {
// TestProcessProposalBadDial validates that a down
// endorser fails gracefully.
func TestProcessProposalBadDial(t *testing.T) {
_, err := testProcessProposal(t, time.Millisecond*10)
_, err := testProcessProposal(t, testAddress, time.Millisecond*10)
if err == nil {
t.Fatalf("Process proposal should have failed")
}
Expand All @@ -205,20 +205,21 @@ func TestProcessProposalBadDial(t *testing.T) {
// TestProcessProposalGoodDial validates that an up
// endorser connects.
func TestProcessProposalGoodDial(t *testing.T) {
startEndorserServer(t)
grpcServer := grpc.NewServer()
defer grpcServer.Stop()
_, addr := startEndorserServer(t, grpcServer)

_, err := testProcessProposal(t, connTimeout)
_, err := testProcessProposal(t, addr, connTimeout)
if err != nil {
t.Fatalf("Process proposal failed (%v)", err)
}
}

func testProcessProposal(t *testing.T, to time.Duration) (apitxn.TransactionProposalResult, error) {
func testProcessProposal(t *testing.T, url string, to time.Duration) (apitxn.TransactionProposalResult, error) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
config := mock_apiconfig.NewMockConfig(mockCtrl)

url := testAddress
config.EXPECT().IsTLSEnabled().Return(false)

conn, err := newPeerEndorser(url, "", "", to, true, config)
Expand All @@ -235,19 +236,19 @@ func mockTransactionProposal() apitxn.TransactionProposal {
}
}

// TODO: this function is duplicated.
func startEndorserServer(t *testing.T) *mocks.MockEndorserServer {
grpcServer := grpc.NewServer()
func startEndorserServer(t *testing.T, grpcServer *grpc.Server) (*mocks.MockEndorserServer, string) {
lis, err := net.Listen("tcp", testAddress)
addr := lis.Addr().String()

endorserServer := &mocks.MockEndorserServer{}
pb.RegisterEndorserServer(grpcServer, endorserServer)
if err != nil {
fmt.Printf("Error starting test server %s", err)
t.FailNow()
}
fmt.Printf("Starting test server\n")
fmt.Printf("Starting test server on %s\n", addr)
go grpcServer.Serve(lis)
return endorserServer
return endorserServer, addr
}

func TestTransactionProposalError(t *testing.T) {
Expand Down

0 comments on commit f71ee06

Please sign in to comment.