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"
|
2014-11-14 03:09:03 +00:00
|
|
|
"strings"
|
2014-06-06 23:40:48 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
2014-09-11 17:02:53 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
|
2014-06-20 16:47:39 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
|
2014-10-22 01:39:13 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/resources"
|
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-09-30 00:15:00 +00:00
|
|
|
type testRequest struct {
|
|
|
|
Method string
|
|
|
|
Path string
|
|
|
|
Header string
|
|
|
|
Query url.Values
|
|
|
|
Body runtime.Object
|
|
|
|
RawBody *string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Response struct {
|
|
|
|
StatusCode int
|
|
|
|
Body runtime.Object
|
|
|
|
RawBody *string
|
|
|
|
}
|
|
|
|
|
|
|
|
type testClient struct {
|
|
|
|
*Client
|
|
|
|
Request testRequest
|
|
|
|
Response Response
|
|
|
|
Error bool
|
2014-10-24 17:16:02 +00:00
|
|
|
Created bool
|
2014-09-30 00:15:00 +00:00
|
|
|
server *httptest.Server
|
|
|
|
handler *util.FakeHandler
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *testClient) Setup() *testClient {
|
|
|
|
c.handler = &util.FakeHandler{
|
|
|
|
StatusCode: c.Response.StatusCode,
|
|
|
|
}
|
|
|
|
if responseBody := body(c.Response.Body, c.Response.RawBody); responseBody != nil {
|
|
|
|
c.handler.ResponseBody = *responseBody
|
|
|
|
}
|
|
|
|
c.server = httptest.NewServer(c.handler)
|
|
|
|
if c.Client == nil {
|
|
|
|
c.Client = NewOrDie(&Config{
|
|
|
|
Host: c.server.URL,
|
|
|
|
Version: "v1beta1",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
c.QueryValidator = map[string]func(string, string) bool{}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *testClient) Validate(t *testing.T, received runtime.Object, err error) {
|
|
|
|
c.ValidateCommon(t, err)
|
|
|
|
|
|
|
|
if c.Response.Body != nil && !reflect.DeepEqual(c.Response.Body, received) {
|
2014-11-06 22:53:28 +00:00
|
|
|
t.Errorf("bad response for request %#v: expected %#v, got %#v", c.Request, c.Response.Body, received)
|
2014-09-11 23:01:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-30 00:15:00 +00:00
|
|
|
func (c *testClient) ValidateRaw(t *testing.T, received []byte, err error) {
|
|
|
|
c.ValidateCommon(t, err)
|
|
|
|
|
|
|
|
if c.Response.Body != nil && !reflect.DeepEqual(c.Response.Body, received) {
|
2014-11-06 22:53:28 +00:00
|
|
|
t.Errorf("bad response for request %#v: expected %#v, got %#v", c.Request, c.Response.Body, received)
|
2014-09-30 00:15:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *testClient) ValidateCommon(t *testing.T, err error) {
|
|
|
|
defer c.server.Close()
|
|
|
|
|
|
|
|
if c.Error {
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("error expected for %#v, got none", c.Request)
|
2014-08-28 13:56:38 +00:00
|
|
|
}
|
2014-09-30 00:15:00 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("no error expected for %#v, got: %v", c.Request, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.handler.RequestReceived == nil {
|
|
|
|
t.Errorf("handler had an empty request, %#v", c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
requestBody := body(c.Request.Body, c.Request.RawBody)
|
|
|
|
actualQuery := c.handler.RequestReceived.URL.Query()
|
2014-11-14 03:09:03 +00:00
|
|
|
t.Logf("got query: %v", actualQuery)
|
2014-09-30 00:15:00 +00:00
|
|
|
// We check the query manually, so blank it out so that FakeHandler.ValidateRequest
|
|
|
|
// won't check it.
|
|
|
|
c.handler.RequestReceived.URL.RawQuery = ""
|
|
|
|
c.handler.ValidateRequest(t, path.Join(apiPath, c.Request.Path), c.Request.Method, requestBody)
|
|
|
|
for key, values := range c.Request.Query {
|
|
|
|
validator, ok := c.QueryValidator[key]
|
|
|
|
if !ok {
|
2014-11-14 03:09:03 +00:00
|
|
|
switch key {
|
|
|
|
case "labels", "fields":
|
|
|
|
validator = validateLabels
|
|
|
|
default:
|
|
|
|
validator = func(a, b string) bool { return a == b }
|
|
|
|
}
|
2014-08-15 21:14:22 +00:00
|
|
|
}
|
2014-09-30 00:15:00 +00:00
|
|
|
observed := actualQuery.Get(key)
|
2014-11-14 03:09:03 +00:00
|
|
|
wanted := strings.Join(values, "")
|
|
|
|
if !validator(wanted, observed) {
|
|
|
|
t.Errorf("Unexpected query arg for key: %s. Expected %s, Received %s", key, wanted, observed)
|
2014-09-30 00:15:00 +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)
|
2014-08-15 21:14:22 +00:00
|
|
|
}
|
|
|
|
}
|
2014-09-30 00:15:00 +00:00
|
|
|
|
|
|
|
if expected, received := requestBody, c.handler.RequestBody; expected != nil && *expected != received {
|
|
|
|
t.Errorf("bad body for request %#v: expected %s, got %s", c.Request, *expected, received)
|
|
|
|
}
|
2014-08-15 21:14:22 +00:00
|
|
|
}
|
|
|
|
|
2014-06-09 05:38:45 +00:00
|
|
|
func TestListEmptyPods(t *testing.T) {
|
2014-10-23 20:55:48 +00:00
|
|
|
ns := api.NamespaceDefault
|
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-09-08 01:31:11 +00:00
|
|
|
Response: Response{StatusCode: 200, Body: &api.PodList{}},
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2014-10-23 20:55:48 +00:00
|
|
|
podList, err := c.Setup().Pods(ns).List(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-10-23 20:55:48 +00:00
|
|
|
ns := api.NamespaceDefault
|
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,
|
2014-09-08 01:31:11 +00:00
|
|
|
Body: &api.PodList{
|
2014-06-07 11:38:16 +00:00
|
|
|
Items: []api.Pod{
|
|
|
|
{
|
2014-11-13 15:52:13 +00:00
|
|
|
Status: api.PodStatus{
|
2014-11-21 19:04:32 +00:00
|
|
|
Phase: api.PodRunning,
|
2014-06-07 11:38:16 +00:00
|
|
|
},
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Labels: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
"name": "baz",
|
|
|
|
},
|
2014-06-07 11:38:16 +00:00
|
|
|
},
|
|
|
|
},
|
2014-06-06 23:40:48 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2014-10-23 20:55:48 +00:00
|
|
|
receivedPodList, err := c.Setup().Pods(ns).List(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-10-23 20:55:48 +00:00
|
|
|
ns := api.NamespaceDefault
|
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,
|
2014-09-08 01:31:11 +00:00
|
|
|
Body: &api.PodList{
|
2014-06-07 11:38:16 +00:00
|
|
|
Items: []api.Pod{
|
|
|
|
{
|
2014-11-13 15:52:13 +00:00
|
|
|
Status: api.PodStatus{
|
2014-11-21 19:04:32 +00:00
|
|
|
Phase: api.PodRunning,
|
2014-06-07 11:38:16 +00:00
|
|
|
},
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Labels: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
"name": "baz",
|
|
|
|
},
|
2014-06-07 11:38:16 +00:00
|
|
|
},
|
|
|
|
},
|
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-10-23 20:55:48 +00:00
|
|
|
receivedPodList, err := c.Pods(ns).List(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-10-23 20:55:48 +00:00
|
|
|
ns := api.NamespaceDefault
|
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,
|
2014-09-08 01:31:11 +00:00
|
|
|
Body: &api.Pod{
|
2014-11-13 15:52:13 +00:00
|
|
|
Status: api.PodStatus{
|
2014-11-21 19:04:32 +00:00
|
|
|
Phase: api.PodRunning,
|
2014-06-07 11:38:16 +00:00
|
|
|
},
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Labels: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
"name": "baz",
|
|
|
|
},
|
2014-06-07 11:38:16 +00:00
|
|
|
},
|
|
|
|
},
|
2014-06-06 23:40:48 +00:00
|
|
|
},
|
|
|
|
}
|
2014-10-23 20:55:48 +00:00
|
|
|
receivedPod, err := c.Setup().Pods(ns).Get("foo")
|
2014-06-07 11:38:16 +00:00
|
|
|
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-10-23 20:55:48 +00:00
|
|
|
err := c.Setup().Pods(api.NamespaceDefault).Delete("foo")
|
2014-06-07 11:38:16 +00:00
|
|
|
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) {
|
2014-09-08 01:31:11 +00:00
|
|
|
requestPod := &api.Pod{
|
2014-11-13 15:52:13 +00:00
|
|
|
Status: api.PodStatus{
|
2014-11-21 19:04:32 +00:00
|
|
|
Phase: api.PodRunning,
|
2014-06-06 23:40:48 +00:00
|
|
|
},
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Labels: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
"name": "baz",
|
|
|
|
},
|
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: "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-10-23 20:55:48 +00:00
|
|
|
receivedPod, err := c.Setup().Pods(api.NamespaceDefault).Create(requestPod)
|
2014-06-07 11:38:16 +00:00
|
|
|
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) {
|
2014-09-08 01:31:11 +00:00
|
|
|
requestPod := &api.Pod{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
ResourceVersion: "1",
|
|
|
|
Labels: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
"name": "baz",
|
|
|
|
},
|
|
|
|
},
|
2014-11-13 15:52:13 +00:00
|
|
|
Status: api.PodStatus{
|
2014-11-21 19:04:32 +00:00
|
|
|
Phase: api.PodRunning,
|
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: "/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-10-23 20:55:48 +00:00
|
|
|
receivedPod, err := c.Setup().Pods(api.NamespaceDefault).Update(requestPod)
|
2014-06-07 11:38:16 +00:00
|
|
|
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,
|
2014-09-08 01:31:11 +00:00
|
|
|
Body: &api.ReplicationControllerList{
|
2014-07-31 14:59:54 +00:00
|
|
|
Items: []api.ReplicationController{
|
|
|
|
{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
Labels: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
"name": "baz",
|
|
|
|
},
|
|
|
|
},
|
2014-11-07 02:09:46 +00:00
|
|
|
Spec: api.ReplicationControllerSpec{
|
2014-07-31 14:59:54 +00:00
|
|
|
Replicas: 2,
|
2014-11-07 02:09:46 +00:00
|
|
|
Template: &api.PodTemplateSpec{},
|
2014-07-31 14:59:54 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2014-10-23 20:55:48 +00:00
|
|
|
receivedControllerList, err := c.Setup().ReplicationControllers(api.NamespaceAll).List(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,
|
2014-09-08 01:31:11 +00:00
|
|
|
Body: &api.ReplicationController{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
Labels: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
"name": "baz",
|
|
|
|
},
|
|
|
|
},
|
2014-11-07 02:09:46 +00:00
|
|
|
Spec: api.ReplicationControllerSpec{
|
2014-06-07 11:38:16 +00:00
|
|
|
Replicas: 2,
|
2014-11-07 02:09:46 +00:00
|
|
|
Template: &api.PodTemplateSpec{},
|
2014-06-07 11:38:16 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2014-10-23 20:55:48 +00:00
|
|
|
receivedController, err := c.Setup().ReplicationControllers(api.NamespaceDefault).Get("foo")
|
2014-06-07 11:38:16 +00:00
|
|
|
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) {
|
2014-09-08 01:31:11 +00:00
|
|
|
requestController := &api.ReplicationController{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "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,
|
2014-09-08 01:31:11 +00:00
|
|
|
Body: &api.ReplicationController{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
Labels: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
"name": "baz",
|
|
|
|
},
|
|
|
|
},
|
2014-11-07 02:09:46 +00:00
|
|
|
Spec: api.ReplicationControllerSpec{
|
2014-06-07 11:38:16 +00:00
|
|
|
Replicas: 2,
|
2014-11-07 02:09:46 +00:00
|
|
|
Template: &api.PodTemplateSpec{},
|
2014-06-07 11:38:16 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2014-10-23 20:55:48 +00:00
|
|
|
receivedController, err := c.Setup().ReplicationControllers(api.NamespaceDefault).Update(requestController)
|
2014-06-07 11:38:16 +00:00
|
|
|
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-10-23 20:55:48 +00:00
|
|
|
err := c.Setup().ReplicationControllers(api.NamespaceDefault).Delete("foo")
|
2014-06-07 11:38:16 +00:00
|
|
|
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) {
|
2014-09-08 01:31:11 +00:00
|
|
|
requestController := &api.ReplicationController{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "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,
|
2014-09-08 01:31:11 +00:00
|
|
|
Body: &api.ReplicationController{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
Labels: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
"name": "baz",
|
|
|
|
},
|
|
|
|
},
|
2014-11-07 02:09:46 +00:00
|
|
|
Spec: api.ReplicationControllerSpec{
|
2014-06-07 11:38:16 +00:00
|
|
|
Replicas: 2,
|
2014-11-07 02:09:46 +00:00
|
|
|
Template: &api.PodTemplateSpec{},
|
2014-06-07 11:38:16 +00:00
|
|
|
},
|
|
|
|
},
|
2014-06-06 23:40:48 +00:00
|
|
|
},
|
|
|
|
}
|
2014-10-23 20:55:48 +00:00
|
|
|
receivedController, err := c.Setup().ReplicationControllers(api.NamespaceDefault).Create(requestController)
|
2014-06-07 11:38:16 +00:00
|
|
|
c.Validate(t, receivedController, err)
|
|
|
|
}
|
|
|
|
|
2014-09-08 01:31:11 +00:00
|
|
|
func body(obj runtime.Object, raw *string) *string {
|
2014-06-07 11:38:16 +00:00
|
|
|
if obj != nil {
|
2014-09-16 19:56:26 +00:00
|
|
|
bs, _ := latest.Codec.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-08-28 04:32:52 +00:00
|
|
|
func TestListServices(t *testing.T) {
|
|
|
|
c := &testClient{
|
|
|
|
Request: testRequest{Method: "GET", Path: "/services"},
|
|
|
|
Response: Response{StatusCode: 200,
|
2014-09-08 01:31:11 +00:00
|
|
|
Body: &api.ServiceList{
|
2014-08-28 04:32:52 +00:00
|
|
|
Items: []api.Service{
|
|
|
|
{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "name",
|
|
|
|
Labels: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
"name": "baz",
|
|
|
|
},
|
2014-08-28 04:32:52 +00:00
|
|
|
},
|
2014-10-30 13:29:11 +00:00
|
|
|
Spec: api.ServiceSpec{
|
|
|
|
Selector: map[string]string{
|
|
|
|
"one": "two",
|
|
|
|
},
|
2014-08-28 04:32:52 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2014-10-23 20:55:48 +00:00
|
|
|
receivedServiceList, err := c.Setup().Services(api.NamespaceDefault).List(labels.Everything())
|
2014-10-23 20:51:34 +00:00
|
|
|
t.Logf("received services: %v %#v", err, receivedServiceList)
|
2014-08-28 04:32:52 +00:00
|
|
|
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,
|
2014-09-08 01:31:11 +00:00
|
|
|
Body: &api.ServiceList{
|
2014-08-28 04:32:52 +00:00
|
|
|
Items: []api.Service{
|
|
|
|
{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "name",
|
|
|
|
Labels: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
"name": "baz",
|
|
|
|
},
|
2014-08-28 04:32:52 +00:00
|
|
|
},
|
2014-10-30 13:29:11 +00:00
|
|
|
Spec: api.ServiceSpec{
|
|
|
|
Selector: map[string]string{
|
|
|
|
"one": "two",
|
|
|
|
},
|
2014-08-28 04:32:52 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
c.Setup()
|
|
|
|
c.QueryValidator["labels"] = validateLabels
|
|
|
|
selector := labels.Set{"foo": "bar", "name": "baz"}.AsSelector()
|
2014-10-23 20:55:48 +00:00
|
|
|
receivedServiceList, err := c.Services(api.NamespaceDefault).List(selector)
|
2014-08-28 04:32:52 +00:00
|
|
|
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-10-23 20:51:34 +00:00
|
|
|
Response: Response{StatusCode: 200, Body: &api.Service{ObjectMeta: api.ObjectMeta{Name: "service-1"}}},
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2014-10-23 20:55:48 +00:00
|
|
|
response, err := c.Setup().Services(api.NamespaceDefault).Get("1")
|
2014-09-08 01:31:11 +00:00
|
|
|
c.Validate(t, response, err)
|
2014-06-07 11:38:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateService(t *testing.T) {
|
2014-09-30 00:15:00 +00:00
|
|
|
c := &testClient{
|
2014-10-23 20:51:34 +00:00
|
|
|
Request: testRequest{Method: "POST", Path: "/services", Body: &api.Service{ObjectMeta: api.ObjectMeta{Name: "service-1"}}},
|
|
|
|
Response: Response{StatusCode: 200, Body: &api.Service{ObjectMeta: api.ObjectMeta{Name: "service-1"}}},
|
2014-09-30 00:15:00 +00:00
|
|
|
}
|
2014-10-23 20:55:48 +00:00
|
|
|
response, err := c.Setup().Services(api.NamespaceDefault).Create(&api.Service{ObjectMeta: api.ObjectMeta{Name: "service-1"}})
|
2014-09-08 01:31:11 +00:00
|
|
|
c.Validate(t, response, err)
|
2014-06-07 11:38:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateService(t *testing.T) {
|
2014-10-23 20:51:34 +00:00
|
|
|
svc := &api.Service{ObjectMeta: api.ObjectMeta{Name: "service-1", ResourceVersion: "1"}}
|
2014-06-24 04:55:11 +00:00
|
|
|
c := &testClient{
|
2014-09-08 01:31:11 +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-10-23 20:55:48 +00:00
|
|
|
response, err := c.Setup().Services(api.NamespaceDefault).Update(svc)
|
2014-09-08 01:31:11 +00:00
|
|
|
c.Validate(t, response, err)
|
2014-06-07 11:38:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-10-23 20:55:48 +00:00
|
|
|
err := c.Setup().Services(api.NamespaceDefault).Delete("1")
|
2014-06-07 11:38:16 +00:00
|
|
|
c.Validate(t, nil, err)
|
|
|
|
}
|
|
|
|
|
2014-09-23 00:39:47 +00:00
|
|
|
func TestListEndpooints(t *testing.T) {
|
|
|
|
c := &testClient{
|
|
|
|
Request: testRequest{Method: "GET", Path: "/endpoints"},
|
|
|
|
Response: Response{StatusCode: 200,
|
|
|
|
Body: &api.EndpointsList{
|
|
|
|
Items: []api.Endpoints{
|
|
|
|
{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "endpoint-1"},
|
|
|
|
Endpoints: []string{"10.245.1.2:8080", "10.245.1.3:8080"},
|
2014-09-23 00:39:47 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2014-10-23 20:55:48 +00:00
|
|
|
receivedEndpointsList, err := c.Setup().Endpoints(api.NamespaceDefault).List(labels.Everything())
|
2014-09-23 00:39:47 +00:00
|
|
|
c.Validate(t, receivedEndpointsList, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetEndpoints(t *testing.T) {
|
|
|
|
c := &testClient{
|
|
|
|
Request: testRequest{Method: "GET", Path: "/endpoints/endpoint-1"},
|
2014-10-23 20:51:34 +00:00
|
|
|
Response: Response{StatusCode: 200, Body: &api.Endpoints{ObjectMeta: api.ObjectMeta{Name: "endpoint-1"}}},
|
2014-09-23 00:39:47 +00:00
|
|
|
}
|
2014-10-23 20:55:48 +00:00
|
|
|
response, err := c.Setup().Endpoints(api.NamespaceDefault).Get("endpoint-1")
|
2014-09-23 00:39:47 +00:00
|
|
|
c.Validate(t, response, err)
|
|
|
|
}
|
|
|
|
|
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-09-30 00:15:00 +00:00
|
|
|
client := NewOrDie(&Config{Host: server.URL})
|
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
|
|
|
|
2014-12-03 05:55:44 +00:00
|
|
|
func TestGetServerAPIVersions(t *testing.T) {
|
|
|
|
versions := []string{"v1", "v2", "v3"}
|
|
|
|
expect := api.APIVersions{Versions: versions}
|
|
|
|
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 := NewOrDie(&Config{Host: server.URL})
|
|
|
|
got, err := client.ServerAPIVersions()
|
|
|
|
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"},
|
2014-12-08 03:44:27 +00:00
|
|
|
Response: Response{StatusCode: 200, Body: &api.NodeList{ListMeta: api.ListMeta{ResourceVersion: "1"}}},
|
2014-08-27 21:07:22 +00:00
|
|
|
}
|
2014-10-23 20:55:48 +00:00
|
|
|
response, err := c.Setup().Minions().List()
|
2014-09-08 01:31:11 +00:00
|
|
|
c.Validate(t, response, err)
|
2014-08-27 21:07:22 +00:00
|
|
|
}
|
2014-10-22 01:39:13 +00:00
|
|
|
|
2014-12-05 09:49:43 +00:00
|
|
|
func TestGetMinion(t *testing.T) {
|
|
|
|
c := &testClient{
|
|
|
|
Request: testRequest{Method: "GET", Path: "/minions/1"},
|
2014-12-08 03:44:27 +00:00
|
|
|
Response: Response{StatusCode: 200, Body: &api.Node{ObjectMeta: api.ObjectMeta{Name: "minion-1"}}},
|
2014-12-05 09:49:43 +00:00
|
|
|
}
|
|
|
|
response, err := c.Setup().Minions().Get("1")
|
|
|
|
c.Validate(t, response, err)
|
|
|
|
}
|
|
|
|
|
2014-10-22 01:39:13 +00:00
|
|
|
func TestCreateMinion(t *testing.T) {
|
2014-12-08 03:44:27 +00:00
|
|
|
requestMinion := &api.Node{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{
|
2014-10-22 17:02:02 +00:00
|
|
|
Name: "minion-1",
|
2014-10-22 01:39:13 +00:00
|
|
|
},
|
2014-11-19 22:39:10 +00:00
|
|
|
Status: api.NodeStatus{
|
|
|
|
HostIP: "123.321.456.654",
|
|
|
|
},
|
|
|
|
Spec: api.NodeSpec{
|
2014-10-22 01:39:13 +00:00
|
|
|
Capacity: api.ResourceList{
|
|
|
|
resources.CPU: util.NewIntOrStringFromInt(1000),
|
|
|
|
resources.Memory: util.NewIntOrStringFromInt(1024 * 1024),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
c := &testClient{
|
|
|
|
Request: testRequest{Method: "POST", Path: "/minions", Body: requestMinion},
|
|
|
|
Response: Response{
|
|
|
|
StatusCode: 200,
|
|
|
|
Body: requestMinion,
|
|
|
|
},
|
|
|
|
}
|
2014-10-23 20:55:48 +00:00
|
|
|
receivedMinion, err := c.Setup().Minions().Create(requestMinion)
|
2014-10-22 01:39:13 +00:00
|
|
|
c.Validate(t, receivedMinion, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteMinion(t *testing.T) {
|
|
|
|
c := &testClient{
|
|
|
|
Request: testRequest{Method: "DELETE", Path: "/minions/foo"},
|
|
|
|
Response: Response{StatusCode: 200},
|
|
|
|
}
|
2014-10-23 20:55:48 +00:00
|
|
|
err := c.Setup().Minions().Delete("foo")
|
2014-10-22 01:39:13 +00:00
|
|
|
c.Validate(t, nil, err)
|
|
|
|
}
|