diff --git a/consul/server_details/server_details.go b/consul/server_details/server_details.go index d2d12ff80b..4f3dc794a6 100644 --- a/consul/server_details/server_details.go +++ b/consul/server_details/server_details.go @@ -10,16 +10,12 @@ import ( // Key is used in maps and for equality tests. A key is based on endpoints. type Key struct { - Datacenter string - Port int - AddrString string + name 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 + return k.name == x.name } // ServerDetails is used to return details of a consul server @@ -35,14 +31,8 @@ type ServerDetails struct { // Key returns the corresponding Key func (s *ServerDetails) Key() *Key { - var serverAddr string - if s.Addr != nil { - serverAddr = s.Addr.String() + s.Addr.Network() - } return &Key{ - Datacenter: s.Datacenter, - Port: s.Port, - AddrString: serverAddr, + name: s.Name, } } diff --git a/consul/server_details/server_details_internal_test.go b/consul/server_details/server_details_internal_test.go new file mode 100644 index 0000000000..4028fe88cc --- /dev/null +++ b/consul/server_details/server_details_internal_test.go @@ -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) + } + } +} diff --git a/consul/server_details/server_details_test.go b/consul/server_details/server_details_test.go index c34cdced81..b16cbf5a0a 100644 --- a/consul/server_details/server_details_test.go +++ b/consul/server_details/server_details_test.go @@ -8,173 +8,6 @@ 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") @@ -185,38 +18,6 @@ func TestServerDetails_Key_params(t *testing.T) { 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{ @@ -231,7 +32,7 @@ func TestServerDetails_Key_params(t *testing.T) { Port: 8300, Addr: &net.IPAddr{IP: ipv4b}, }, - equal: false, + equal: true, }, }