Skip to content

Commit d494dcd

Browse files
committed
[FAB-9404] Fix port conflicts in core/peer tests
Modifies tests to use dynamic ports rather than statically configured ones where possible Change-Id: I8d46dc70b7fc5b7e41a882b8ba3cdaada7704c40 Signed-off-by: Gari Singh <gari.r.singh@gmail.com>
1 parent 0d3ccd0 commit d494dcd

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

core/peer/peer_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func TestCreateChainFromBlock(t *testing.T) {
103103

104104
// Initialize gossip service
105105
grpcServer := grpc.NewServer()
106-
socket, err := net.Listen("tcp", fmt.Sprintf("%s:%d", "", 13611))
106+
socket, err := net.Listen("tcp", "localhost:0")
107107
require.NoError(t, err)
108108

109109
msptesttools.LoadMSPSetupForTesting()
@@ -117,7 +117,7 @@ func TestCreateChainFromBlock(t *testing.T) {
117117
return dialOpts
118118
}
119119
err = service.InitGossipServiceCustomDeliveryFactory(
120-
identity, "localhost:13611", grpcServer, nil,
120+
identity, socket.Addr().String(), grpcServer, nil,
121121
&mockDeliveryClientFactory{},
122122
messageCryptoService, secAdv, defaultSecureDialOpts)
123123

core/peer/pkg_test.go

+17-12
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
"crypto/tls"
1111
"crypto/x509"
1212
"errors"
13-
"fmt"
1413
"io/ioutil"
14+
"net"
1515
"os"
1616
"path/filepath"
1717
"testing"
@@ -219,7 +219,6 @@ func TestUpdateRootsFromConfigBlock(t *testing.T) {
219219
// basic function tests
220220
var tests = []struct {
221221
name string
222-
listenAddress string
223222
serverConfig comm.ServerConfig
224223
createChannel func()
225224
goodOptions []grpc.DialOption
@@ -229,8 +228,7 @@ func TestUpdateRootsFromConfigBlock(t *testing.T) {
229228
}{
230229

231230
{
232-
name: "MutualTLSOrg1Org1",
233-
listenAddress: fmt.Sprintf("localhost:%d", 4051),
231+
name: "MutualTLSOrg1Org1",
234232
serverConfig: comm.ServerConfig{
235233
SecOpts: &comm.SecureOptions{
236234
UseTLS: true,
@@ -247,8 +245,7 @@ func TestUpdateRootsFromConfigBlock(t *testing.T) {
247245
numOrdererCAs: 1,
248246
},
249247
{
250-
name: "MutualTLSOrg1Org2",
251-
listenAddress: fmt.Sprintf("localhost:%d", 4052),
248+
name: "MutualTLSOrg1Org2",
252249
serverConfig: comm.ServerConfig{
253250
SecOpts: &comm.SecureOptions{
254251
UseTLS: true,
@@ -267,8 +264,7 @@ func TestUpdateRootsFromConfigBlock(t *testing.T) {
267264
numOrdererCAs: 2,
268265
},
269266
{
270-
name: "MutualTLSOrg1Org2Intermediate",
271-
listenAddress: fmt.Sprintf("localhost:%d", 4053),
267+
name: "MutualTLSOrg1Org2Intermediate",
272268
serverConfig: comm.ServerConfig{
273269
SecOpts: &comm.SecureOptions{
274270
UseTLS: true,
@@ -292,7 +288,7 @@ func TestUpdateRootsFromConfigBlock(t *testing.T) {
292288
test := test
293289
t.Run(test.name, func(t *testing.T) {
294290
t.Logf("Running test %s ...", test.name)
295-
server, err := peer.NewPeerServer(test.listenAddress, test.serverConfig)
291+
server, err := peer.NewPeerServer("localhost:0", test.serverConfig)
296292
if err != nil {
297293
t.Fatalf("NewPeerServer failed with error [%s]", err)
298294
} else {
@@ -303,9 +299,18 @@ func TestUpdateRootsFromConfigBlock(t *testing.T) {
303299
go server.Start()
304300
defer server.Stop()
305301

302+
// extract dynamic listen port
303+
_, port, err := net.SplitHostPort(server.Listener().Addr().String())
304+
if err != nil {
305+
t.Fatal(err)
306+
}
307+
t.Logf("listenAddress: %s", server.Listener().Addr())
308+
testAddress := "localhost:" + port
309+
t.Logf("testAddress: %s", testAddress)
310+
306311
// invoke the EmptyCall service with good options but should fail
307312
// until channel is created and root CAs are updated
308-
_, err = invokeEmptyCall(test.listenAddress, test.goodOptions)
313+
_, err = invokeEmptyCall(testAddress, test.goodOptions)
309314
assert.Error(t, err, "Expected error invoking the EmptyCall service ")
310315

311316
// creating channel should update the trusted client roots
@@ -319,11 +324,11 @@ func TestUpdateRootsFromConfigBlock(t *testing.T) {
319324
"Did not find expected number of orderer CAs for channel")
320325

321326
// invoke the EmptyCall service with good options
322-
_, err = invokeEmptyCall(test.listenAddress, test.goodOptions)
327+
_, err = invokeEmptyCall(testAddress, test.goodOptions)
323328
assert.NoError(t, err, "Failed to invoke the EmptyCall service")
324329

325330
// invoke the EmptyCall service with bad options
326-
_, err = invokeEmptyCall(test.listenAddress, test.badOptions)
331+
_, err = invokeEmptyCall(testAddress, test.badOptions)
327332
assert.Error(t, err, "Expected error using bad dial options")
328333
}
329334
})

0 commit comments

Comments
 (0)