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-26 00:55:43 +00:00
|
|
|
package kubecfg
|
2014-06-06 23:40:48 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
2014-06-21 13:03:02 +00:00
|
|
|
"os"
|
2014-07-21 05:34:06 +00:00
|
|
|
"reflect"
|
2014-06-06 23:40:48 +00:00
|
|
|
"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-23 00:02:48 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
|
2014-06-06 23:40:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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-23 00:02:48 +00:00
|
|
|
func (client *FakeKubeClient) ListPods(selector labels.Selector) (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)
|
2014-06-24 05:18:14 +00:00
|
|
|
// Update deletes the pods, it relies on the replication controller to replace them.
|
|
|
|
validateAction(Action{action: "delete-pod", value: "pod-1"}, client.actions[2], t)
|
|
|
|
validateAction(Action{action: "delete-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 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])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-12 17:03:17 +00:00
|
|
|
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)
|
2014-06-12 17:03:17 +00:00
|
|
|
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)
|
2014-07-21 01:28:55 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error: %v", err)
|
|
|
|
}
|
2014-06-06 23:40:48 +00:00
|
|
|
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.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-21 13:03:02 +00:00
|
|
|
func TestLoadAuthInfo(t *testing.T) {
|
|
|
|
testAuthInfo := &client.AuthInfo{
|
|
|
|
User: "TestUser",
|
|
|
|
Password: "TestPassword",
|
|
|
|
}
|
|
|
|
aifile, err := ioutil.TempFile("", "testAuthInfo")
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Could not open temp file")
|
|
|
|
}
|
|
|
|
defer os.Remove(aifile.Name())
|
|
|
|
defer aifile.Close()
|
|
|
|
|
|
|
|
ai, err := LoadAuthInfo(aifile.Name())
|
|
|
|
if err == nil {
|
|
|
|
t.Error("LoadAuthInfo didn't fail on empty file")
|
|
|
|
}
|
|
|
|
data, err := json.Marshal(testAuthInfo)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Unexpected JSON marshal error")
|
|
|
|
}
|
|
|
|
_, err = aifile.Write(data)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Unexpected error in writing test file")
|
|
|
|
}
|
|
|
|
ai, err = LoadAuthInfo(aifile.Name())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if *testAuthInfo != *ai {
|
|
|
|
t.Error("Test data and loaded data are not equal")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-06 23:40:48 +00:00
|
|
|
func TestMakePorts(t *testing.T) {
|
2014-07-21 05:34:06 +00:00
|
|
|
var makePortsTests = []struct {
|
|
|
|
spec string
|
|
|
|
ports []api.Port
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"8080:80,8081:8081,443:444",
|
|
|
|
[]api.Port{
|
|
|
|
api.Port{HostPort: 8080, ContainerPort: 80},
|
|
|
|
api.Port{HostPort: 8081, ContainerPort: 8081},
|
|
|
|
api.Port{HostPort: 443, ContainerPort: 444},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range makePortsTests {
|
|
|
|
ports := makePorts(tt.spec)
|
|
|
|
if !reflect.DeepEqual(ports, tt.ports) {
|
|
|
|
t.Errorf("Expected %#v, got %#v", tt.ports, ports)
|
|
|
|
}
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
}
|