Skip to content

Commit

Permalink
Use an environment variable for unit-test peer address
Browse files Browse the repository at this point in the history
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 <gregory.haskins@gmail.com>
  • Loading branch information
ghaskins committed Nov 7, 2016
1 parent db404bd commit 2471f9a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
14 changes: 14 additions & 0 deletions core/comm/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package comm

import (
"os"
"time"

"google.golang.org/grpc"
Expand All @@ -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
Expand Down
15 changes: 8 additions & 7 deletions core/comm/connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
}
Expand Down
6 changes: 4 additions & 2 deletions core/peer/peer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down

0 comments on commit 2471f9a

Please sign in to comment.