Skip to content

Commit

Permalink
Fix tests in IPv6-only environments (hashicorp#415)
Browse files Browse the repository at this point in the history
  • Loading branch information
stapelberg committed Aug 19, 2020
1 parent e8ce96e commit bda5e42
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 22 deletions.
2 changes: 1 addition & 1 deletion integ_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func MakeRaft(t *testing.T, conf *Config, bootstrap bool) *RaftEnv {
snapshot: snap,
fsm: &MockFSM{},
}
trans, err := NewTCPTransport("127.0.0.1:0", nil, 2, time.Second, nil)
trans, err := NewTCPTransport("localhost:0", nil, 2, time.Second, nil)
if err != nil {
t.Fatalf("err: %v", err)
}
Expand Down
34 changes: 17 additions & 17 deletions net_transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (t *testAddrProvider) ServerAddr(id ServerID) (ServerAddress, error) {

func TestNetworkTransport_CloseStreams(t *testing.T) {
// Transport 1 is consumer
trans1, err := NewTCPTransportWithLogger("127.0.0.1:0", nil, 2, time.Second, newTestLogger(t))
trans1, err := NewTCPTransportWithLogger("localhost:0", nil, 2, time.Second, newTestLogger(t))
if err != nil {
t.Fatalf("err: %v", err)
}
Expand Down Expand Up @@ -71,7 +71,7 @@ func TestNetworkTransport_CloseStreams(t *testing.T) {
}()

// Transport 2 makes outbound request, 3 conn pool
trans2, err := NewTCPTransportWithLogger("127.0.0.1:0", nil, 3, time.Second, newTestLogger(t))
trans2, err := NewTCPTransportWithLogger("localhost:0", nil, 3, time.Second, newTestLogger(t))
if err != nil {
t.Fatalf("err: %v", err)
}
Expand Down Expand Up @@ -119,7 +119,7 @@ func TestNetworkTransport_CloseStreams(t *testing.T) {
}

func TestNetworkTransport_StartStop(t *testing.T) {
trans, err := NewTCPTransportWithLogger("127.0.0.1:0", nil, 2, time.Second, newTestLogger(t))
trans, err := NewTCPTransportWithLogger("localhost:0", nil, 2, time.Second, newTestLogger(t))
if err != nil {
t.Fatalf("err: %v", err)
}
Expand All @@ -128,7 +128,7 @@ func TestNetworkTransport_StartStop(t *testing.T) {

func TestNetworkTransport_Heartbeat_FastPath(t *testing.T) {
// Transport 1 is consumer
trans1, err := NewTCPTransportWithLogger("127.0.0.1:0", nil, 2, time.Second, newTestLogger(t))
trans1, err := NewTCPTransportWithLogger("localhost:0", nil, 2, time.Second, newTestLogger(t))
if err != nil {
t.Fatalf("err: %v", err)
}
Expand Down Expand Up @@ -159,7 +159,7 @@ func TestNetworkTransport_Heartbeat_FastPath(t *testing.T) {
trans1.SetHeartbeatHandler(fastpath)

// Transport 2 makes outbound request
trans2, err := NewTCPTransportWithLogger("127.0.0.1:0", nil, 2, time.Second, newTestLogger(t))
trans2, err := NewTCPTransportWithLogger("localhost:0", nil, 2, time.Second, newTestLogger(t))
if err != nil {
t.Fatalf("err: %v", err)
}
Expand All @@ -185,7 +185,7 @@ func TestNetworkTransport_AppendEntries(t *testing.T) {

for _, useAddrProvider := range []bool{true, false} {
// Transport 1 is consumer
trans1, err := makeTransport(t, useAddrProvider, "127.0.0.1:0")
trans1, err := makeTransport(t, useAddrProvider, "localhost:0")
if err != nil {
t.Fatalf("err: %v", err)
}
Expand Down Expand Up @@ -254,7 +254,7 @@ func TestNetworkTransport_AppendEntriesPipeline(t *testing.T) {

for _, useAddrProvider := range []bool{true, false} {
// Transport 1 is consumer
trans1, err := makeTransport(t, useAddrProvider, "127.0.0.1:0")
trans1, err := makeTransport(t, useAddrProvider, "localhost:0")
if err != nil {
t.Fatalf("err: %v", err)
}
Expand Down Expand Up @@ -337,7 +337,7 @@ func TestNetworkTransport_AppendEntriesPipeline(t *testing.T) {

func TestNetworkTransport_AppendEntriesPipeline_CloseStreams(t *testing.T) {
// Transport 1 is consumer
trans1, err := makeTransport(t, true, "127.0.0.1:0")
trans1, err := makeTransport(t, true, "localhost:0")
if err != nil {
t.Fatalf("err: %v", err)
}
Expand Down Expand Up @@ -446,7 +446,7 @@ func TestNetworkTransport_RequestVote(t *testing.T) {

for _, useAddrProvider := range []bool{true, false} {
// Transport 1 is consumer
trans1, err := makeTransport(t, useAddrProvider, "127.0.0.1:0")
trans1, err := makeTransport(t, useAddrProvider, "localhost:0")
if err != nil {
t.Fatalf("err: %v", err)
}
Expand Down Expand Up @@ -505,7 +505,7 @@ func TestNetworkTransport_InstallSnapshot(t *testing.T) {

for _, useAddrProvider := range []bool{true, false} {
// Transport 1 is consumer
trans1, err := makeTransport(t, useAddrProvider, "127.0.0.1:0")
trans1, err := makeTransport(t, useAddrProvider, "localhost:0")
if err != nil {
t.Fatalf("err: %v", err)
}
Expand Down Expand Up @@ -576,7 +576,7 @@ func TestNetworkTransport_InstallSnapshot(t *testing.T) {

func TestNetworkTransport_EncodeDecode(t *testing.T) {
// Transport 1 is consumer
trans1, err := NewTCPTransportWithLogger("127.0.0.1:0", nil, 2, time.Second, newTestLogger(t))
trans1, err := NewTCPTransportWithLogger("localhost:0", nil, 2, time.Second, newTestLogger(t))
if err != nil {
t.Fatalf("err: %v", err)
}
Expand All @@ -592,9 +592,9 @@ func TestNetworkTransport_EncodeDecode(t *testing.T) {
}

func TestNetworkTransport_EncodeDecode_AddressProvider(t *testing.T) {
addressOverride := "127.0.0.1:11111"
addressOverride := "localhost:11111"
config := &NetworkTransportConfig{MaxPool: 2, Timeout: time.Second, Logger: newTestLogger(t), ServerAddressProvider: &testAddrProvider{addressOverride}}
trans1, err := NewTCPTransportWithConfig("127.0.0.1:0", nil, config)
trans1, err := NewTCPTransportWithConfig("localhost:0", nil, config)
if err != nil {
t.Fatalf("err: %v", err)
}
Expand All @@ -611,7 +611,7 @@ func TestNetworkTransport_EncodeDecode_AddressProvider(t *testing.T) {

func TestNetworkTransport_PooledConn(t *testing.T) {
// Transport 1 is consumer
trans1, err := NewTCPTransportWithLogger("127.0.0.1:0", nil, 2, time.Second, newTestLogger(t))
trans1, err := NewTCPTransportWithLogger("localhost:0", nil, 2, time.Second, newTestLogger(t))
if err != nil {
t.Fatalf("err: %v", err)
}
Expand Down Expand Up @@ -658,7 +658,7 @@ func TestNetworkTransport_PooledConn(t *testing.T) {
}()

// Transport 2 makes outbound request, 3 conn pool
trans2, err := NewTCPTransportWithLogger("127.0.0.1:0", nil, 3, time.Second, newTestLogger(t))
trans2, err := NewTCPTransportWithLogger("localhost:0", nil, 3, time.Second, newTestLogger(t))
if err != nil {
t.Fatalf("err: %v", err)
}
Expand Down Expand Up @@ -699,9 +699,9 @@ func TestNetworkTransport_PooledConn(t *testing.T) {
func makeTransport(t *testing.T, useAddrProvider bool, addressOverride string) (*NetworkTransport, error) {
if useAddrProvider {
config := &NetworkTransportConfig{MaxPool: 2, Timeout: time.Second, Logger: newTestLogger(t), ServerAddressProvider: &testAddrProvider{addressOverride}}
return NewTCPTransportWithConfig("127.0.0.1:0", nil, config)
return NewTCPTransportWithConfig("localhost:0", nil, config)
}
return NewTCPTransportWithLogger("127.0.0.1:0", nil, 2, time.Second, newTestLogger(t))
return NewTCPTransportWithLogger("localhost:0", nil, 2, time.Second, newTestLogger(t))
}

type testCountingWriter struct {
Expand Down
4 changes: 2 additions & 2 deletions raft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1966,7 +1966,7 @@ func TestRaft_LeadershipTransferToInvalidID(t *testing.T) {
c := MakeCluster(3, t, nil)
defer c.Close()

future := c.Leader().LeadershipTransferToServer(ServerID("abc"), ServerAddress("127.0.0.1"))
future := c.Leader().LeadershipTransferToServer(ServerID("abc"), ServerAddress("localhost"))
if future.Error() == nil {
t.Fatal("leadership transfer should err")
}
Expand All @@ -1983,7 +1983,7 @@ func TestRaft_LeadershipTransferToInvalidAddress(t *testing.T) {
defer c.Close()

follower := c.GetInState(Follower)[0]
future := c.Leader().LeadershipTransferToServer(follower.localID, ServerAddress("127.0.0.1"))
future := c.Leader().LeadershipTransferToServer(follower.localID, ServerAddress("localhost"))
if future.Error() == nil {
t.Fatal("leadership transfer should err")
}
Expand Down
11 changes: 9 additions & 2 deletions tcp_transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,19 @@ func TestTCPTransport_EmptyAddr(t *testing.T) {
}

func TestTCPTransport_WithAdvertise(t *testing.T) {
addr := &net.TCPAddr{IP: []byte{127, 0, 0, 1}, Port: 12345}
ips, err := net.LookupIP("localhost")
if err != nil {
t.Fatal(err)
}
if len(ips) == 0 {
t.Fatalf("localhost did not resolve to any IPs")
}
addr := &net.TCPAddr{IP: ips[0], Port: 12345}
trans, err := NewTCPTransportWithLogger("0.0.0.0:0", addr, 1, 0, newTestLogger(t))
if err != nil {
t.Fatalf("err: %v", err)
}
if trans.LocalAddr() != "127.0.0.1:12345" {
if trans.LocalAddr() != ServerAddress(net.JoinHostPort(ips[0].String(), "12345")) {
t.Fatalf("bad: %v", trans.LocalAddr())
}
}

0 comments on commit bda5e42

Please sign in to comment.