Change the definition of the ServerDetails struct key

Use only the serf Name for now.  Leaving the plumbing for now.
This commit is contained in:
Sean Chittenden 2016-03-28 12:53:19 -07:00
parent 2bcff6bac4
commit 6c9fb06511
3 changed files with 88 additions and 213 deletions

View File

@ -10,16 +10,12 @@ import (
// Key is used in maps and for equality tests. A key is based on endpoints. // Key is used in maps and for equality tests. A key is based on endpoints.
type Key struct { type Key struct {
Datacenter string name string
Port int
AddrString string
} }
// Equal compares two Key objects // Equal compares two Key objects
func (k *Key) Equal(x *Key) bool { func (k *Key) Equal(x *Key) bool {
return k.Datacenter == x.Datacenter && return k.name == x.name
k.Port == x.Port &&
k.AddrString == x.AddrString
} }
// ServerDetails is used to return details of a consul server // ServerDetails is used to return details of a consul server
@ -35,14 +31,8 @@ type ServerDetails struct {
// Key returns the corresponding Key // Key returns the corresponding Key
func (s *ServerDetails) Key() *Key { func (s *ServerDetails) Key() *Key {
var serverAddr string
if s.Addr != nil {
serverAddr = s.Addr.String() + s.Addr.Network()
}
return &Key{ return &Key{
Datacenter: s.Datacenter, name: s.Name,
Port: s.Port,
AddrString: serverAddr,
} }
} }

View File

@ -0,0 +1,84 @@
package server_details
import (
"testing"
)
func TestServerDetails_Key_Equal(t *testing.T) {
tests := []struct {
name string
k1 *Key
k2 *Key
equal bool
}{
{
name: "IPv4 equality",
k1: &Key{
name: "s1",
},
k2: &Key{
name: "s1",
},
equal: true,
},
{
name: "IPv4 Inequality",
k1: &Key{
name: "s1",
},
k2: &Key{
name: "s2",
},
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[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) {
tests := []struct {
name string
sd *ServerDetails
k *Key
equal bool
}{
{
name: "Key equality",
sd: &ServerDetails{
Name: "s1",
},
k: &Key{
name: "s1",
},
equal: true,
},
{
name: "Key inequality",
sd: &ServerDetails{
Name: "s1",
},
k: &Key{
name: "s2",
},
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)
}
}
}

View File

@ -8,173 +8,6 @@ import (
"github.com/hashicorp/serf/serf" "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) { func TestServerDetails_Key_params(t *testing.T) {
ipv4a := net.ParseIP("127.0.0.1") ipv4a := net.ParseIP("127.0.0.1")
ipv4b := net.ParseIP("1.2.3.4") ipv4b := net.ParseIP("1.2.3.4")
@ -185,38 +18,6 @@ func TestServerDetails_Key_params(t *testing.T) {
sd2 *server_details.ServerDetails sd2 *server_details.ServerDetails
equal bool 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", name: "Addr inequality",
sd1: &server_details.ServerDetails{ sd1: &server_details.ServerDetails{
@ -231,7 +32,7 @@ func TestServerDetails_Key_params(t *testing.T) {
Port: 8300, Port: 8300,
Addr: &net.IPAddr{IP: ipv4b}, Addr: &net.IPAddr{IP: ipv4b},
}, },
equal: false, equal: true,
}, },
} }