mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
Merge pull request #1194 from unihorn/141
etcdserver: return client urls for /v2/machines
This commit is contained in:
@@ -99,10 +99,10 @@ func (c Cluster) IDs() []int64 {
|
||||
return ids
|
||||
}
|
||||
|
||||
// Endpoints returns a list of all peer addresses. Each address is prefixed
|
||||
// PeerURLs returns a list of all peer addresses. Each address is prefixed
|
||||
// with the scheme (currently "http://"). The returned list is sorted in
|
||||
// ascending lexicographical order.
|
||||
func (c Cluster) Endpoints() []string {
|
||||
func (c Cluster) PeerURLs() []string {
|
||||
endpoints := make([]string, 0)
|
||||
for _, p := range c {
|
||||
for _, addr := range p.PeerURLs {
|
||||
@@ -112,3 +112,17 @@ func (c Cluster) Endpoints() []string {
|
||||
sort.Strings(endpoints)
|
||||
return endpoints
|
||||
}
|
||||
|
||||
// ClientURLs returns a list of all client addresses. Each address is prefixed
|
||||
// with the scheme (currently "http://"). The returned list is sorted in
|
||||
// ascending lexicographical order.
|
||||
func (c Cluster) ClientURLs() []string {
|
||||
urls := make([]string, 0)
|
||||
for _, p := range c {
|
||||
for _, url := range p.ClientURLs {
|
||||
urls = append(urls, addScheme(url))
|
||||
}
|
||||
}
|
||||
sort.Strings(urls)
|
||||
return urls
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package etcdserver
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -141,3 +142,121 @@ func TestClusterAddBad(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestClusterPeerURLs(t *testing.T) {
|
||||
tests := []struct {
|
||||
mems []Member
|
||||
wurls []string
|
||||
}{
|
||||
// single peer with a single address
|
||||
{
|
||||
mems: []Member{
|
||||
{ID: 1, PeerURLs: []string{"192.0.2.1"}},
|
||||
},
|
||||
wurls: []string{"http://192.0.2.1"},
|
||||
},
|
||||
|
||||
// single peer with a single address with a port
|
||||
{
|
||||
mems: []Member{
|
||||
{ID: 1, PeerURLs: []string{"192.0.2.1:8001"}},
|
||||
},
|
||||
wurls: []string{"http://192.0.2.1:8001"},
|
||||
},
|
||||
|
||||
// several members explicitly unsorted
|
||||
{
|
||||
mems: []Member{
|
||||
{ID: 2, PeerURLs: []string{"192.0.2.3", "192.0.2.4"}},
|
||||
{ID: 3, PeerURLs: []string{"192.0.2.5", "192.0.2.6"}},
|
||||
{ID: 1, PeerURLs: []string{"192.0.2.1", "192.0.2.2"}},
|
||||
},
|
||||
wurls: []string{"http://192.0.2.1", "http://192.0.2.2", "http://192.0.2.3", "http://192.0.2.4", "http://192.0.2.5", "http://192.0.2.6"},
|
||||
},
|
||||
|
||||
// no members
|
||||
{
|
||||
mems: []Member{},
|
||||
wurls: []string{},
|
||||
},
|
||||
|
||||
// peer with no peer urls
|
||||
{
|
||||
mems: []Member{
|
||||
{ID: 3, PeerURLs: []string{}},
|
||||
},
|
||||
wurls: []string{},
|
||||
},
|
||||
}
|
||||
|
||||
for i, tt := range tests {
|
||||
c := Cluster{}
|
||||
if err := c.AddSlice(tt.mems); err != nil {
|
||||
t.Errorf("AddSlice error: %v", err)
|
||||
continue
|
||||
}
|
||||
urls := c.PeerURLs()
|
||||
if !reflect.DeepEqual(urls, tt.wurls) {
|
||||
t.Errorf("#%d: PeerURLs = %v, want %v", i, urls, tt.wurls)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestClusterClientURLs(t *testing.T) {
|
||||
tests := []struct {
|
||||
mems []Member
|
||||
wurls []string
|
||||
}{
|
||||
// single peer with a single address
|
||||
{
|
||||
mems: []Member{
|
||||
{ID: 1, ClientURLs: []string{"192.0.2.1"}},
|
||||
},
|
||||
wurls: []string{"http://192.0.2.1"},
|
||||
},
|
||||
|
||||
// single peer with a single address with a port
|
||||
{
|
||||
mems: []Member{
|
||||
{ID: 1, ClientURLs: []string{"192.0.2.1:8001"}},
|
||||
},
|
||||
wurls: []string{"http://192.0.2.1:8001"},
|
||||
},
|
||||
|
||||
// several members explicitly unsorted
|
||||
{
|
||||
mems: []Member{
|
||||
{ID: 2, ClientURLs: []string{"192.0.2.3", "192.0.2.4"}},
|
||||
{ID: 3, ClientURLs: []string{"192.0.2.5", "192.0.2.6"}},
|
||||
{ID: 1, ClientURLs: []string{"192.0.2.1", "192.0.2.2"}},
|
||||
},
|
||||
wurls: []string{"http://192.0.2.1", "http://192.0.2.2", "http://192.0.2.3", "http://192.0.2.4", "http://192.0.2.5", "http://192.0.2.6"},
|
||||
},
|
||||
|
||||
// no members
|
||||
{
|
||||
mems: []Member{},
|
||||
wurls: []string{},
|
||||
},
|
||||
|
||||
// peer with no client urls
|
||||
{
|
||||
mems: []Member{
|
||||
{ID: 3, ClientURLs: []string{}},
|
||||
},
|
||||
wurls: []string{},
|
||||
},
|
||||
}
|
||||
|
||||
for i, tt := range tests {
|
||||
c := Cluster{}
|
||||
if err := c.AddSlice(tt.mems); err != nil {
|
||||
t.Errorf("AddSlice error: %v", err)
|
||||
continue
|
||||
}
|
||||
urls := c.ClientURLs()
|
||||
if !reflect.DeepEqual(urls, tt.wurls) {
|
||||
t.Errorf("#%d: ClientURLs = %v, want %v", i, urls, tt.wurls)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -115,7 +115,7 @@ func (h serverHandler) serveMachines(w http.ResponseWriter, r *http.Request) {
|
||||
if !allowMethod(w, r.Method, "GET", "HEAD") {
|
||||
return
|
||||
}
|
||||
endpoints := h.clusterStore.Get().Endpoints()
|
||||
endpoints := h.clusterStore.Get().ClientURLs()
|
||||
w.Write([]byte(strings.Join(endpoints, ", ")))
|
||||
}
|
||||
|
||||
|
||||
@@ -612,9 +612,9 @@ func TestV2MachinesEndpoint(t *testing.T) {
|
||||
func TestServeMachines(t *testing.T) {
|
||||
cluster := &fakeCluster{
|
||||
members: []etcdserver.Member{
|
||||
{ID: 0xBEEF0, PeerURLs: []string{"localhost:8080"}},
|
||||
{ID: 0xBEEF1, PeerURLs: []string{"localhost:8081"}},
|
||||
{ID: 0xBEEF2, PeerURLs: []string{"localhost:8082"}},
|
||||
{ID: 0xBEEF0, ClientURLs: []string{"localhost:8080"}},
|
||||
{ID: 0xBEEF1, ClientURLs: []string{"localhost:8081"}},
|
||||
{ID: 0xBEEF2, ClientURLs: []string{"localhost:8082"}},
|
||||
},
|
||||
}
|
||||
|
||||
@@ -634,68 +634,6 @@ func TestServeMachines(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestClusterGetEndpoints(t *testing.T) {
|
||||
tests := []struct {
|
||||
clusterStore etcdserver.ClusterStore
|
||||
endpoints []string
|
||||
}{
|
||||
// single peer with a single address
|
||||
{
|
||||
clusterStore: &fakeCluster{
|
||||
members: []etcdserver.Member{
|
||||
{ID: 1, PeerURLs: []string{"192.0.2.1"}},
|
||||
},
|
||||
},
|
||||
endpoints: []string{"http://192.0.2.1"},
|
||||
},
|
||||
|
||||
// single peer with a single address with a port
|
||||
{
|
||||
clusterStore: &fakeCluster{
|
||||
members: []etcdserver.Member{
|
||||
{ID: 1, PeerURLs: []string{"192.0.2.1:8001"}},
|
||||
},
|
||||
},
|
||||
endpoints: []string{"http://192.0.2.1:8001"},
|
||||
},
|
||||
|
||||
// several members explicitly unsorted
|
||||
{
|
||||
clusterStore: &fakeCluster{
|
||||
members: []etcdserver.Member{
|
||||
{ID: 2, PeerURLs: []string{"192.0.2.3", "192.0.2.4"}},
|
||||
{ID: 3, PeerURLs: []string{"192.0.2.5", "192.0.2.6"}},
|
||||
{ID: 1, PeerURLs: []string{"192.0.2.1", "192.0.2.2"}},
|
||||
},
|
||||
},
|
||||
endpoints: []string{"http://192.0.2.1", "http://192.0.2.2", "http://192.0.2.3", "http://192.0.2.4", "http://192.0.2.5", "http://192.0.2.6"},
|
||||
},
|
||||
|
||||
// no members
|
||||
{
|
||||
clusterStore: &fakeCluster{members: []etcdserver.Member{}},
|
||||
endpoints: []string{},
|
||||
},
|
||||
|
||||
// peer with no endpoints
|
||||
{
|
||||
clusterStore: &fakeCluster{
|
||||
members: []etcdserver.Member{
|
||||
{ID: 3, PeerURLs: []string{}},
|
||||
},
|
||||
},
|
||||
endpoints: []string{},
|
||||
},
|
||||
}
|
||||
|
||||
for i, tt := range tests {
|
||||
endpoints := tt.clusterStore.Get().Endpoints()
|
||||
if !reflect.DeepEqual(tt.endpoints, endpoints) {
|
||||
t.Errorf("#%d: members.Endpoints() incorrect: want=%#v got=%#v", i, tt.endpoints, endpoints)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAllowMethod(t *testing.T) {
|
||||
tests := []struct {
|
||||
m string
|
||||
|
||||
Reference in New Issue
Block a user