2014-06-06 23:40:48 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
2014-06-23 18:32:11 +00:00
|
|
|
|
2014-06-06 23:40:48 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2014-07-25 19:28:20 +00:00
|
|
|
"encoding/json"
|
2014-06-07 11:38:16 +00:00
|
|
|
"net/http"
|
2014-06-06 23:40:48 +00:00
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
2014-08-28 13:56:38 +00:00
|
|
|
"path"
|
2014-06-06 23:40:48 +00:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
2014-06-20 16:47:39 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
|
2014-09-02 17:55:27 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
2014-06-06 23:40:48 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
2014-07-25 19:28:20 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/version"
|
2014-06-06 23:40:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// TODO: Move this to a common place, it's needed in multiple tests.
|
2014-08-28 13:56:38 +00:00
|
|
|
const apiPath = "/api/v1beta1"
|
2014-06-06 23:40:48 +00:00
|
|
|
|
2014-08-15 21:14:22 +00:00
|
|
|
func TestValidatesHostParameter(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
2014-08-28 13:56:38 +00:00
|
|
|
Host string
|
|
|
|
Prefix string
|
|
|
|
Err bool
|
2014-08-15 21:14:22 +00:00
|
|
|
}{
|
2014-08-28 13:56:38 +00:00
|
|
|
"127.0.0.1": {"http://127.0.0.1", "/api/v1beta1/", false},
|
|
|
|
"127.0.0.1:8080": {"http://127.0.0.1:8080", "/api/v1beta1/", false},
|
|
|
|
"foo.bar.com": {"http://foo.bar.com", "/api/v1beta1/", false},
|
|
|
|
"http://host/server": {"http://host", "/server/api/v1beta1/", false},
|
|
|
|
"host/server": {"", "", true},
|
2014-08-15 21:14:22 +00:00
|
|
|
}
|
|
|
|
for k, expected := range testCases {
|
2014-08-28 13:56:38 +00:00
|
|
|
c, err := NewRESTClient(k, nil, "/api/v1beta1/")
|
2014-08-15 21:14:22 +00:00
|
|
|
switch {
|
|
|
|
case err == nil && expected.Err:
|
|
|
|
t.Errorf("expected error but was nil")
|
|
|
|
continue
|
|
|
|
case err != nil && !expected.Err:
|
|
|
|
t.Errorf("unexpected error %v", err)
|
|
|
|
continue
|
2014-08-28 13:56:38 +00:00
|
|
|
case err != nil:
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if e, a := expected.Host, c.host; e != a {
|
|
|
|
t.Errorf("%s: expected host %s, got %s", k, e, a)
|
|
|
|
continue
|
2014-08-15 21:14:22 +00:00
|
|
|
}
|
2014-08-28 13:56:38 +00:00
|
|
|
if e, a := expected.Prefix, c.prefix; e != a {
|
|
|
|
t.Errorf("%s: expected prefix %s, got %s", k, e, a)
|
2014-08-15 21:14:22 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-09 05:38:45 +00:00
|
|
|
func TestListEmptyPods(t *testing.T) {
|
2014-06-24 04:55:11 +00:00
|
|
|
c := &testClient{
|
2014-06-22 21:18:01 +00:00
|
|
|
Request: testRequest{Method: "GET", Path: "/pods"},
|
2014-06-07 11:38:16 +00:00
|
|
|
Response: Response{StatusCode: 200, Body: api.PodList{}},
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2014-08-05 22:23:33 +00:00
|
|
|
podList, err := c.Setup().ListPods(labels.Everything())
|
2014-06-07 11:38:16 +00:00
|
|
|
c.Validate(t, podList, err)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
2014-06-09 05:38:45 +00:00
|
|
|
func TestListPods(t *testing.T) {
|
2014-06-24 04:55:11 +00:00
|
|
|
c := &testClient{
|
2014-06-22 21:18:01 +00:00
|
|
|
Request: testRequest{Method: "GET", Path: "/pods"},
|
2014-06-07 11:38:16 +00:00
|
|
|
Response: Response{StatusCode: 200,
|
|
|
|
Body: api.PodList{
|
|
|
|
Items: []api.Pod{
|
|
|
|
{
|
|
|
|
CurrentState: api.PodState{
|
|
|
|
Status: "Foobar",
|
|
|
|
},
|
|
|
|
Labels: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
"name": "baz",
|
|
|
|
},
|
|
|
|
},
|
2014-06-06 23:40:48 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2014-08-05 22:23:33 +00:00
|
|
|
receivedPodList, err := c.Setup().ListPods(labels.Everything())
|
2014-06-07 11:38:16 +00:00
|
|
|
c.Validate(t, receivedPodList, err)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
2014-06-20 16:47:39 +00:00
|
|
|
func validateLabels(a, b string) bool {
|
|
|
|
sA, _ := labels.ParseSelector(a)
|
|
|
|
sB, _ := labels.ParseSelector(b)
|
|
|
|
return sA.String() == sB.String()
|
|
|
|
}
|
|
|
|
|
2014-06-09 05:38:45 +00:00
|
|
|
func TestListPodsLabels(t *testing.T) {
|
2014-06-24 04:55:11 +00:00
|
|
|
c := &testClient{
|
2014-06-22 21:18:01 +00:00
|
|
|
Request: testRequest{Method: "GET", Path: "/pods", Query: url.Values{"labels": []string{"foo=bar,name=baz"}}},
|
2014-06-07 11:38:16 +00:00
|
|
|
Response: Response{
|
|
|
|
StatusCode: 200,
|
|
|
|
Body: api.PodList{
|
|
|
|
Items: []api.Pod{
|
|
|
|
{
|
|
|
|
CurrentState: api.PodState{
|
|
|
|
Status: "Foobar",
|
|
|
|
},
|
|
|
|
Labels: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
"name": "baz",
|
|
|
|
},
|
|
|
|
},
|
2014-06-06 23:40:48 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2014-06-20 16:47:39 +00:00
|
|
|
c.Setup()
|
|
|
|
c.QueryValidator["labels"] = validateLabels
|
2014-06-23 00:02:48 +00:00
|
|
|
selector := labels.Set{"foo": "bar", "name": "baz"}.AsSelector()
|
2014-06-20 16:47:39 +00:00
|
|
|
receivedPodList, err := c.ListPods(selector)
|
2014-06-07 11:38:16 +00:00
|
|
|
c.Validate(t, receivedPodList, err)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
2014-06-09 05:38:45 +00:00
|
|
|
func TestGetPod(t *testing.T) {
|
2014-06-24 04:55:11 +00:00
|
|
|
c := &testClient{
|
2014-06-22 21:18:01 +00:00
|
|
|
Request: testRequest{Method: "GET", Path: "/pods/foo"},
|
2014-06-07 11:38:16 +00:00
|
|
|
Response: Response{
|
|
|
|
StatusCode: 200,
|
|
|
|
Body: api.Pod{
|
|
|
|
CurrentState: api.PodState{
|
|
|
|
Status: "Foobar",
|
|
|
|
},
|
|
|
|
Labels: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
"name": "baz",
|
|
|
|
},
|
|
|
|
},
|
2014-06-06 23:40:48 +00:00
|
|
|
},
|
|
|
|
}
|
2014-06-07 11:38:16 +00:00
|
|
|
receivedPod, err := c.Setup().GetPod("foo")
|
|
|
|
c.Validate(t, receivedPod, err)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
2014-06-09 05:38:45 +00:00
|
|
|
func TestDeletePod(t *testing.T) {
|
2014-06-24 04:55:11 +00:00
|
|
|
c := &testClient{
|
2014-06-22 21:18:01 +00:00
|
|
|
Request: testRequest{Method: "DELETE", Path: "/pods/foo"},
|
2014-06-07 11:38:16 +00:00
|
|
|
Response: Response{StatusCode: 200},
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2014-06-07 11:38:16 +00:00
|
|
|
err := c.Setup().DeletePod("foo")
|
|
|
|
c.Validate(t, nil, err)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
2014-06-09 05:38:45 +00:00
|
|
|
func TestCreatePod(t *testing.T) {
|
|
|
|
requestPod := api.Pod{
|
2014-06-09 04:39:57 +00:00
|
|
|
CurrentState: api.PodState{
|
2014-06-06 23:40:48 +00:00
|
|
|
Status: "Foobar",
|
|
|
|
},
|
|
|
|
Labels: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
"name": "baz",
|
|
|
|
},
|
|
|
|
}
|
2014-06-24 04:55:11 +00:00
|
|
|
c := &testClient{
|
2014-06-22 21:18:01 +00:00
|
|
|
Request: testRequest{Method: "POST", Path: "/pods", Body: requestPod},
|
2014-06-07 11:38:16 +00:00
|
|
|
Response: Response{
|
|
|
|
StatusCode: 200,
|
|
|
|
Body: requestPod,
|
|
|
|
},
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2014-06-07 11:38:16 +00:00
|
|
|
receivedPod, err := c.Setup().CreatePod(requestPod)
|
|
|
|
c.Validate(t, receivedPod, err)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
2014-06-09 05:38:45 +00:00
|
|
|
func TestUpdatePod(t *testing.T) {
|
|
|
|
requestPod := api.Pod{
|
2014-08-01 21:14:33 +00:00
|
|
|
JSONBase: api.JSONBase{ID: "foo", ResourceVersion: 1},
|
2014-06-09 04:39:57 +00:00
|
|
|
CurrentState: api.PodState{
|
2014-06-06 23:40:48 +00:00
|
|
|
Status: "Foobar",
|
|
|
|
},
|
|
|
|
Labels: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
"name": "baz",
|
|
|
|
},
|
|
|
|
}
|
2014-06-24 04:55:11 +00:00
|
|
|
c := &testClient{
|
2014-06-22 21:18:01 +00:00
|
|
|
Request: testRequest{Method: "PUT", Path: "/pods/foo"},
|
2014-06-07 11:38:16 +00:00
|
|
|
Response: Response{StatusCode: 200, Body: requestPod},
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2014-06-07 11:38:16 +00:00
|
|
|
receivedPod, err := c.Setup().UpdatePod(requestPod)
|
|
|
|
c.Validate(t, receivedPod, err)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
2014-07-31 14:59:54 +00:00
|
|
|
func TestListControllers(t *testing.T) {
|
|
|
|
c := &testClient{
|
|
|
|
Request: testRequest{Method: "GET", Path: "/replicationControllers"},
|
|
|
|
Response: Response{StatusCode: 200,
|
|
|
|
Body: api.ReplicationControllerList{
|
|
|
|
Items: []api.ReplicationController{
|
|
|
|
{
|
|
|
|
JSONBase: api.JSONBase{ID: "foo"},
|
|
|
|
DesiredState: api.ReplicationControllerState{
|
|
|
|
Replicas: 2,
|
|
|
|
},
|
|
|
|
Labels: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
"name": "baz",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2014-08-05 22:23:33 +00:00
|
|
|
receivedControllerList, err := c.Setup().ListReplicationControllers(labels.Everything())
|
2014-07-31 14:59:54 +00:00
|
|
|
c.Validate(t, receivedControllerList, err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-06-07 11:38:16 +00:00
|
|
|
func TestGetController(t *testing.T) {
|
2014-06-24 04:55:11 +00:00
|
|
|
c := &testClient{
|
2014-06-22 21:18:01 +00:00
|
|
|
Request: testRequest{Method: "GET", Path: "/replicationControllers/foo"},
|
2014-06-07 11:38:16 +00:00
|
|
|
Response: Response{
|
|
|
|
StatusCode: 200,
|
|
|
|
Body: api.ReplicationController{
|
2014-07-16 21:30:28 +00:00
|
|
|
JSONBase: api.JSONBase{ID: "foo"},
|
2014-06-07 11:38:16 +00:00
|
|
|
DesiredState: api.ReplicationControllerState{
|
|
|
|
Replicas: 2,
|
|
|
|
},
|
|
|
|
Labels: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
"name": "baz",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2014-06-07 11:38:16 +00:00
|
|
|
receivedController, err := c.Setup().GetReplicationController("foo")
|
|
|
|
c.Validate(t, receivedController, err)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
2014-06-07 11:38:16 +00:00
|
|
|
func TestUpdateController(t *testing.T) {
|
|
|
|
requestController := api.ReplicationController{
|
2014-08-01 21:14:33 +00:00
|
|
|
JSONBase: api.JSONBase{ID: "foo", ResourceVersion: 1},
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2014-06-24 04:55:11 +00:00
|
|
|
c := &testClient{
|
2014-06-22 21:18:01 +00:00
|
|
|
Request: testRequest{Method: "PUT", Path: "/replicationControllers/foo"},
|
2014-06-07 11:38:16 +00:00
|
|
|
Response: Response{
|
|
|
|
StatusCode: 200,
|
|
|
|
Body: api.ReplicationController{
|
2014-07-16 21:30:28 +00:00
|
|
|
JSONBase: api.JSONBase{ID: "foo"},
|
2014-06-07 11:38:16 +00:00
|
|
|
DesiredState: api.ReplicationControllerState{
|
|
|
|
Replicas: 2,
|
|
|
|
},
|
|
|
|
Labels: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
"name": "baz",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2014-06-07 11:38:16 +00:00
|
|
|
receivedController, err := c.Setup().UpdateReplicationController(requestController)
|
|
|
|
c.Validate(t, receivedController, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteController(t *testing.T) {
|
2014-06-24 04:55:11 +00:00
|
|
|
c := &testClient{
|
2014-06-22 21:18:01 +00:00
|
|
|
Request: testRequest{Method: "DELETE", Path: "/replicationControllers/foo"},
|
2014-06-07 11:38:16 +00:00
|
|
|
Response: Response{StatusCode: 200},
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2014-06-07 11:38:16 +00:00
|
|
|
err := c.Setup().DeleteReplicationController("foo")
|
|
|
|
c.Validate(t, nil, err)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
2014-06-07 11:38:16 +00:00
|
|
|
func TestCreateController(t *testing.T) {
|
|
|
|
requestController := api.ReplicationController{
|
2014-07-16 21:30:28 +00:00
|
|
|
JSONBase: api.JSONBase{ID: "foo"},
|
2014-06-07 11:38:16 +00:00
|
|
|
}
|
2014-06-24 04:55:11 +00:00
|
|
|
c := &testClient{
|
2014-06-22 21:18:01 +00:00
|
|
|
Request: testRequest{Method: "POST", Path: "/replicationControllers", Body: requestController},
|
2014-06-07 11:38:16 +00:00
|
|
|
Response: Response{
|
|
|
|
StatusCode: 200,
|
|
|
|
Body: api.ReplicationController{
|
2014-07-16 21:30:28 +00:00
|
|
|
JSONBase: api.JSONBase{ID: "foo"},
|
2014-06-07 11:38:16 +00:00
|
|
|
DesiredState: api.ReplicationControllerState{
|
|
|
|
Replicas: 2,
|
|
|
|
},
|
|
|
|
Labels: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
"name": "baz",
|
|
|
|
},
|
|
|
|
},
|
2014-06-06 23:40:48 +00:00
|
|
|
},
|
|
|
|
}
|
2014-06-07 11:38:16 +00:00
|
|
|
receivedController, err := c.Setup().CreateReplicationController(requestController)
|
|
|
|
c.Validate(t, receivedController, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func body(obj interface{}, raw *string) *string {
|
|
|
|
if obj != nil {
|
2014-09-06 01:47:09 +00:00
|
|
|
bs, _ := runtime.DefaultCodec.Encode(obj)
|
2014-06-07 11:38:16 +00:00
|
|
|
body := string(bs)
|
|
|
|
return &body
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2014-06-07 11:38:16 +00:00
|
|
|
return raw
|
|
|
|
}
|
|
|
|
|
2014-06-22 21:18:01 +00:00
|
|
|
type testRequest struct {
|
2014-06-07 11:38:16 +00:00
|
|
|
Method string
|
|
|
|
Path string
|
|
|
|
Header string
|
|
|
|
Query url.Values
|
|
|
|
Body interface{}
|
|
|
|
RawBody *string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Response struct {
|
|
|
|
StatusCode int
|
|
|
|
Body interface{}
|
|
|
|
RawBody *string
|
|
|
|
}
|
|
|
|
|
2014-06-24 04:55:11 +00:00
|
|
|
type testClient struct {
|
2014-06-07 11:38:16 +00:00
|
|
|
*Client
|
2014-06-22 21:18:01 +00:00
|
|
|
Request testRequest
|
2014-06-07 11:38:16 +00:00
|
|
|
Response Response
|
|
|
|
Error bool
|
|
|
|
server *httptest.Server
|
|
|
|
handler *util.FakeHandler
|
|
|
|
Target interface{}
|
2014-06-20 16:47:39 +00:00
|
|
|
// For query args, an optional function to validate the contents
|
|
|
|
// useful when the contents can change but still be correct.
|
|
|
|
// Maps from query arg key to validator.
|
|
|
|
// If no validator is present, string equality is used.
|
|
|
|
QueryValidator map[string]func(string, string) bool
|
2014-06-07 11:38:16 +00:00
|
|
|
}
|
|
|
|
|
2014-06-24 04:55:11 +00:00
|
|
|
func (c *testClient) Setup() *testClient {
|
2014-06-07 11:38:16 +00:00
|
|
|
c.handler = &util.FakeHandler{
|
|
|
|
StatusCode: c.Response.StatusCode,
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2014-06-07 11:38:16 +00:00
|
|
|
if responseBody := body(c.Response.Body, c.Response.RawBody); responseBody != nil {
|
|
|
|
c.handler.ResponseBody = *responseBody
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2014-08-05 04:59:42 +00:00
|
|
|
c.server = httptest.NewServer(c.handler)
|
2014-06-07 11:38:16 +00:00
|
|
|
if c.Client == nil {
|
2014-08-28 13:56:38 +00:00
|
|
|
c.Client = NewOrDie("localhost", nil)
|
2014-06-07 11:38:16 +00:00
|
|
|
}
|
2014-06-22 21:18:01 +00:00
|
|
|
c.Client.host = c.server.URL
|
2014-08-28 13:56:38 +00:00
|
|
|
c.Client.prefix = "/api/v1beta1/"
|
2014-06-20 16:47:39 +00:00
|
|
|
c.QueryValidator = map[string]func(string, string) bool{}
|
2014-06-07 11:38:16 +00:00
|
|
|
return c
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
2014-06-24 04:55:11 +00:00
|
|
|
func (c *testClient) Validate(t *testing.T, received interface{}, err error) {
|
2014-06-07 11:38:16 +00:00
|
|
|
defer c.server.Close()
|
|
|
|
|
|
|
|
if c.Error {
|
|
|
|
if err == nil {
|
2014-07-18 19:03:22 +00:00
|
|
|
t.Errorf("error expected for %#v, got none", c.Request)
|
2014-06-07 11:38:16 +00:00
|
|
|
}
|
|
|
|
return
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
2014-06-07 11:38:16 +00:00
|
|
|
t.Errorf("no error expected for %#v, got: %v", c.Request, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
requestBody := body(c.Request.Body, c.Request.RawBody)
|
2014-08-20 22:30:09 +00:00
|
|
|
actualQuery := c.handler.RequestReceived.URL.Query()
|
|
|
|
// We check the query manually, so blank it out so that FakeHandler.ValidateRequest
|
|
|
|
// won't check it.
|
|
|
|
c.handler.RequestReceived.URL.RawQuery = ""
|
2014-08-28 13:56:38 +00:00
|
|
|
c.handler.ValidateRequest(t, path.Join(apiPath, c.Request.Path), c.Request.Method, requestBody)
|
2014-06-20 16:47:39 +00:00
|
|
|
for key, values := range c.Request.Query {
|
|
|
|
validator, ok := c.QueryValidator[key]
|
|
|
|
if !ok {
|
|
|
|
validator = func(a, b string) bool { return a == b }
|
|
|
|
}
|
2014-08-20 22:30:09 +00:00
|
|
|
observed := actualQuery.Get(key)
|
2014-06-20 16:47:39 +00:00
|
|
|
if !validator(values[0], observed) {
|
|
|
|
t.Errorf("Unexpected query arg for key: %s. Expected %s, Received %s", key, values[0], observed)
|
|
|
|
}
|
2014-06-07 11:38:16 +00:00
|
|
|
}
|
|
|
|
if c.Request.Header != "" {
|
|
|
|
if c.handler.RequestReceived.Header.Get(c.Request.Header) == "" {
|
|
|
|
t.Errorf("header %q not found in request %#v", c.Request.Header, c.handler.RequestReceived)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if expected, received := requestBody, c.handler.RequestBody; expected != nil && *expected != received {
|
2014-08-14 17:51:17 +00:00
|
|
|
t.Errorf("bad body for request %#v: expected %s, got %s", c.Request, *expected, received)
|
2014-06-07 11:38:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.Response.Body != nil && !reflect.DeepEqual(c.Response.Body, received) {
|
2014-07-18 19:03:22 +00:00
|
|
|
t.Errorf("bad response for request %#v: expected %s, got %s", c.Request, c.Response.Body, received)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-28 04:32:52 +00:00
|
|
|
func TestListServices(t *testing.T) {
|
|
|
|
c := &testClient{
|
|
|
|
Request: testRequest{Method: "GET", Path: "/services"},
|
|
|
|
Response: Response{StatusCode: 200,
|
|
|
|
Body: api.ServiceList{
|
|
|
|
Items: []api.Service{
|
|
|
|
{
|
|
|
|
JSONBase: api.JSONBase{ID: "name"},
|
|
|
|
Labels: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
"name": "baz",
|
|
|
|
},
|
|
|
|
Selector: map[string]string{
|
|
|
|
"one": "two",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
receivedServiceList, err := c.Setup().ListServices(labels.Everything())
|
|
|
|
c.Validate(t, receivedServiceList, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestListServicesLabels(t *testing.T) {
|
|
|
|
c := &testClient{
|
|
|
|
Request: testRequest{Method: "GET", Path: "/services", Query: url.Values{"labels": []string{"foo=bar,name=baz"}}},
|
|
|
|
Response: Response{StatusCode: 200,
|
|
|
|
Body: api.ServiceList{
|
|
|
|
Items: []api.Service{
|
|
|
|
{
|
|
|
|
JSONBase: api.JSONBase{ID: "name"},
|
|
|
|
Labels: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
"name": "baz",
|
|
|
|
},
|
|
|
|
Selector: map[string]string{
|
|
|
|
"one": "two",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
c.Setup()
|
|
|
|
c.QueryValidator["labels"] = validateLabels
|
|
|
|
selector := labels.Set{"foo": "bar", "name": "baz"}.AsSelector()
|
|
|
|
receivedServiceList, err := c.ListServices(selector)
|
|
|
|
c.Validate(t, receivedServiceList, err)
|
|
|
|
}
|
|
|
|
|
2014-06-07 11:38:16 +00:00
|
|
|
func TestGetService(t *testing.T) {
|
2014-06-24 04:55:11 +00:00
|
|
|
c := &testClient{
|
2014-06-22 21:18:01 +00:00
|
|
|
Request: testRequest{Method: "GET", Path: "/services/1"},
|
2014-06-07 11:38:16 +00:00
|
|
|
Response: Response{StatusCode: 200, Body: &api.Service{JSONBase: api.JSONBase{ID: "service-1"}}},
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2014-06-07 11:38:16 +00:00
|
|
|
response, err := c.Setup().GetService("1")
|
|
|
|
c.Validate(t, &response, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateService(t *testing.T) {
|
2014-06-24 04:55:11 +00:00
|
|
|
c := (&testClient{
|
2014-06-22 21:18:01 +00:00
|
|
|
Request: testRequest{Method: "POST", Path: "/services", Body: &api.Service{JSONBase: api.JSONBase{ID: "service-1"}}},
|
2014-06-07 11:38:16 +00:00
|
|
|
Response: Response{StatusCode: 200, Body: &api.Service{JSONBase: api.JSONBase{ID: "service-1"}}},
|
|
|
|
}).Setup()
|
|
|
|
response, err := c.Setup().CreateService(api.Service{JSONBase: api.JSONBase{ID: "service-1"}})
|
|
|
|
c.Validate(t, &response, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateService(t *testing.T) {
|
2014-08-01 21:14:33 +00:00
|
|
|
svc := api.Service{JSONBase: api.JSONBase{ID: "service-1", ResourceVersion: 1}}
|
2014-06-24 04:55:11 +00:00
|
|
|
c := &testClient{
|
2014-08-01 21:14:33 +00:00
|
|
|
Request: testRequest{Method: "PUT", Path: "/services/service-1", Body: &svc},
|
|
|
|
Response: Response{StatusCode: 200, Body: &svc},
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2014-08-01 21:14:33 +00:00
|
|
|
response, err := c.Setup().UpdateService(svc)
|
2014-06-07 11:38:16 +00:00
|
|
|
c.Validate(t, &response, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteService(t *testing.T) {
|
2014-06-24 04:55:11 +00:00
|
|
|
c := &testClient{
|
2014-06-22 21:18:01 +00:00
|
|
|
Request: testRequest{Method: "DELETE", Path: "/services/1"},
|
2014-06-07 11:38:16 +00:00
|
|
|
Response: Response{StatusCode: 200},
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2014-06-07 11:38:16 +00:00
|
|
|
err := c.Setup().DeleteService("1")
|
|
|
|
c.Validate(t, nil, err)
|
|
|
|
}
|
|
|
|
|
2014-08-28 13:56:38 +00:00
|
|
|
func TestDoRequest(t *testing.T) {
|
|
|
|
invalid := "aaaaa"
|
2014-06-24 04:55:11 +00:00
|
|
|
testClients := []testClient{
|
2014-08-28 13:56:38 +00:00
|
|
|
{Request: testRequest{Method: "GET", Path: "good"}, Response: Response{StatusCode: 200}},
|
|
|
|
{Request: testRequest{Method: "GET", Path: "bad%ZZ"}, Error: true},
|
|
|
|
{Client: NewOrDie("localhost", &AuthInfo{"foo", "bar"}), Request: testRequest{Method: "GET", Path: "auth", Header: "Authorization"}, Response: Response{StatusCode: 200}},
|
|
|
|
{Client: &Client{&RESTClient{httpClient: http.DefaultClient}}, Request: testRequest{Method: "GET", Path: "nocertificate"}, Error: true},
|
|
|
|
{Request: testRequest{Method: "GET", Path: "error"}, Response: Response{StatusCode: 500}, Error: true},
|
|
|
|
{Request: testRequest{Method: "POST", Path: "faildecode"}, Response: Response{StatusCode: 200, RawBody: &invalid}, Target: &struct{}{}},
|
|
|
|
{Request: testRequest{Method: "GET", Path: "failread"}, Response: Response{StatusCode: 200, RawBody: &invalid}, Target: &struct{}{}},
|
2014-06-07 11:38:16 +00:00
|
|
|
}
|
|
|
|
for _, c := range testClients {
|
2014-08-28 13:56:38 +00:00
|
|
|
client := c.Setup()
|
|
|
|
prefix, _ := url.Parse(client.host)
|
|
|
|
prefix.Path = client.prefix + c.Request.Path
|
|
|
|
request := &http.Request{
|
|
|
|
Method: c.Request.Method,
|
|
|
|
Header: make(http.Header),
|
|
|
|
URL: prefix,
|
|
|
|
}
|
|
|
|
response, err := client.doRequest(request)
|
2014-06-07 11:38:16 +00:00
|
|
|
c.Validate(t, response, err)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
2014-06-22 21:18:01 +00:00
|
|
|
|
2014-06-23 01:14:32 +00:00
|
|
|
func TestDoRequestAccepted(t *testing.T) {
|
|
|
|
status := api.Status{Status: api.StatusWorking}
|
2014-09-06 01:47:09 +00:00
|
|
|
expectedBody, _ := runtime.DefaultCodec.Encode(status)
|
2014-06-23 01:14:32 +00:00
|
|
|
fakeHandler := util.FakeHandler{
|
|
|
|
StatusCode: 202,
|
|
|
|
ResponseBody: string(expectedBody),
|
|
|
|
T: t,
|
|
|
|
}
|
2014-08-05 04:59:42 +00:00
|
|
|
testServer := httptest.NewServer(&fakeHandler)
|
2014-06-23 01:14:32 +00:00
|
|
|
request, _ := http.NewRequest("GET", testServer.URL+"/foo/bar", nil)
|
|
|
|
auth := AuthInfo{User: "user", Password: "pass"}
|
2014-08-28 13:56:38 +00:00
|
|
|
c, err := New(testServer.URL, &auth)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
2014-06-23 01:14:32 +00:00
|
|
|
body, err := c.doRequest(request)
|
|
|
|
if request.Header["Authorization"] == nil {
|
|
|
|
t.Errorf("Request is missing authorization header: %#v", *request)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
t.Error("Unexpected non-error")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
se, ok := err.(*StatusErr)
|
|
|
|
if !ok {
|
|
|
|
t.Errorf("Unexpected kind of error: %#v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(se.Status, status) {
|
|
|
|
t.Errorf("Unexpected status: %#v", se.Status)
|
|
|
|
}
|
|
|
|
if body != nil {
|
|
|
|
t.Errorf("Expected nil body, but saw: '%s'", body)
|
|
|
|
}
|
|
|
|
fakeHandler.ValidateRequest(t, "/foo/bar", "GET", nil)
|
|
|
|
}
|
2014-06-24 21:57:09 +00:00
|
|
|
|
|
|
|
func TestDoRequestAcceptedSuccess(t *testing.T) {
|
|
|
|
status := api.Status{Status: api.StatusSuccess}
|
2014-09-06 01:47:09 +00:00
|
|
|
expectedBody, _ := runtime.DefaultCodec.Encode(status)
|
2014-06-24 21:57:09 +00:00
|
|
|
fakeHandler := util.FakeHandler{
|
|
|
|
StatusCode: 202,
|
|
|
|
ResponseBody: string(expectedBody),
|
|
|
|
T: t,
|
|
|
|
}
|
2014-08-05 04:59:42 +00:00
|
|
|
testServer := httptest.NewServer(&fakeHandler)
|
2014-06-24 21:57:09 +00:00
|
|
|
request, _ := http.NewRequest("GET", testServer.URL+"/foo/bar", nil)
|
|
|
|
auth := AuthInfo{User: "user", Password: "pass"}
|
2014-08-28 13:56:38 +00:00
|
|
|
c, err := New(testServer.URL, &auth)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
2014-06-24 21:57:09 +00:00
|
|
|
body, err := c.doRequest(request)
|
|
|
|
if request.Header["Authorization"] == nil {
|
|
|
|
t.Errorf("Request is missing authorization header: %#v", *request)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error %#v", err)
|
|
|
|
}
|
2014-09-06 01:47:09 +00:00
|
|
|
statusOut, err := runtime.DefaultCodec.Decode(body)
|
2014-06-24 21:57:09 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error %#v", err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(&status, statusOut) {
|
|
|
|
t.Errorf("Unexpected mis-match. Expected %#v. Saw %#v", status, statusOut)
|
|
|
|
}
|
|
|
|
fakeHandler.ValidateRequest(t, "/foo/bar", "GET", nil)
|
|
|
|
}
|
2014-07-25 19:28:20 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}))
|
2014-08-28 13:56:38 +00:00
|
|
|
client := NewOrDie(server.URL, nil)
|
2014-07-25 19:28:20 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2014-08-27 21:07:22 +00:00
|
|
|
|
|
|
|
func TestListMinions(t *testing.T) {
|
|
|
|
c := &testClient{
|
|
|
|
Request: testRequest{Method: "GET", Path: "/minions"},
|
|
|
|
Response: Response{StatusCode: 200, Body: &api.MinionList{JSONBase: api.JSONBase{ID: "minion-1"}}},
|
|
|
|
}
|
|
|
|
response, err := c.Setup().ListMinions()
|
|
|
|
c.Validate(t, &response, err)
|
|
|
|
}
|