2017-08-30 00:37:48 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
2017-08-30 03:35:22 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent/metadata"
|
|
|
|
"github.com/hashicorp/raft"
|
2017-08-30 00:37:48 +00:00
|
|
|
)
|
|
|
|
|
2017-08-30 03:35:22 +00:00
|
|
|
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()
|
2017-08-30 00:37:48 +00:00
|
|
|
addr := "72.0.0.17:8300"
|
2017-08-30 03:35:22 +00:00
|
|
|
id := "1"
|
|
|
|
|
|
|
|
svr := &metadata.Server{ID: id, Addr: &testAddr{addr}}
|
|
|
|
lookup.AddServer(svr)
|
2017-08-30 00:37:48 +00:00
|
|
|
|
2017-08-30 03:35:22 +00:00
|
|
|
got, err := lookup.ServerAddr(raft.ServerID(id))
|
2017-08-30 00:37:48 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error:%v", err)
|
|
|
|
}
|
|
|
|
if string(got) != addr {
|
|
|
|
t.Fatalf("Expected %v but got %v", addr, got)
|
|
|
|
}
|
|
|
|
|
2017-08-30 17:31:36 +00:00
|
|
|
server := lookup.Server(raft.ServerAddress(addr))
|
|
|
|
if server == nil {
|
2017-08-30 03:35:22 +00:00
|
|
|
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)
|
2017-08-30 00:37:48 +00:00
|
|
|
|
|
|
|
got, err = lookup.ServerAddr("1")
|
|
|
|
expectedErr := fmt.Errorf("Could not find address for server id 1")
|
|
|
|
if expectedErr.Error() != err.Error() {
|
|
|
|
t.Fatalf("Unexpected error, got %v wanted %v", err, expectedErr)
|
|
|
|
}
|
|
|
|
|
2017-08-30 03:35:22 +00:00
|
|
|
svr2 := &metadata.Server{ID: "2", Addr: &testAddr{"123.4.5.6"}}
|
|
|
|
lookup.RemoveServer(svr2)
|
|
|
|
|
2017-08-30 00:37:48 +00:00
|
|
|
}
|