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.
|
|
|
|
*/
|
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TODO: This doesn't reduce typing enough to make it worth the less readable errors. Remove.
|
|
|
|
func expectNoError(t *testing.T, err error) {
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error: %#v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Move this to a common place, it's needed in multiple tests.
|
|
|
|
var apiPath = "/api/v1beta1"
|
|
|
|
|
|
|
|
func makeUrl(suffix string) string {
|
|
|
|
return apiPath + suffix
|
|
|
|
}
|
|
|
|
|
2014-06-09 05:38:45 +00:00
|
|
|
func TestListEmptyPods(t *testing.T) {
|
2014-06-06 23:40:48 +00:00
|
|
|
fakeHandler := util.FakeHandler{
|
|
|
|
StatusCode: 200,
|
|
|
|
ResponseBody: `{ "items": []}`,
|
|
|
|
}
|
|
|
|
testServer := httptest.NewTLSServer(&fakeHandler)
|
|
|
|
client := Client{
|
|
|
|
Host: testServer.URL,
|
|
|
|
}
|
2014-06-09 05:38:45 +00:00
|
|
|
podList, err := client.ListPods(nil)
|
2014-06-09 06:00:12 +00:00
|
|
|
fakeHandler.ValidateRequest(t, makeUrl("/pods"), "GET", nil)
|
2014-06-06 23:40:48 +00:00
|
|
|
if err != nil {
|
2014-06-09 05:38:45 +00:00
|
|
|
t.Errorf("Unexpected error in listing pods: %#v", err)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2014-06-09 05:38:45 +00:00
|
|
|
if len(podList.Items) != 0 {
|
|
|
|
t.Errorf("Unexpected items in pod list: %#v", podList)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
testServer.Close()
|
|
|
|
}
|
|
|
|
|
2014-06-09 05:38:45 +00:00
|
|
|
func TestListPods(t *testing.T) {
|
|
|
|
expectedPodList := api.PodList{
|
2014-06-09 04:17:53 +00:00
|
|
|
Items: []api.Pod{
|
2014-06-12 21:09:40 +00:00
|
|
|
{
|
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-09 05:38:45 +00:00
|
|
|
body, _ := json.Marshal(expectedPodList)
|
2014-06-06 23:40:48 +00:00
|
|
|
fakeHandler := util.FakeHandler{
|
|
|
|
StatusCode: 200,
|
|
|
|
ResponseBody: string(body),
|
|
|
|
}
|
|
|
|
testServer := httptest.NewTLSServer(&fakeHandler)
|
|
|
|
client := Client{
|
|
|
|
Host: testServer.URL,
|
|
|
|
}
|
2014-06-09 05:38:45 +00:00
|
|
|
receivedPodList, err := client.ListPods(nil)
|
2014-06-09 06:00:12 +00:00
|
|
|
fakeHandler.ValidateRequest(t, makeUrl("/pods"), "GET", nil)
|
2014-06-06 23:40:48 +00:00
|
|
|
if err != nil {
|
2014-06-09 05:38:45 +00:00
|
|
|
t.Errorf("Unexpected error in listing pods: %#v", err)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2014-06-09 05:38:45 +00:00
|
|
|
if !reflect.DeepEqual(expectedPodList, receivedPodList) {
|
|
|
|
t.Errorf("Unexpected pod list: %#v\nvs.\n%#v", receivedPodList, expectedPodList)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
testServer.Close()
|
|
|
|
}
|
|
|
|
|
2014-06-09 05:38:45 +00:00
|
|
|
func TestListPodsLabels(t *testing.T) {
|
|
|
|
expectedPodList := api.PodList{
|
2014-06-09 04:17:53 +00:00
|
|
|
Items: []api.Pod{
|
2014-06-12 21:09:40 +00:00
|
|
|
{
|
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-09 05:38:45 +00:00
|
|
|
body, _ := json.Marshal(expectedPodList)
|
2014-06-06 23:40:48 +00:00
|
|
|
fakeHandler := util.FakeHandler{
|
|
|
|
StatusCode: 200,
|
|
|
|
ResponseBody: string(body),
|
|
|
|
}
|
|
|
|
testServer := httptest.NewTLSServer(&fakeHandler)
|
|
|
|
client := Client{
|
|
|
|
Host: testServer.URL,
|
|
|
|
}
|
2014-06-18 23:47:41 +00:00
|
|
|
selector := map[string]string{"foo": "bar", "name": "baz"}
|
|
|
|
receivedPodList, err := client.ListPods(selector)
|
2014-06-09 06:00:12 +00:00
|
|
|
fakeHandler.ValidateRequest(t, makeUrl("/pods"), "GET", nil)
|
2014-06-18 23:47:41 +00:00
|
|
|
selectorString := fakeHandler.RequestReceived.URL.Query().Get("labels")
|
|
|
|
selectorString, _ = url.QueryUnescape(selectorString)
|
|
|
|
parsedSelectorString := DecodeSelector(selectorString)
|
|
|
|
expectEqual(t, selector, parsedSelectorString)
|
2014-06-06 23:40:48 +00:00
|
|
|
if err != nil {
|
2014-06-09 05:38:45 +00:00
|
|
|
t.Errorf("Unexpected error in listing pods: %#v", err)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2014-06-09 05:38:45 +00:00
|
|
|
if !reflect.DeepEqual(expectedPodList, receivedPodList) {
|
|
|
|
t.Errorf("Unexpected pod list: %#v\nvs.\n%#v", receivedPodList, expectedPodList)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
testServer.Close()
|
|
|
|
}
|
|
|
|
|
2014-06-09 05:38:45 +00:00
|
|
|
func TestGetPod(t *testing.T) {
|
|
|
|
expectedPod := 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-09 05:38:45 +00:00
|
|
|
body, _ := json.Marshal(expectedPod)
|
2014-06-06 23:40:48 +00:00
|
|
|
fakeHandler := util.FakeHandler{
|
|
|
|
StatusCode: 200,
|
|
|
|
ResponseBody: string(body),
|
|
|
|
}
|
|
|
|
testServer := httptest.NewTLSServer(&fakeHandler)
|
|
|
|
client := Client{
|
|
|
|
Host: testServer.URL,
|
|
|
|
}
|
2014-06-09 05:38:45 +00:00
|
|
|
receivedPod, err := client.GetPod("foo")
|
2014-06-09 06:00:12 +00:00
|
|
|
fakeHandler.ValidateRequest(t, makeUrl("/pods/foo"), "GET", nil)
|
2014-06-06 23:40:48 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error: %#v", err)
|
|
|
|
}
|
2014-06-09 05:38:45 +00:00
|
|
|
if !reflect.DeepEqual(expectedPod, receivedPod) {
|
|
|
|
t.Errorf("Received pod: %#v\n doesn't match expected pod: %#v", receivedPod, expectedPod)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
testServer.Close()
|
|
|
|
}
|
|
|
|
|
2014-06-09 05:38:45 +00:00
|
|
|
func TestDeletePod(t *testing.T) {
|
2014-06-06 23:40:48 +00:00
|
|
|
fakeHandler := util.FakeHandler{
|
|
|
|
StatusCode: 200,
|
|
|
|
ResponseBody: `{"success": true}`,
|
|
|
|
}
|
|
|
|
testServer := httptest.NewTLSServer(&fakeHandler)
|
|
|
|
client := Client{
|
|
|
|
Host: testServer.URL,
|
|
|
|
}
|
2014-06-09 05:38:45 +00:00
|
|
|
err := client.DeletePod("foo")
|
2014-06-09 06:00:12 +00:00
|
|
|
fakeHandler.ValidateRequest(t, makeUrl("/pods/foo"), "DELETE", nil)
|
2014-06-06 23:40:48 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error: %#v", err)
|
|
|
|
}
|
|
|
|
testServer.Close()
|
|
|
|
}
|
|
|
|
|
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-09 05:38:45 +00:00
|
|
|
body, _ := json.Marshal(requestPod)
|
2014-06-06 23:40:48 +00:00
|
|
|
fakeHandler := util.FakeHandler{
|
|
|
|
StatusCode: 200,
|
|
|
|
ResponseBody: string(body),
|
|
|
|
}
|
|
|
|
testServer := httptest.NewTLSServer(&fakeHandler)
|
|
|
|
client := Client{
|
|
|
|
Host: testServer.URL,
|
|
|
|
}
|
2014-06-09 05:38:45 +00:00
|
|
|
receivedPod, err := client.CreatePod(requestPod)
|
2014-06-09 06:00:12 +00:00
|
|
|
fakeHandler.ValidateRequest(t, makeUrl("/pods"), "POST", nil)
|
2014-06-06 23:40:48 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error: %#v", err)
|
|
|
|
}
|
2014-06-09 05:38:45 +00:00
|
|
|
if !reflect.DeepEqual(requestPod, receivedPod) {
|
|
|
|
t.Errorf("Received pod: %#v\n doesn't match expected pod: %#v", receivedPod, requestPod)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
testServer.Close()
|
|
|
|
}
|
|
|
|
|
2014-06-09 05:38:45 +00:00
|
|
|
func TestUpdatePod(t *testing.T) {
|
|
|
|
requestPod := api.Pod{
|
2014-06-06 23:40:48 +00:00
|
|
|
JSONBase: api.JSONBase{ID: "foo"},
|
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-09 05:38:45 +00:00
|
|
|
body, _ := json.Marshal(requestPod)
|
2014-06-06 23:40:48 +00:00
|
|
|
fakeHandler := util.FakeHandler{
|
|
|
|
StatusCode: 200,
|
|
|
|
ResponseBody: string(body),
|
|
|
|
}
|
|
|
|
testServer := httptest.NewTLSServer(&fakeHandler)
|
|
|
|
client := Client{
|
|
|
|
Host: testServer.URL,
|
|
|
|
}
|
2014-06-09 05:38:45 +00:00
|
|
|
receivedPod, err := client.UpdatePod(requestPod)
|
2014-06-09 06:00:12 +00:00
|
|
|
fakeHandler.ValidateRequest(t, makeUrl("/pods/foo"), "PUT", nil)
|
2014-06-06 23:40:48 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error: %#v", err)
|
|
|
|
}
|
2014-06-09 05:38:45 +00:00
|
|
|
expectEqual(t, requestPod, receivedPod)
|
2014-06-06 23:40:48 +00:00
|
|
|
testServer.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func expectEqual(t *testing.T, expected, observed interface{}) {
|
|
|
|
if !reflect.DeepEqual(expected, observed) {
|
|
|
|
t.Errorf("Unexpected inequality. Expected: %#v Observed: %#v", expected, observed)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetController(t *testing.T) {
|
|
|
|
expectedController := api.ReplicationController{
|
|
|
|
JSONBase: api.JSONBase{
|
|
|
|
ID: "foo",
|
|
|
|
},
|
|
|
|
DesiredState: api.ReplicationControllerState{
|
|
|
|
Replicas: 2,
|
|
|
|
},
|
|
|
|
Labels: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
"name": "baz",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
body, _ := json.Marshal(expectedController)
|
|
|
|
fakeHandler := util.FakeHandler{
|
|
|
|
StatusCode: 200,
|
|
|
|
ResponseBody: string(body),
|
|
|
|
}
|
|
|
|
testServer := httptest.NewTLSServer(&fakeHandler)
|
|
|
|
client := Client{
|
|
|
|
Host: testServer.URL,
|
|
|
|
}
|
|
|
|
receivedController, err := client.GetReplicationController("foo")
|
|
|
|
expectNoError(t, err)
|
|
|
|
if !reflect.DeepEqual(expectedController, receivedController) {
|
|
|
|
t.Errorf("Unexpected controller, expected: %#v, received %#v", expectedController, receivedController)
|
|
|
|
}
|
|
|
|
fakeHandler.ValidateRequest(t, makeUrl("/replicationControllers/foo"), "GET", nil)
|
|
|
|
testServer.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateController(t *testing.T) {
|
|
|
|
expectedController := api.ReplicationController{
|
|
|
|
JSONBase: api.JSONBase{
|
|
|
|
ID: "foo",
|
|
|
|
},
|
|
|
|
DesiredState: api.ReplicationControllerState{
|
|
|
|
Replicas: 2,
|
|
|
|
},
|
|
|
|
Labels: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
"name": "baz",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
body, _ := json.Marshal(expectedController)
|
|
|
|
fakeHandler := util.FakeHandler{
|
|
|
|
StatusCode: 200,
|
|
|
|
ResponseBody: string(body),
|
|
|
|
}
|
|
|
|
testServer := httptest.NewTLSServer(&fakeHandler)
|
|
|
|
client := Client{
|
|
|
|
Host: testServer.URL,
|
|
|
|
}
|
|
|
|
receivedController, err := client.UpdateReplicationController(api.ReplicationController{
|
|
|
|
JSONBase: api.JSONBase{
|
|
|
|
ID: "foo",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
expectNoError(t, err)
|
|
|
|
if !reflect.DeepEqual(expectedController, receivedController) {
|
|
|
|
t.Errorf("Unexpected controller, expected: %#v, received %#v", expectedController, receivedController)
|
|
|
|
}
|
|
|
|
fakeHandler.ValidateRequest(t, makeUrl("/replicationControllers/foo"), "PUT", nil)
|
|
|
|
testServer.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteController(t *testing.T) {
|
|
|
|
fakeHandler := util.FakeHandler{
|
|
|
|
StatusCode: 200,
|
|
|
|
ResponseBody: `{"success": true}`,
|
|
|
|
}
|
|
|
|
testServer := httptest.NewTLSServer(&fakeHandler)
|
|
|
|
client := Client{
|
|
|
|
Host: testServer.URL,
|
|
|
|
}
|
|
|
|
err := client.DeleteReplicationController("foo")
|
|
|
|
fakeHandler.ValidateRequest(t, makeUrl("/replicationControllers/foo"), "DELETE", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error: %#v", err)
|
|
|
|
}
|
|
|
|
testServer.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateController(t *testing.T) {
|
|
|
|
expectedController := api.ReplicationController{
|
|
|
|
JSONBase: api.JSONBase{
|
|
|
|
ID: "foo",
|
|
|
|
},
|
|
|
|
DesiredState: api.ReplicationControllerState{
|
|
|
|
Replicas: 2,
|
|
|
|
},
|
|
|
|
Labels: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
"name": "baz",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
body, _ := json.Marshal(expectedController)
|
|
|
|
fakeHandler := util.FakeHandler{
|
|
|
|
StatusCode: 200,
|
|
|
|
ResponseBody: string(body),
|
|
|
|
}
|
|
|
|
testServer := httptest.NewTLSServer(&fakeHandler)
|
|
|
|
client := Client{
|
|
|
|
Host: testServer.URL,
|
|
|
|
}
|
|
|
|
receivedController, err := client.CreateReplicationController(api.ReplicationController{
|
|
|
|
JSONBase: api.JSONBase{
|
|
|
|
ID: "foo",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
expectNoError(t, err)
|
|
|
|
if !reflect.DeepEqual(expectedController, receivedController) {
|
|
|
|
t.Errorf("Unexpected controller, expected: %#v, received %#v", expectedController, receivedController)
|
|
|
|
}
|
|
|
|
fakeHandler.ValidateRequest(t, makeUrl("/replicationControllers"), "POST", nil)
|
|
|
|
testServer.Close()
|
|
|
|
}
|