@@ -10,8 +10,8 @@ import (
10
10
"crypto/tls"
11
11
"crypto/x509"
12
12
"errors"
13
- "fmt"
14
13
"io/ioutil"
14
+ "net"
15
15
"os"
16
16
"path/filepath"
17
17
"testing"
@@ -219,7 +219,6 @@ func TestUpdateRootsFromConfigBlock(t *testing.T) {
219
219
// basic function tests
220
220
var tests = []struct {
221
221
name string
222
- listenAddress string
223
222
serverConfig comm.ServerConfig
224
223
createChannel func ()
225
224
goodOptions []grpc.DialOption
@@ -229,8 +228,7 @@ func TestUpdateRootsFromConfigBlock(t *testing.T) {
229
228
}{
230
229
231
230
{
232
- name : "MutualTLSOrg1Org1" ,
233
- listenAddress : fmt .Sprintf ("localhost:%d" , 4051 ),
231
+ name : "MutualTLSOrg1Org1" ,
234
232
serverConfig : comm.ServerConfig {
235
233
SecOpts : & comm.SecureOptions {
236
234
UseTLS : true ,
@@ -247,8 +245,7 @@ func TestUpdateRootsFromConfigBlock(t *testing.T) {
247
245
numOrdererCAs : 1 ,
248
246
},
249
247
{
250
- name : "MutualTLSOrg1Org2" ,
251
- listenAddress : fmt .Sprintf ("localhost:%d" , 4052 ),
248
+ name : "MutualTLSOrg1Org2" ,
252
249
serverConfig : comm.ServerConfig {
253
250
SecOpts : & comm.SecureOptions {
254
251
UseTLS : true ,
@@ -267,8 +264,7 @@ func TestUpdateRootsFromConfigBlock(t *testing.T) {
267
264
numOrdererCAs : 2 ,
268
265
},
269
266
{
270
- name : "MutualTLSOrg1Org2Intermediate" ,
271
- listenAddress : fmt .Sprintf ("localhost:%d" , 4053 ),
267
+ name : "MutualTLSOrg1Org2Intermediate" ,
272
268
serverConfig : comm.ServerConfig {
273
269
SecOpts : & comm.SecureOptions {
274
270
UseTLS : true ,
@@ -292,7 +288,7 @@ func TestUpdateRootsFromConfigBlock(t *testing.T) {
292
288
test := test
293
289
t .Run (test .name , func (t * testing.T ) {
294
290
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 )
296
292
if err != nil {
297
293
t .Fatalf ("NewPeerServer failed with error [%s]" , err )
298
294
} else {
@@ -303,9 +299,18 @@ func TestUpdateRootsFromConfigBlock(t *testing.T) {
303
299
go server .Start ()
304
300
defer server .Stop ()
305
301
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
+
306
311
// invoke the EmptyCall service with good options but should fail
307
312
// until channel is created and root CAs are updated
308
- _ , err = invokeEmptyCall (test . listenAddress , test .goodOptions )
313
+ _ , err = invokeEmptyCall (testAddress , test .goodOptions )
309
314
assert .Error (t , err , "Expected error invoking the EmptyCall service " )
310
315
311
316
// creating channel should update the trusted client roots
@@ -319,11 +324,11 @@ func TestUpdateRootsFromConfigBlock(t *testing.T) {
319
324
"Did not find expected number of orderer CAs for channel" )
320
325
321
326
// invoke the EmptyCall service with good options
322
- _ , err = invokeEmptyCall (test . listenAddress , test .goodOptions )
327
+ _ , err = invokeEmptyCall (testAddress , test .goodOptions )
323
328
assert .NoError (t , err , "Failed to invoke the EmptyCall service" )
324
329
325
330
// invoke the EmptyCall service with bad options
326
- _ , err = invokeEmptyCall (test . listenAddress , test .badOptions )
331
+ _ , err = invokeEmptyCall (testAddress , test .badOptions )
327
332
assert .Error (t , err , "Expected error using bad dial options" )
328
333
}
329
334
})
0 commit comments