From 2471f9a7de6b12473d335abe9003a1cbf166988b Mon Sep 17 00:00:00 2001 From: Gregory Haskins Date: Fri, 4 Nov 2016 15:12:09 -0400 Subject: [PATCH] Use an environment variable for unit-test peer address We want to be able to programmatically direct the unit tests to a peer instance without using a big-hammer approach like CORE_PEER_ADDRESS. Since the viper knobs impact and override all configurations, this would break most contexts. Instead, we define UNIT_TEST_PEER_IP which defaults to "localhost". Change-Id: I509a8ad6595fbb0b9bb9e1164c59dc07ea23fbc9 Signed-off-by: Greg Haskins --- core/comm/connection.go | 14 ++++++++++++++ core/comm/connection_test.go | 15 ++++++++------- core/peer/peer_test.go | 6 ++++-- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/core/comm/connection.go b/core/comm/connection.go index f315fe68780..c103e987749 100644 --- a/core/comm/connection.go +++ b/core/comm/connection.go @@ -17,6 +17,7 @@ limitations under the License. package comm import ( + "os" "time" "google.golang.org/grpc" @@ -31,6 +32,19 @@ const defaultTimeout = time.Second * 3 var commLogger = logging.MustGetLogger("comm") +func getEnv(key, def string) string { + val := os.Getenv(key) + if len(val) > 0 { + return val + } else { + return def + } +} + +func GetPeerTestingAddress(port string) string { + return getEnv("UNIT_TEST_PEER_IP", "localhost") + ":" + port +} + // NewClientConnectionWithAddress Returns a new grpc.ClientConn to the given address. func NewClientConnectionWithAddress(peerAddress string, block bool, tslEnabled bool, creds credentials.TransportCredentials) (*grpc.ClientConn, error) { var opts []grpc.DialOption diff --git a/core/comm/connection_test.go b/core/comm/connection_test.go index d6d1a5865c9..8483b323f1b 100644 --- a/core/comm/connection_test.go +++ b/core/comm/connection_test.go @@ -29,14 +29,15 @@ import ( func TestConnection_Correct(t *testing.T) { config.SetupTestConfig("./../../peer") viper.Set("ledger.blockchain.deploy-system-chaincode", "false") + peerAddress := GetPeerTestingAddress("7051") var tmpConn *grpc.ClientConn var err error if TLSEnabled() { - tmpConn, err = NewClientConnectionWithAddress(viper.GetString("peer.address"), true, true, InitTLSForPeer()) + tmpConn, err = NewClientConnectionWithAddress(peerAddress, true, true, InitTLSForPeer()) } - tmpConn, err = NewClientConnectionWithAddress(viper.GetString("peer.address"), true, false, nil) + tmpConn, err = NewClientConnectionWithAddress(peerAddress, true, false, nil) if err != nil { - t.Fatalf("error connection to server at host:port = %s\n", viper.GetString("peer.address")) + t.Fatalf("error connection to server at host:port = %s\n", peerAddress) } tmpConn.Close() @@ -45,15 +46,15 @@ func TestConnection_Correct(t *testing.T) { func TestConnection_WrongAddress(t *testing.T) { config.SetupTestConfig("./../../peer") viper.Set("ledger.blockchain.deploy-system-chaincode", "false") - viper.Set("peer.address", "0.0.0.0:7052") + peerAddress := GetPeerTestingAddress("7052") var tmpConn *grpc.ClientConn var err error if TLSEnabled() { - tmpConn, err = NewClientConnectionWithAddress(viper.GetString("peer.address"), true, true, InitTLSForPeer()) + tmpConn, err = NewClientConnectionWithAddress(peerAddress, true, true, InitTLSForPeer()) } - tmpConn, err = NewClientConnectionWithAddress(viper.GetString("peer.address"), true, false, nil) + tmpConn, err = NewClientConnectionWithAddress(peerAddress, true, false, nil) if err == nil { - fmt.Printf("error connection to server - at host:port = %s\n", viper.GetString("peer.address")) + fmt.Printf("error connection to server - at host:port = %s\n", peerAddress) t.Error("error connection to server - connection should fail") tmpConn.Close() } diff --git a/core/peer/peer_test.go b/core/peer/peer_test.go index 3f8f5d5fe1d..cf4a65a98a0 100644 --- a/core/peer/peer_test.go +++ b/core/peer/peer_test.go @@ -25,6 +25,7 @@ import ( "github.com/spf13/viper" + "github.com/hyperledger/fabric/core/comm" "github.com/hyperledger/fabric/core/config" pb "github.com/hyperledger/fabric/protos" "golang.org/x/net/context" @@ -37,9 +38,10 @@ func TestMain(m *testing.M) { config.SetupTestConfig("./../../peer") viper.Set("ledger.blockchain.deploy-system-chaincode", "false") - tmpConn, err := NewPeerClientConnection() + peerAddress := comm.GetPeerTestingAddress("7051") + tmpConn, err := NewPeerClientConnectionWithAddress(peerAddress) if err != nil { - fmt.Printf("error connection to server at host:port = %s\n", viper.GetString("peer.address")) + fmt.Printf("error connection to server at host:port = %s\n", peerAddress) os.Exit(1) } peerClientConn = tmpConn