Skip to content

Commit

Permalink
[FAB-8902] Use random port for unit tests
Browse files Browse the repository at this point in the history
This change adjusts most unit tests to use random ports
when creating mock servers.

Change-Id: I20db69c91c5a5f8860a2555402bef1de6ae74044
Signed-off-by: Troy Ronda <[email protected]>
  • Loading branch information
troyronda committed Mar 15, 2018
1 parent 212c733 commit b15f6d3
Show file tree
Hide file tree
Showing 15 changed files with 122 additions and 54 deletions.
3 changes: 1 addition & 2 deletions pkg/client/channel/chclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ import (
)

const (
testAddress = "127.0.0.1:47882"
channelID = "testChannel"
channelID = "testChannel"
)

func TestTxProposalResponseFilter(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ const (
org10 = "Org10MSP"
)

var p1 = peer("peer1", "peer1:7051")
var p2 = peer("peer2", "peer2:7051")
var p3 = peer("peer3", "peer3:7051")
var p4 = peer("peer4", "peer4:7051")
var p5 = peer("peer5", "peer5:7051")
var p6 = peer("peer6", "peer6:7051")
var p7 = peer("peer7", "peer7:7051")
var p8 = peer("peer8", "peer8:7051")
var p9 = peer("peer9", "peer9:7051")
var p10 = peer("peer10", "peer10:7051")
var p11 = peer("peer11", "peer11:7051")
var p12 = peer("peer12", "peer12:7051")
var p1 = peer("peer1", "peer1:9999")
var p2 = peer("peer2", "peer2:9999")
var p3 = peer("peer3", "peer3:9999")
var p4 = peer("peer4", "peer4:9999")
var p5 = peer("peer5", "peer5:9999")
var p6 = peer("peer6", "peer6:9999")
var p7 = peer("peer7", "peer7:9999")
var p8 = peer("peer8", "peer8:9999")
var p9 = peer("peer9", "peer9:9999")
var p10 = peer("peer10", "peer10:9999")
var p11 = peer("peer11", "peer11:9999")
var p12 = peer("peer12", "peer12:9999")

var peersByMSPID = map[string][]fab.Peer{
org1: peers(p1, p2),
Expand Down
45 changes: 37 additions & 8 deletions pkg/client/msp/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ SPDX-License-Identifier: Apache-2.0
package msp

import (
"io/ioutil"
"math/rand"
"net"
"strconv"
"strings"
"testing"
Expand All @@ -22,10 +24,12 @@ import (
)

const (
caServerURL = "http://localhost:8090"
configPath = "testdata/config_test.yaml"
caServerURLListen = "http://localhost:0"
configPath = "testdata/config_test.yaml"
)

var caServerURL string

// TestMSP is a unit test for Client enrollment and re-enrollment scenarios
func TestMSP(t *testing.T) {

Expand Down Expand Up @@ -137,18 +141,30 @@ var caServer = &mocks.MockFabricCAServer{}

func (f *textFixture) setup() *fabsdk.FabricSDK {

configProvider := config.FromFile(configPath)
var lis net.Listener
var err error
if !caServer.Running() {
lis, err = net.Listen("tcp", strings.TrimPrefix(caServerURLListen, "http://"))
if err != nil {
panic(fmt.Sprintf("Error starting CA Server %s", err))
}

caServerURL = "http://" + lis.Addr().String()
}

cfgRaw := readConfigWithReplacement(configPath, "http://localhost:8050", caServerURL)
configProvider := config.FromRaw(cfgRaw, "yaml")
if err != nil {
panic(fmt.Sprintf("Failed to read config: %v", err))
}

// Instantiate the SDK
sdk, err := fabsdk.New(configProvider)
if err != nil {
panic(fmt.Sprintf("SDK init failed: %v", err))
}

f.config, err = config.FromFile(configPath)()
if err != nil {
panic(fmt.Sprintf("Failed to read config: %v", err))
}
f.config = sdk.Config()

// Delete all private keys from the crypto suite store
// and users from the user store
Expand All @@ -160,8 +176,11 @@ func (f *textFixture) setup() *fabsdk.FabricSDK {
if err != nil {
panic(fmt.Sprintf("Failed to init context: %v", err))
}

// Start Http Server if it's not running
caServer.Start(strings.TrimPrefix(caServerURL, "http://"), ctx.CryptoSuite())
if !caServer.Running() {
caServer.Start(lis, ctx.CryptoSuite())
}

return sdk
}
Expand All @@ -171,6 +190,16 @@ func (f *textFixture) close() {
cleanup(f.config.KeyStorePath())
}

func readConfigWithReplacement(path string, origURL, newURL string) []byte {
cfgRaw, err := ioutil.ReadFile(path)
if err != nil {
panic(fmt.Sprintf("Failed to read config [%s]", err))
}

updatedCfg := strings.Replace(string(cfgRaw), origURL, newURL, -1)
return []byte(updatedCfg)
}

func cleanup(storePath string) {
err := os.RemoveAll(storePath)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/client/msp/testdata/config_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ peers:
#
certificateAuthorities:
ca.org1.example.com:
url: "http://localhost:8090"
url: "http://localhost:8050"
# the properties specified under this object are passed to the 'http' client verbatim when
# making the request to the Fabric-CA server
httpOptions:
Expand All @@ -328,7 +328,7 @@ certificateAuthorities:
# [Optional] The optional name of the CA.
caName: ca.org1.example.com
ca.org2.example.com:
url: "http://localhost:8090"
url: "http://localhost:8050"
# the properties specified under this object are passed to the 'http' client verbatim when
# making the request to the Fabric-CA server
httpOptions:
Expand Down
14 changes: 7 additions & 7 deletions pkg/client/resmgmt/resmgmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1289,7 +1289,7 @@ func setupTestContextWithDiscoveryError(username string, mspID string, discErr e
}

func startEndorserServer(t *testing.T, grpcServer *grpc.Server) (*fcmocks.MockEndorserServer, string) {
lis, err := net.Listen("tcp", "127.0.0.1:7051")
lis, err := net.Listen("tcp", "127.0.0.1:0")
addr := lis.Addr().String()

endorserServer := &fcmocks.MockEndorserServer{}
Expand All @@ -1316,7 +1316,7 @@ func TestSaveChannelSuccess(t *testing.T) {

grpcServer := grpc.NewServer()
defer grpcServer.Stop()
fcmocks.StartMockBroadcastServer("127.0.0.1:7050", grpcServer)
_, addr := fcmocks.StartMockBroadcastServer("127.0.0.1:0", grpcServer)

ctx := setupTestContext("test", "Org1MSP")

Expand All @@ -1325,7 +1325,7 @@ func TestSaveChannelSuccess(t *testing.T) {
grpcOpts["allow-insecure"] = true

oConfig := &core.OrdererConfig{
URL: "127.0.0.1:7050",
URL: addr,
GRPCOptions: grpcOpts,
}
mockConfig.SetCustomOrdererCfg(oConfig)
Expand Down Expand Up @@ -1423,7 +1423,7 @@ func TestSaveChannelWithOpts(t *testing.T) {

grpcServer := grpc.NewServer()
defer grpcServer.Stop()
fcmocks.StartMockBroadcastServer("127.0.0.1:7050", grpcServer)
_, addr := fcmocks.StartMockBroadcastServer("127.0.0.1:0", grpcServer)

ctx := setupTestContext("test", "Org1MSP")

Expand All @@ -1432,7 +1432,7 @@ func TestSaveChannelWithOpts(t *testing.T) {
grpcOpts["allow-insecure"] = true

oConfig := &core.OrdererConfig{
URL: "127.0.0.1:7050",
URL: addr,
GRPCOptions: grpcOpts,
}
mockConfig.SetCustomOrdererCfg(oConfig)
Expand Down Expand Up @@ -1501,15 +1501,15 @@ func TestJoinChannelWithInvalidOpts(t *testing.T) {
func TestSaveChannelWithMultipleSigningIdenities(t *testing.T) {
grpcServer := grpc.NewServer()
defer grpcServer.Stop()
fcmocks.StartMockBroadcastServer("127.0.0.1:7050", grpcServer)
_, addr := fcmocks.StartMockBroadcastServer("127.0.0.1:0", grpcServer)
ctx := setupTestContext("test", "Org1MSP")

mockConfig := &fcmocks.MockConfig{}
grpcOpts := make(map[string]interface{})
grpcOpts["allow-insecure"] = true

oConfig := &core.OrdererConfig{
URL: "127.0.0.1:7050",
URL: addr,
GRPCOptions: grpcOpts,
}
mockConfig.SetCustomRandomOrdererCfg(oConfig)
Expand Down
6 changes: 3 additions & 3 deletions pkg/fab/channel/ledger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func TestQueryConfig(t *testing.T) {
"Org1MSP",
"Org2MSP",
},
OrdererAddress: "localhost:7054",
OrdererAddress: "localhost:9999",
RootCA: validRootCA,
},
Index: 0,
Expand Down Expand Up @@ -192,7 +192,7 @@ func TestQueryConfig(t *testing.T) {
MSPNames: []string{
"Org1MSP",
},
OrdererAddress: "builder2:7054",
OrdererAddress: "builder2:9999",
RootCA: validRootCA,
},
Index: 0,
Expand Down Expand Up @@ -222,7 +222,7 @@ func TestQueryConfigBlockDifferentMetadata(t *testing.T) {
"Org1MSP",
"Org2MSP",
},
OrdererAddress: "localhost:7054",
OrdererAddress: "localhost:9999",
RootCA: validRootCA,
},
Index: 0,
Expand Down
2 changes: 1 addition & 1 deletion pkg/fab/chconfig/chconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func TestChannelConfigWithPeerError(t *testing.T) {
func TestChannelConfigWithOrdererError(t *testing.T) {

ctx := setupTestContext()
o, err := orderer.New(ctx.Config(), orderer.WithURL("localhost:7054"))
o, err := orderer.New(ctx.Config(), orderer.WithURL("localhost:9999"))
assert.Nil(t, err)
channelConfig, err := New(channelID, WithOrderer(o))
if err != nil {
Expand Down
11 changes: 8 additions & 3 deletions pkg/fab/events/eventhubclient/connection/connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ import (
)

const (
eventAddress = "localhost:7053"
eventURL = "grpc://" + eventAddress
eventAddressListen = "localhost:0"
)

var eventAddress string
var eventURL string

func TestInvalidConnectionOpts(t *testing.T) {
if _, err := New(newMockContext(), fabmocks.NewMockChannelCfg("channelid"), "grpcs://invalidhost:7053"); err == nil {
t.Fatalf("expecting error creating new connection with invaid address but got none")
Expand Down Expand Up @@ -177,11 +179,14 @@ func TestMain(m *testing.M) {
var opts []grpc.ServerOption
grpcServer := grpc.NewServer(opts...)

lis, err := net.Listen("tcp", eventAddress)
lis, err := net.Listen("tcp", eventAddressListen)
if err != nil {
panic(fmt.Sprintf("Error starting events listener %s", err))
}

eventAddress = lis.Addr().String()
eventURL = "grpc://" + eventAddress

ehServer = eventmocks.NewMockEventhubServer()

pb.RegisterEventsServer(grpcServer, ehServer)
Expand Down
6 changes: 4 additions & 2 deletions pkg/fab/mocks/mockbroadcastserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,16 @@ func (m *MockBroadcastServer) Deliver(server po.AtomicBroadcast_DeliverServer) e
}

//StartMockBroadcastServer starts mock server for unit testing purpose
func StartMockBroadcastServer(broadcastTestURL string, grpcServer *grpc.Server) *MockBroadcastServer {
func StartMockBroadcastServer(broadcastTestURL string, grpcServer *grpc.Server) (*MockBroadcastServer, string) {
lis, err := net.Listen("tcp", broadcastTestURL)
if err != nil {
panic(fmt.Sprintf("Error starting BroadcastServer %s", err))
}
addr := lis.Addr().String()

broadcastServer := new(MockBroadcastServer)
po.RegisterAtomicBroadcastServer(grpcServer, broadcastServer)
go grpcServer.Serve(lis)

return broadcastServer
return broadcastServer, addr
}
4 changes: 2 additions & 2 deletions pkg/fab/peer/peerendorser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ import (
)

const (
peer1URL = "localhost:7050"
peer2URL = "localhost:7054"
peer1URL = "localhost:0"
peer2URL = "localhost:0"
testAddress = "127.0.0.1:0"
)

Expand Down
2 changes: 1 addition & 1 deletion pkg/fabsdk/provider/fabpvdr/fabpvdr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func verifyPeer(t *testing.T, peer fab.Peer, url string) {
func TestCreatePeerFromConfig(t *testing.T) {
p := newMockInfraProvider(t)

url := "grpc://localhost:8080"
url := "grpc://localhost:9999"

peerCfg := core.NetworkPeer{
PeerConfig: core.PeerConfig{
Expand Down
Loading

0 comments on commit b15f6d3

Please sign in to comment.