5f24171f13
And fix the 'value not used' issues. Many of these are not bugs, but a few are tests not checking errors, and one appears to be a missed error in non-test code.
56 lines
1.1 KiB
Go
56 lines
1.1 KiB
Go
package consul
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/hashicorp/consul/agent/metadata"
|
|
"github.com/hashicorp/raft"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
type testAddr struct {
|
|
addr string
|
|
}
|
|
|
|
func (ta *testAddr) Network() string {
|
|
return "tcp"
|
|
}
|
|
|
|
func (ta *testAddr) String() string {
|
|
return ta.addr
|
|
}
|
|
|
|
func TestServerLookup(t *testing.T) {
|
|
lookup := NewServerLookup()
|
|
addr := "72.0.0.17:8300"
|
|
id := "1"
|
|
|
|
svr := &metadata.Server{ID: id, Addr: &testAddr{addr}}
|
|
lookup.AddServer(svr)
|
|
|
|
got, err := lookup.ServerAddr(raft.ServerID(id))
|
|
if err != nil {
|
|
t.Fatalf("Unexpected error:%v", err)
|
|
}
|
|
if string(got) != addr {
|
|
t.Fatalf("Expected %v but got %v", addr, got)
|
|
}
|
|
|
|
server := lookup.Server(raft.ServerAddress(addr))
|
|
if server == nil {
|
|
t.Fatalf("Expected lookup to return true")
|
|
}
|
|
if server.Addr.String() != addr {
|
|
t.Fatalf("Expected lookup to return address %v but got %v", addr, server.Addr)
|
|
}
|
|
|
|
lookup.RemoveServer(svr)
|
|
|
|
_, err = lookup.ServerAddr("1")
|
|
require.EqualError(t, err, "Could not find address for server id 1")
|
|
|
|
svr2 := &metadata.Server{ID: "2", Addr: &testAddr{"123.4.5.6"}}
|
|
lookup.RemoveServer(svr2)
|
|
|
|
}
|