k3s/pkg/cloudcfg/cloudcfg_test.go

332 lines
11 KiB
Go
Raw Normal View History

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-16 18:16:18 +00:00
2014-06-06 23:40:48 +00:00
package cloudcfg
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
2014-06-16 18:16:18 +00:00
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
2014-06-12 20:35:00 +00:00
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
2014-06-06 23:40:48 +00:00
"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)
}
}
type Action struct {
action string
value interface{}
}
type FakeKubeClient struct {
actions []Action
2014-06-16 18:16:18 +00:00
pods api.PodList
ctrl api.ReplicationController
2014-06-06 23:40:48 +00:00
}
2014-06-16 18:16:18 +00:00
func (client *FakeKubeClient) ListPods(labelQuery map[string]string) (api.PodList, error) {
2014-06-09 05:38:45 +00:00
client.actions = append(client.actions, Action{action: "list-pods"})
return client.pods, nil
2014-06-06 23:40:48 +00:00
}
2014-06-16 18:16:18 +00:00
func (client *FakeKubeClient) GetPod(name string) (api.Pod, error) {
2014-06-09 05:38:45 +00:00
client.actions = append(client.actions, Action{action: "get-pod", value: name})
2014-06-16 18:16:18 +00:00
return api.Pod{}, nil
2014-06-06 23:40:48 +00:00
}
2014-06-09 05:38:45 +00:00
func (client *FakeKubeClient) DeletePod(name string) error {
client.actions = append(client.actions, Action{action: "delete-pod", value: name})
2014-06-06 23:40:48 +00:00
return nil
}
2014-06-16 18:16:18 +00:00
func (client *FakeKubeClient) CreatePod(pod api.Pod) (api.Pod, error) {
2014-06-09 05:38:45 +00:00
client.actions = append(client.actions, Action{action: "create-pod"})
2014-06-16 18:16:18 +00:00
return api.Pod{}, nil
2014-06-06 23:40:48 +00:00
}
2014-06-16 18:16:18 +00:00
func (client *FakeKubeClient) UpdatePod(pod api.Pod) (api.Pod, error) {
2014-06-09 05:38:45 +00:00
client.actions = append(client.actions, Action{action: "update-pod", value: pod.ID})
2014-06-16 18:16:18 +00:00
return api.Pod{}, nil
2014-06-06 23:40:48 +00:00
}
2014-06-16 18:16:18 +00:00
func (client *FakeKubeClient) GetReplicationController(name string) (api.ReplicationController, error) {
2014-06-06 23:40:48 +00:00
client.actions = append(client.actions, Action{action: "get-controller", value: name})
return client.ctrl, nil
}
2014-06-16 18:16:18 +00:00
func (client *FakeKubeClient) CreateReplicationController(controller api.ReplicationController) (api.ReplicationController, error) {
2014-06-06 23:40:48 +00:00
client.actions = append(client.actions, Action{action: "create-controller", value: controller})
2014-06-16 18:16:18 +00:00
return api.ReplicationController{}, nil
2014-06-06 23:40:48 +00:00
}
2014-06-16 18:16:18 +00:00
func (client *FakeKubeClient) UpdateReplicationController(controller api.ReplicationController) (api.ReplicationController, error) {
2014-06-06 23:40:48 +00:00
client.actions = append(client.actions, Action{action: "update-controller", value: controller})
2014-06-16 18:16:18 +00:00
return api.ReplicationController{}, nil
2014-06-06 23:40:48 +00:00
}
func (client *FakeKubeClient) DeleteReplicationController(controller string) error {
client.actions = append(client.actions, Action{action: "delete-controller", value: controller})
return nil
}
2014-06-16 18:16:18 +00:00
func (client *FakeKubeClient) GetService(name string) (api.Service, error) {
2014-06-06 23:40:48 +00:00
client.actions = append(client.actions, Action{action: "get-controller", value: name})
2014-06-16 18:16:18 +00:00
return api.Service{}, nil
2014-06-06 23:40:48 +00:00
}
2014-06-16 18:16:18 +00:00
func (client *FakeKubeClient) CreateService(controller api.Service) (api.Service, error) {
2014-06-06 23:40:48 +00:00
client.actions = append(client.actions, Action{action: "create-service", value: controller})
2014-06-16 18:16:18 +00:00
return api.Service{}, nil
2014-06-06 23:40:48 +00:00
}
2014-06-16 18:16:18 +00:00
func (client *FakeKubeClient) UpdateService(controller api.Service) (api.Service, error) {
2014-06-06 23:40:48 +00:00
client.actions = append(client.actions, Action{action: "update-service", value: controller})
2014-06-16 18:16:18 +00:00
return api.Service{}, nil
2014-06-06 23:40:48 +00:00
}
func (client *FakeKubeClient) DeleteService(controller string) error {
client.actions = append(client.actions, Action{action: "delete-service", value: controller})
return nil
}
func validateAction(expectedAction, actualAction Action, t *testing.T) {
if expectedAction != actualAction {
t.Errorf("Unexpected action: %#v, expected: %#v", actualAction, expectedAction)
}
}
2014-06-09 05:38:45 +00:00
func TestUpdateWithPods(t *testing.T) {
2014-06-06 23:40:48 +00:00
client := FakeKubeClient{
2014-06-16 18:16:18 +00:00
pods: api.PodList{
Items: []api.Pod{
{JSONBase: api.JSONBase{ID: "pod-1"}},
{JSONBase: api.JSONBase{ID: "pod-2"}},
2014-06-06 23:40:48 +00:00
},
},
}
Update("foo", &client, 0)
if len(client.actions) != 4 {
t.Errorf("Unexpected action list %#v", client.actions)
}
validateAction(Action{action: "get-controller", value: "foo"}, client.actions[0], t)
2014-06-09 05:38:45 +00:00
validateAction(Action{action: "list-pods"}, client.actions[1], t)
validateAction(Action{action: "update-pod", value: "pod-1"}, client.actions[2], t)
validateAction(Action{action: "update-pod", value: "pod-2"}, client.actions[3], t)
2014-06-06 23:40:48 +00:00
}
2014-06-09 05:38:45 +00:00
func TestUpdateNoPods(t *testing.T) {
2014-06-06 23:40:48 +00:00
client := FakeKubeClient{}
Update("foo", &client, 0)
if len(client.actions) != 2 {
t.Errorf("Unexpected action list %#v", client.actions)
}
validateAction(Action{action: "get-controller", value: "foo"}, client.actions[0], t)
2014-06-09 05:38:45 +00:00
validateAction(Action{action: "list-pods"}, client.actions[1], t)
2014-06-06 23:40:48 +00:00
}
func TestDoRequest(t *testing.T) {
expectedBody := `{ "items": []}`
fakeHandler := util.FakeHandler{
StatusCode: 200,
ResponseBody: expectedBody,
T: t,
2014-06-06 23:40:48 +00:00
}
testServer := httptest.NewTLSServer(&fakeHandler)
request, _ := http.NewRequest("GET", testServer.URL+"/foo/bar", nil)
2014-06-12 20:35:00 +00:00
auth := client.AuthInfo{User: "user", Password: "pass"}
body, err := DoRequest(request, &auth)
2014-06-06 23:40:48 +00:00
if request.Header["Authorization"] == nil {
t.Errorf("Request is missing authorization header: %#v", *request)
}
if err != nil {
t.Error("Unexpected error")
}
if body != expectedBody {
t.Errorf("Expected body: '%s', saw: '%s'", expectedBody, body)
}
fakeHandler.ValidateRequest(t, "/foo/bar", "GET", nil)
2014-06-06 23:40:48 +00:00
}
func TestRunController(t *testing.T) {
fakeClient := FakeKubeClient{}
name := "name"
image := "foo/bar"
replicas := 3
RunController(image, name, replicas, &fakeClient, "8080:80", -1)
if len(fakeClient.actions) != 1 || fakeClient.actions[0].action != "create-controller" {
t.Errorf("Unexpected actions: %#v", fakeClient.actions)
}
2014-06-16 18:16:18 +00:00
controller := fakeClient.actions[0].value.(api.ReplicationController)
2014-06-06 23:40:48 +00:00
if controller.ID != name ||
controller.DesiredState.Replicas != replicas ||
2014-06-09 04:39:57 +00:00
controller.DesiredState.PodTemplate.DesiredState.Manifest.Containers[0].Image != image {
2014-06-06 23:40:48 +00:00
t.Errorf("Unexpected controller: %#v", controller)
}
}
func TestRunControllerWithService(t *testing.T) {
fakeClient := FakeKubeClient{}
name := "name"
image := "foo/bar"
replicas := 3
RunController(image, name, replicas, &fakeClient, "", 8000)
if len(fakeClient.actions) != 2 ||
fakeClient.actions[0].action != "create-controller" ||
fakeClient.actions[1].action != "create-service" {
t.Errorf("Unexpected actions: %#v", fakeClient.actions)
}
2014-06-16 18:16:18 +00:00
controller := fakeClient.actions[0].value.(api.ReplicationController)
2014-06-06 23:40:48 +00:00
if controller.ID != name ||
controller.DesiredState.Replicas != replicas ||
2014-06-09 04:39:57 +00:00
controller.DesiredState.PodTemplate.DesiredState.Manifest.Containers[0].Image != image {
2014-06-06 23:40:48 +00:00
t.Errorf("Unexpected controller: %#v", controller)
}
}
func TestStopController(t *testing.T) {
fakeClient := FakeKubeClient{}
name := "name"
StopController(name, &fakeClient)
if len(fakeClient.actions) != 2 {
t.Errorf("Unexpected actions: %#v", fakeClient.actions)
}
if fakeClient.actions[0].action != "get-controller" ||
fakeClient.actions[0].value.(string) != name {
t.Errorf("Unexpected action: %#v", fakeClient.actions[0])
}
2014-06-16 18:16:18 +00:00
controller := fakeClient.actions[1].value.(api.ReplicationController)
2014-06-06 23:40:48 +00:00
if fakeClient.actions[1].action != "update-controller" ||
controller.DesiredState.Replicas != 0 {
t.Errorf("Unexpected action: %#v", fakeClient.actions[1])
}
}
func TestResizeController(t *testing.T) {
fakeClient := FakeKubeClient{}
name := "name"
replicas := 17
ResizeController(name, replicas, &fakeClient)
if len(fakeClient.actions) != 2 {
t.Errorf("Unexpected actions: %#v", fakeClient.actions)
}
if fakeClient.actions[0].action != "get-controller" ||
fakeClient.actions[0].value.(string) != name {
t.Errorf("Unexpected action: %#v", fakeClient.actions[0])
}
2014-06-16 18:16:18 +00:00
controller := fakeClient.actions[1].value.(api.ReplicationController)
if fakeClient.actions[1].action != "update-controller" ||
controller.DesiredState.Replicas != 17 {
t.Errorf("Unexpected action: %#v", fakeClient.actions[1])
}
}
2014-06-06 23:40:48 +00:00
func TestCloudCfgDeleteController(t *testing.T) {
fakeClient := FakeKubeClient{}
name := "name"
err := DeleteController(name, &fakeClient)
expectNoError(t, err)
if len(fakeClient.actions) != 2 {
t.Errorf("Unexpected actions: %#v", fakeClient.actions)
}
if fakeClient.actions[0].action != "get-controller" ||
fakeClient.actions[0].value.(string) != name {
t.Errorf("Unexpected action: %#v", fakeClient.actions[0])
}
if fakeClient.actions[1].action != "delete-controller" ||
fakeClient.actions[1].value.(string) != name {
t.Errorf("Unexpected action: %#v", fakeClient.actions[1])
}
}
func TestCloudCfgDeleteControllerWithReplicas(t *testing.T) {
fakeClient := FakeKubeClient{
2014-06-16 18:16:18 +00:00
ctrl: api.ReplicationController{
DesiredState: api.ReplicationControllerState{
2014-06-06 23:40:48 +00:00
Replicas: 2,
},
},
}
name := "name"
err := DeleteController(name, &fakeClient)
if len(fakeClient.actions) != 1 {
t.Errorf("Unexpected actions: %#v", fakeClient.actions)
}
if fakeClient.actions[0].action != "get-controller" ||
fakeClient.actions[0].value.(string) != name {
t.Errorf("Unexpected action: %#v", fakeClient.actions[0])
}
if err == nil {
t.Errorf("Unexpected non-error.")
}
}
func TestRequestWithBodyNoSuchFile(t *testing.T) {
request, err := RequestWithBody("non/existent/file.json", "http://www.google.com", "GET")
if request != nil {
t.Error("Unexpected non-nil result")
}
if err == nil {
t.Error("Unexpected non-error")
}
}
func TestRequestWithBody(t *testing.T) {
file, err := ioutil.TempFile("", "foo")
expectNoError(t, err)
2014-06-16 18:16:18 +00:00
data, err := json.Marshal(api.Pod{JSONBase: api.JSONBase{ID: "foo"}})
2014-06-06 23:40:48 +00:00
expectNoError(t, err)
_, err = file.Write(data)
expectNoError(t, err)
request, err := RequestWithBody(file.Name(), "http://www.google.com", "GET")
if request == nil {
t.Error("Unexpected nil result")
}
if err != nil {
t.Errorf("Unexpected error: %#v")
}
dataOut, err := ioutil.ReadAll(request.Body)
expectNoError(t, err)
if string(data) != string(dataOut) {
t.Errorf("Mismatched data. Expected %s, got %s", data, dataOut)
}
}
2014-06-16 18:16:18 +00:00
func validatePort(t *testing.T, p api.Port, external int, internal int) {
2014-06-06 23:40:48 +00:00
if p.HostPort != external || p.ContainerPort != internal {
t.Errorf("Unexpected port: %#v != (%d, %d)", p, external, internal)
}
}
func TestMakePorts(t *testing.T) {
ports := makePorts("8080:80,8081:8081,443:444")
if len(ports) != 3 {
t.Errorf("Unexpected ports: %#v", ports)
}
validatePort(t, ports[0], 8080, 80)
validatePort(t, ports[1], 8081, 8081)
validatePort(t, ports[2], 443, 444)
}