From c3e35722bad23e13366c1186fd073ba60450e9ec Mon Sep 17 00:00:00 2001 From: Nishant Totla Date: Fri, 1 Apr 2016 00:07:56 -0700 Subject: [PATCH] Adding function to allow for updating client version Signed-off-by: Nishant Totla --- client/client.go | 6 ++++++ client/client_test.go | 47 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/client/client.go b/client/client.go index 0716667b..f91d235b 100644 --- a/client/client.go +++ b/client/client.go @@ -114,6 +114,12 @@ func (cli *Client) ClientVersion() string { return cli.version } +// UpdateClientVersion updates the version string associated with this +// instance of the Client. +func (cli *Client) UpdateClientVersion(v string) { + cli.version = v +} + // ParseHost verifies that the given host strings is valid. func ParseHost(host string) (string, string, string, error) { protoAddrParts := strings.SplitN(host, "://", 2) diff --git a/client/client_test.go b/client/client_test.go index a00b21b3..45698544 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -1,8 +1,16 @@ package client import ( + "bytes" + "encoding/json" + "io/ioutil" + "net/http" "net/url" + "strings" "testing" + + "github.com/docker/engine-api/types" + "golang.org/x/net/context" ) func TestGetAPIPath(t *testing.T) { @@ -70,3 +78,42 @@ func TestParseHost(t *testing.T) { } } } + +func TestUpdateClientVersion(t *testing.T) { + client := &Client{ + transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { + splitQuery := strings.Split(req.URL.Path, "/") + queryVersion := splitQuery[1] + b, err := json.Marshal(types.Version{ + APIVersion: queryVersion, + }) + if err != nil { + return nil, err + } + return &http.Response{ + StatusCode: http.StatusOK, + Body: ioutil.NopCloser(bytes.NewReader(b)), + }, nil + }), + } + + cases := []struct { + v string + }{ + {"1.20"}, + {"v1.21"}, + {"1.22"}, + {"v1.22"}, + } + + for _, cs := range cases { + client.UpdateClientVersion(cs.v) + r, err := client.ServerVersion(context.Background()) + if err != nil { + t.Fatal(err) + } + if strings.TrimPrefix(r.APIVersion, "v") != strings.TrimPrefix(cs.v, "v") { + t.Fatalf("Expected %s, got %s", cs.v, r.APIVersion) + } + } +}