Add a struct key type for server_details

This commit is contained in:
Sean Chittenden 2016-03-26 17:58:12 -07:00
parent ae32a3ceae
commit 3ecd72f3b5
2 changed files with 265 additions and 0 deletions

View File

@ -8,6 +8,20 @@ import (
"github.com/hashicorp/serf/serf"
)
// Key is used in maps and for equality tests. A key is based on endpoints.
type Key struct {
Datacenter string
Port int
AddrString string
}
// Equal compares two Key objects
func (k *Key) Equal(x *Key) bool {
return k.Datacenter == x.Datacenter &&
k.Port == x.Port &&
k.AddrString == x.AddrString
}
// ServerDetails is used to return details of a consul server
type ServerDetails struct {
Name string
@ -19,6 +33,16 @@ type ServerDetails struct {
Addr net.Addr
}
// Key returns the corresponding Key
func (s *ServerDetails) Key() *Key {
return &Key{
Datacenter: s.Datacenter,
Port: s.Port,
AddrString: s.Addr.String() + s.Addr.Network(),
}
}
// String returns a string representation of ServerDetails
func (s *ServerDetails) String() string {
return fmt.Sprintf("%s (Addr: %s) (DC: %s)", s.Name, s.Addr, s.Datacenter)
}

View File

@ -8,6 +8,247 @@ import (
"github.com/hashicorp/serf/serf"
)
func TestServerDetails_Key_Equal(t *testing.T) {
tests := []struct {
name string
k1 *server_details.Key
k2 *server_details.Key
equal bool
}{
{
name: "IPv4 equality",
k1: &server_details.Key{
Datacenter: "dc1",
Port: 8300,
AddrString: "127.0.0.1",
},
k2: &server_details.Key{
Datacenter: "dc1",
Port: 8300,
AddrString: "127.0.0.1",
},
equal: true,
},
{
name: "IPv6 equality",
k1: &server_details.Key{
Datacenter: "dc1",
Port: 8300,
AddrString: "fc00::1",
},
k2: &server_details.Key{
Datacenter: "dc1",
Port: 8300,
AddrString: "fc00::1",
},
equal: true,
},
{
name: "IPv4 Inequality",
k1: &server_details.Key{
Datacenter: "dc1",
Port: 8300,
AddrString: "127.0.0.1",
},
k2: &server_details.Key{
Datacenter: "dc1",
Port: 8300,
AddrString: "1.2.3.4",
},
equal: false,
},
{
name: "IPv4 Inequality AddrString",
k1: &server_details.Key{
Datacenter: "dc1",
Port: 8300,
AddrString: "127.0.0.1ip+net",
},
k2: &server_details.Key{
Datacenter: "dc1",
Port: 8300,
AddrString: "127.0.0.1ip",
},
equal: false,
},
{
name: "IPv6 Inequality",
k1: &server_details.Key{
Datacenter: "dc1",
Port: 8300,
AddrString: "fc00::1",
},
k2: &server_details.Key{
Datacenter: "dc1",
Port: 8300,
AddrString: "2001:0db8:85a3::8a2e:0370:7334",
},
equal: false,
},
{
name: "Port Inequality",
k1: &server_details.Key{
Datacenter: "dc1",
Port: 8300,
AddrString: "127.0.0.1",
},
k2: &server_details.Key{
Datacenter: "dc1",
Port: 8500,
AddrString: "1.2.3.4",
},
equal: false,
},
{
name: "DC Inequality",
k1: &server_details.Key{
Datacenter: "dc1",
Port: 8300,
AddrString: "127.0.0.1",
},
k2: &server_details.Key{
Datacenter: "dc2",
Port: 8300,
AddrString: "127.0.0.1",
},
equal: false,
},
}
for _, test := range tests {
if test.k1.Equal(test.k2) != test.equal {
t.Errorf("Expected a %v result from test %s", test.equal, test.name)
}
// Test Key to make sure it actually works as a key
m := make(map[server_details.Key]bool)
m[*test.k1] = true
if _, found := m[*test.k2]; found != test.equal {
t.Errorf("Expected a %v result from map test %s", test.equal, test.name)
}
}
}
func TestServerDetails_Key(t *testing.T) {
ip := net.ParseIP("127.0.0.1")
tests := []struct {
name string
sd *server_details.ServerDetails
k *server_details.Key
equal bool
}{
{
name: "Key equality",
sd: &server_details.ServerDetails{
Datacenter: "dc1",
Port: 8300,
Addr: &net.IPAddr{IP: ip},
},
k: &server_details.Key{
Datacenter: "dc1",
Port: 8300,
AddrString: "127.0.0.1ip",
},
equal: true,
},
{
name: "Key inequality",
sd: &server_details.ServerDetails{
Datacenter: "dc1",
Port: 8300,
Addr: &net.IPAddr{IP: ip},
},
k: &server_details.Key{
Datacenter: "dc2",
Port: 8300,
AddrString: "127.0.0.1ip",
},
equal: false,
},
}
for _, test := range tests {
if test.k.Equal(test.sd.Key()) != test.equal {
t.Errorf("Expected a %v result from test %s", test.equal, test.name)
}
}
}
func TestServerDetails_Key_params(t *testing.T) {
ipv4a := net.ParseIP("127.0.0.1")
ipv4b := net.ParseIP("1.2.3.4")
tests := []struct {
name string
sd1 *server_details.ServerDetails
sd2 *server_details.ServerDetails
equal bool
}{
{
name: "Key equality",
sd1: &server_details.ServerDetails{
Name: "s1",
Datacenter: "dc1",
Port: 8300,
Addr: &net.IPAddr{IP: ipv4a},
},
sd2: &server_details.ServerDetails{
Name: "s1",
Datacenter: "dc1",
Port: 8300,
Addr: &net.IPAddr{IP: ipv4a},
},
equal: true,
},
{
name: "Key equality",
sd1: &server_details.ServerDetails{
Name: "s1",
Datacenter: "dc1",
Port: 8300,
Addr: &net.IPAddr{IP: ipv4a},
},
sd2: &server_details.ServerDetails{
Name: "s2",
Datacenter: "dc1",
Port: 8300,
Addr: &net.IPAddr{IP: ipv4a},
},
equal: true,
},
{
name: "Addr inequality",
sd1: &server_details.ServerDetails{
Name: "s1",
Datacenter: "dc1",
Port: 8300,
Addr: &net.IPAddr{IP: ipv4a},
},
sd2: &server_details.ServerDetails{
Name: "s1",
Datacenter: "dc1",
Port: 8300,
Addr: &net.IPAddr{IP: ipv4b},
},
equal: false,
},
}
for _, test := range tests {
if test.sd1.Key().Equal(test.sd2.Key()) != test.equal {
t.Errorf("Expected a %v result from test %s", test.equal, test.name)
}
// Test Key to make sure it actually works as a key
m := make(map[server_details.Key]bool)
m[*test.sd1.Key()] = true
if _, found := m[*test.sd2.Key()]; found != test.equal {
t.Errorf("Expected a %v result from map test %s", test.equal, test.name)
}
}
}
func TestIsConsulServer(t *testing.T) {
m := serf.Member{
Name: "foo",