diff --git a/pkg/client/channel/chclient_test.go b/pkg/client/channel/chclient_test.go index 30c24f225e..2848222023 100644 --- a/pkg/client/channel/chclient_test.go +++ b/pkg/client/channel/chclient_test.go @@ -32,8 +32,7 @@ import ( ) const ( - testAddress = "127.0.0.1:47882" - channelID = "testChannel" + channelID = "testChannel" ) func TestTxProposalResponseFilter(t *testing.T) { diff --git a/pkg/client/common/selection/dynamicselection/pgresolver/pgresolver_test.go b/pkg/client/common/selection/dynamicselection/pgresolver/pgresolver_test.go index e9e61b74a0..b855f9df00 100644 --- a/pkg/client/common/selection/dynamicselection/pgresolver/pgresolver_test.go +++ b/pkg/client/common/selection/dynamicselection/pgresolver/pgresolver_test.go @@ -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), diff --git a/pkg/client/msp/client_test.go b/pkg/client/msp/client_test.go index aba7016a86..b68ad57d4a 100644 --- a/pkg/client/msp/client_test.go +++ b/pkg/client/msp/client_test.go @@ -7,7 +7,9 @@ SPDX-License-Identifier: Apache-2.0 package msp import ( + "io/ioutil" "math/rand" + "net" "strconv" "strings" "testing" @@ -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) { @@ -137,7 +141,22 @@ 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) @@ -145,10 +164,7 @@ func (f *textFixture) setup() *fabsdk.FabricSDK { 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 @@ -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 } @@ -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 { diff --git a/pkg/client/msp/testdata/config_test.yaml b/pkg/client/msp/testdata/config_test.yaml index a1695b6a71..c1e4602563 100755 --- a/pkg/client/msp/testdata/config_test.yaml +++ b/pkg/client/msp/testdata/config_test.yaml @@ -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: @@ -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: diff --git a/pkg/client/resmgmt/resmgmt_test.go b/pkg/client/resmgmt/resmgmt_test.go index 87310b70c4..270af7201d 100644 --- a/pkg/client/resmgmt/resmgmt_test.go +++ b/pkg/client/resmgmt/resmgmt_test.go @@ -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{} @@ -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") @@ -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) @@ -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") @@ -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) @@ -1501,7 +1501,7 @@ 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{} @@ -1509,7 +1509,7 @@ func TestSaveChannelWithMultipleSigningIdenities(t *testing.T) { grpcOpts["allow-insecure"] = true oConfig := &core.OrdererConfig{ - URL: "127.0.0.1:7050", + URL: addr, GRPCOptions: grpcOpts, } mockConfig.SetCustomRandomOrdererCfg(oConfig) diff --git a/pkg/fab/channel/ledger_test.go b/pkg/fab/channel/ledger_test.go index 1c1affb04d..7ebba13dc3 100644 --- a/pkg/fab/channel/ledger_test.go +++ b/pkg/fab/channel/ledger_test.go @@ -149,7 +149,7 @@ func TestQueryConfig(t *testing.T) { "Org1MSP", "Org2MSP", }, - OrdererAddress: "localhost:7054", + OrdererAddress: "localhost:9999", RootCA: validRootCA, }, Index: 0, @@ -192,7 +192,7 @@ func TestQueryConfig(t *testing.T) { MSPNames: []string{ "Org1MSP", }, - OrdererAddress: "builder2:7054", + OrdererAddress: "builder2:9999", RootCA: validRootCA, }, Index: 0, @@ -222,7 +222,7 @@ func TestQueryConfigBlockDifferentMetadata(t *testing.T) { "Org1MSP", "Org2MSP", }, - OrdererAddress: "localhost:7054", + OrdererAddress: "localhost:9999", RootCA: validRootCA, }, Index: 0, diff --git a/pkg/fab/chconfig/chconfig_test.go b/pkg/fab/chconfig/chconfig_test.go index ce259376c2..209c82cdb5 100644 --- a/pkg/fab/chconfig/chconfig_test.go +++ b/pkg/fab/chconfig/chconfig_test.go @@ -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 { diff --git a/pkg/fab/events/eventhubclient/connection/connection_test.go b/pkg/fab/events/eventhubclient/connection/connection_test.go index 59747d7303..2d5c41ba92 100755 --- a/pkg/fab/events/eventhubclient/connection/connection_test.go +++ b/pkg/fab/events/eventhubclient/connection/connection_test.go @@ -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") @@ -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) diff --git a/pkg/fab/mocks/mockbroadcastserver.go b/pkg/fab/mocks/mockbroadcastserver.go index d7a3e4e3da..a4deea8eda 100644 --- a/pkg/fab/mocks/mockbroadcastserver.go +++ b/pkg/fab/mocks/mockbroadcastserver.go @@ -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 } diff --git a/pkg/fab/peer/peerendorser_test.go b/pkg/fab/peer/peerendorser_test.go index 8f2943c20c..92bbb648fa 100644 --- a/pkg/fab/peer/peerendorser_test.go +++ b/pkg/fab/peer/peerendorser_test.go @@ -31,8 +31,8 @@ import ( ) const ( - peer1URL = "localhost:7050" - peer2URL = "localhost:7054" + peer1URL = "localhost:0" + peer2URL = "localhost:0" testAddress = "127.0.0.1:0" ) diff --git a/pkg/fabsdk/provider/fabpvdr/fabpvdr_test.go b/pkg/fabsdk/provider/fabpvdr/fabpvdr_test.go index 0d9016e75f..03e665a24d 100644 --- a/pkg/fabsdk/provider/fabpvdr/fabpvdr_test.go +++ b/pkg/fabsdk/provider/fabpvdr/fabpvdr_test.go @@ -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{ diff --git a/pkg/msp/main_test.go b/pkg/msp/main_test.go index 74c2d14822..97dccb3e89 100644 --- a/pkg/msp/main_test.go +++ b/pkg/msp/main_test.go @@ -9,6 +9,7 @@ package msp import ( "fmt" "io/ioutil" + "net" "os" "strings" "testing" @@ -24,7 +25,7 @@ import ( const ( org1 = "Org1" - caServerURL = "http://localhost:8090" + caServerURLListen = "http://127.0.0.1:0" dummyUserStorePath = "/tmp/userstore" fullConfigPath = "testdata/config_test.yaml" wrongURLConfigPath = "testdata/config_wrong_url.yaml" @@ -33,6 +34,8 @@ const ( noRegistrarConfigPath = "testdata/config_no_registrar.yaml" ) +var caServerURL string + type textFixture struct { config core.Config cryptoSuite core.CryptoSuite @@ -49,8 +52,19 @@ func (f *textFixture) setup(configPath string) { configPath = fullConfigPath } + var lis net.Listener var err error - f.config, err = config.FromFile(configPath)() + 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) + f.config, err = config.FromRaw(cfgRaw, "yaml")() if err != nil { panic(fmt.Sprintf("Failed to read config: %v", err)) } @@ -64,6 +78,7 @@ func (f *textFixture) setup(configPath string) { if f.cryptoSuite == nil { panic(fmt.Sprintf("Failed initialize cryptoSuite: %v", err)) } + if f.config.CredentialStorePath() != "" { f.userStore, err = NewCertFileUserStore(f.config.CredentialStorePath()) if err != nil { @@ -83,7 +98,9 @@ func (f *textFixture) setup(configPath string) { } // Start Http Server if it's not running - caServer.Start(strings.TrimPrefix(caServerURL, "http://"), f.cryptoSuite) + if !caServer.Running() { + caServer.Start(lis, f.cryptoSuite) + } } func (f *textFixture) close() { @@ -100,6 +117,16 @@ func readCert(t *testing.T) []byte { return cert } +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 { diff --git a/pkg/msp/mocks/mockfabriccaserver.go b/pkg/msp/mocks/mockfabriccaserver.go index 859ccce251..77328b9ec8 100644 --- a/pkg/msp/mocks/mockfabriccaserver.go +++ b/pkg/msp/mocks/mockfabriccaserver.go @@ -7,6 +7,7 @@ SPDX-License-Identifier: Apache-2.0 package mocks import ( + "net" "net/http" "time" @@ -70,13 +71,14 @@ type MockFabricCAServer struct { } // Start fabric CA mock server -func (s *MockFabricCAServer) Start(address string, cryptoSuite core.CryptoSuite) error { +func (s *MockFabricCAServer) Start(lis net.Listener, cryptoSuite core.CryptoSuite) { if s.running { - return nil + panic("already started") } - s.address = address + addr := lis.Addr().String() + s.address = addr s.cryptoSuite = cryptoSuite // Register request handlers @@ -85,12 +87,12 @@ func (s *MockFabricCAServer) Start(address string, cryptoSuite core.CryptoSuite) http.HandleFunc("/reenroll", s.enroll) server := &http.Server{ - Addr: s.address, + Addr: addr, TLSConfig: nil, } go func() { - err := server.ListenAndServe() + err := server.Serve(lis) if err != nil { panic("HTTP Server: Failed to start") } @@ -99,10 +101,14 @@ func (s *MockFabricCAServer) Start(address string, cryptoSuite core.CryptoSuite) logger.Infof("HTTP Server started on %s", s.address) s.running = true - return nil } +// Running returns the status of the mock server +func (s *MockFabricCAServer) Running() bool { + return s.running +} + func (s *MockFabricCAServer) addKeyToKeyStore(privateKey []byte) error { // Import private key that matches the cert we will return // from this mock service, so it can be looked up by SKI from the cert diff --git a/pkg/msp/testdata/config_embedded_registrar.yaml b/pkg/msp/testdata/config_embedded_registrar.yaml index a61c3bd42d..da780485c4 100755 --- a/pkg/msp/testdata/config_embedded_registrar.yaml +++ b/pkg/msp/testdata/config_embedded_registrar.yaml @@ -280,7 +280,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: diff --git a/pkg/msp/testdata/config_test.yaml b/pkg/msp/testdata/config_test.yaml index a1695b6a71..e68adb3775 100755 --- a/pkg/msp/testdata/config_test.yaml +++ b/pkg/msp/testdata/config_test.yaml @@ -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: