Add /version to server and check it in client.

Will help detect client/version skew and prevent e2e test from passing
while running a version other than the one you think it's running.
This commit is contained in:
Daniel Smith
2014-07-25 12:28:20 -07:00
parent 9fc52c8aaa
commit 3b8488028d
9 changed files with 148 additions and 31 deletions

View File

@@ -18,6 +18,7 @@ package client
import (
"crypto/tls"
"encoding/json"
"fmt"
"io"
"io/ioutil"
@@ -26,6 +27,7 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/GoogleCloudPlatform/kubernetes/pkg/version"
"github.com/golang/glog"
)
@@ -238,3 +240,17 @@ func (c *Client) UpdateService(svc api.Service) (result api.Service, err error)
func (c *Client) DeleteService(name string) error {
return c.Delete().Path("services").Path(name).Do().Error()
}
// ServerVersion retrieves and parses the server's version.
func (c *Client) ServerVersion() (*version.Info, error) {
body, err := c.Get().AbsPath("/version").Do().Raw()
if err != nil {
return nil, err
}
var info version.Info
err = json.Unmarshal(body, &info)
if err != nil {
return nil, fmt.Errorf("Got '%s': %v", string(body), err)
}
return &info, nil
}

View File

@@ -17,6 +17,7 @@ limitations under the License.
package client
import (
"encoding/json"
"net/http"
"net/http/httptest"
"net/url"
@@ -26,6 +27,7 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/GoogleCloudPlatform/kubernetes/pkg/version"
)
// TODO: Move this to a common place, it's needed in multiple tests.
@@ -456,3 +458,30 @@ func TestDoRequestAcceptedSuccess(t *testing.T) {
}
fakeHandler.ValidateRequest(t, "/foo/bar", "GET", nil)
}
func TestGetServerVersion(t *testing.T) {
expect := version.Info{
Major: "foo",
Minor: "bar",
GitCommit: "baz",
}
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
output, err := json.Marshal(expect)
if err != nil {
t.Errorf("unexpected encoding error: %v", err)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(output)
}))
client := New(server.URL, nil)
got, err := client.ServerVersion()
if err != nil {
t.Fatalf("unexpected encoding error: %v", err)
}
if e, a := expect, *got; !reflect.DeepEqual(e, a) {
t.Errorf("expected %v, got %v", e, a)
}
}