Skip to content

Commit

Permalink
client: add GetVersion method for retrieving etcdserver and etcdclust…
Browse files Browse the repository at this point in the history
…er version

this adds a GetVersion method to the Client interface for retrieving
information about the etcdserver and etcdcluster.
  • Loading branch information
shaunthium committed Sep 22, 2016
1 parent c6bfdb9 commit 00ee3e7
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
44 changes: 44 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package client

import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -202,6 +203,9 @@ type Client interface {
// returned
SetEndpoints(eps []string) error

// GetVersion retrieves the current etcd server and cluster version
GetVersion(ctx context.Context) (*VersionResponse, error)

httpClient
}

Expand Down Expand Up @@ -440,6 +444,46 @@ func (c *httpClusterClient) AutoSync(ctx context.Context, interval time.Duration
}
}

type VersionResponse struct {
// ServerVersion represents the version of the current server
// that the client is running on.
ServerVersion string `json:"server_version"`

// ClusterVersion represents the cluster architecture version
// of the current cluster.
ClusterVersion string `json:"cluster_version"`
}

func (c *httpClusterClient) GetVersion(ctx context.Context) (*VersionResponse, error) {
act := &getAction{Prefix: "/version"}

resp, body, err := c.Do(ctx, act)
if err != nil {
return nil, err
}

switch resp.StatusCode {
case http.StatusOK:
if len(body) == 0 {
return nil, ErrEmptyBody
}
var m map[string]string
if err := json.Unmarshal(body, &m); err != nil {
return nil, ErrInvalidJSON
}
return &VersionResponse{
ServerVersion: m["etcdserver"],
ClusterVersion: m["etcdcluster"],
}, nil
default:
var etcdErr Error
if err := json.Unmarshal(body, &etcdErr); err != nil {
return nil, ErrInvalidJSON
}
return nil, etcdErr
}
}

type roundTripResponse struct {
resp *http.Response
err error
Expand Down
31 changes: 31 additions & 0 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,37 @@ func TestHTTPClusterClientAutoSyncFail(t *testing.T) {
}
}

func TestHTTPClusterClientGetVersion(t *testing.T) {
body := []byte(`{"etcdserver":"2.3.2","etcdcluster":"2.3.0"}`)
cf := newStaticHTTPClientFactory([]staticHTTPResponse{
{
resp: http.Response{StatusCode: http.StatusOK, Header: http.Header{"Content-Length": []string{"44"}}},
body: body,
},
})

hc := &httpClusterClient{
clientFactory: cf,
rand: rand.New(rand.NewSource(0)),
}
err := hc.SetEndpoints([]string{"http://127.0.0.1:4003", "http://127.0.0.1:2379", "http://127.0.0.1:4001", "http://127.0.0.1:4002"})
if err != nil {
t.Fatalf("unexpected error during setup: %#v", err)
}

actual, err := hc.GetVersion(context.Background())
if err != nil {
t.Errorf("non-nil error: %#v", err)
}
expected := &VersionResponse{
ServerVersion: "2.3.2",
ClusterVersion: "2.3.0",
}
if !reflect.DeepEqual(expected, actual) {
t.Errorf("incorrect Response: want=%#v got=%#v", expected, actual)
}
}

// TestHTTPClusterClientSyncPinEndpoint tests that Sync() pins the endpoint when
// it gets the exactly same member list as before.
func TestHTTPClusterClientSyncPinEndpoint(t *testing.T) {
Expand Down

0 comments on commit 00ee3e7

Please sign in to comment.