2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2017-08-30 00:37:48 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2017-08-30 03:35:22 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent/metadata"
|
|
|
|
"github.com/hashicorp/raft"
|
2020-06-05 19:28:03 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
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
|
|
|
|
2020-06-05 19:28:03 +00:00
|
|
|
_, err = lookup.ServerAddr("1")
|
|
|
|
require.EqualError(t, err, "Could not find address for server id 1")
|
2017-08-30 00:37:48 +00:00
|
|
|
|
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
|
|
|
}
|