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-08-11 07:34:59 +00:00
|
|
|
package controller
|
2014-06-06 23:40:48 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
2014-06-26 19:43:08 +00:00
|
|
|
"time"
|
2014-06-06 23:40:48 +00:00
|
|
|
|
2014-06-12 20:17:34 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
2014-09-03 21:16:00 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
|
2014-09-08 21:40:56 +00:00
|
|
|
_ "github.com/GoogleCloudPlatform/kubernetes/pkg/api/v1beta1"
|
2014-06-17 02:10:43 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
|
2014-08-11 07:34:59 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/registrytest"
|
2014-09-02 17:55:27 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
2014-06-06 23:40:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestListControllersError(t *testing.T) {
|
2014-08-11 07:34:59 +00:00
|
|
|
mockRegistry := registrytest.ControllerRegistry{
|
|
|
|
Err: fmt.Errorf("test error"),
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2014-09-08 21:40:56 +00:00
|
|
|
storage := REST{
|
2014-06-06 23:40:48 +00:00
|
|
|
registry: &mockRegistry,
|
|
|
|
}
|
2014-08-29 00:48:07 +00:00
|
|
|
controllers, err := storage.List(nil)
|
2014-08-11 07:34:59 +00:00
|
|
|
if err != mockRegistry.Err {
|
|
|
|
t.Errorf("Expected %#v, Got %#v", mockRegistry.Err, err)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2014-08-29 00:48:07 +00:00
|
|
|
if controllers != nil {
|
|
|
|
t.Errorf("Unexpected non-nil ctrl list: %#v", controllers)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestListEmptyControllerList(t *testing.T) {
|
2014-08-29 00:48:07 +00:00
|
|
|
mockRegistry := registrytest.ControllerRegistry{nil, &api.ReplicationControllerList{JSONBase: api.JSONBase{ResourceVersion: 1}}}
|
2014-09-08 21:40:56 +00:00
|
|
|
storage := REST{
|
2014-06-06 23:40:48 +00:00
|
|
|
registry: &mockRegistry,
|
|
|
|
}
|
2014-06-17 02:10:43 +00:00
|
|
|
controllers, err := storage.List(labels.Everything())
|
2014-08-03 23:51:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-08-29 00:48:07 +00:00
|
|
|
if len(controllers.(*api.ReplicationControllerList).Items) != 0 {
|
2014-06-09 05:38:45 +00:00
|
|
|
t.Errorf("Unexpected non-zero ctrl list: %#v", controllers)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2014-08-29 00:48:07 +00:00
|
|
|
if controllers.(*api.ReplicationControllerList).ResourceVersion != 1 {
|
|
|
|
t.Errorf("Unexpected resource version: %#v", controllers)
|
|
|
|
}
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestListControllerList(t *testing.T) {
|
2014-08-11 07:34:59 +00:00
|
|
|
mockRegistry := registrytest.ControllerRegistry{
|
2014-08-29 00:48:07 +00:00
|
|
|
Controllers: &api.ReplicationControllerList{
|
|
|
|
Items: []api.ReplicationController{
|
|
|
|
{
|
|
|
|
JSONBase: api.JSONBase{
|
|
|
|
ID: "foo",
|
|
|
|
},
|
2014-06-06 23:40:48 +00:00
|
|
|
},
|
2014-08-29 00:48:07 +00:00
|
|
|
{
|
|
|
|
JSONBase: api.JSONBase{
|
|
|
|
ID: "bar",
|
|
|
|
},
|
2014-06-06 23:40:48 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2014-09-08 21:40:56 +00:00
|
|
|
storage := REST{
|
2014-06-06 23:40:48 +00:00
|
|
|
registry: &mockRegistry,
|
|
|
|
}
|
2014-06-17 02:10:43 +00:00
|
|
|
controllersObj, err := storage.List(labels.Everything())
|
2014-08-29 00:48:07 +00:00
|
|
|
controllers := controllersObj.(*api.ReplicationControllerList)
|
2014-08-03 23:51:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-06-06 23:40:48 +00:00
|
|
|
if len(controllers.Items) != 2 {
|
|
|
|
t.Errorf("Unexpected controller list: %#v", controllers)
|
|
|
|
}
|
|
|
|
if controllers.Items[0].ID != "foo" {
|
|
|
|
t.Errorf("Unexpected controller: %#v", controllers.Items[0])
|
|
|
|
}
|
|
|
|
if controllers.Items[1].ID != "bar" {
|
|
|
|
t.Errorf("Unexpected controller: %#v", controllers.Items[1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-06 03:10:48 +00:00
|
|
|
func TestControllerDecode(t *testing.T) {
|
2014-08-11 07:34:59 +00:00
|
|
|
mockRegistry := registrytest.ControllerRegistry{}
|
2014-09-08 21:40:56 +00:00
|
|
|
storage := REST{
|
2014-06-06 23:40:48 +00:00
|
|
|
registry: &mockRegistry,
|
|
|
|
}
|
2014-08-06 03:10:48 +00:00
|
|
|
controller := &api.ReplicationController{
|
2014-06-12 20:17:34 +00:00
|
|
|
JSONBase: api.JSONBase{
|
2014-06-06 23:40:48 +00:00
|
|
|
ID: "foo",
|
|
|
|
},
|
|
|
|
}
|
2014-09-16 19:56:26 +00:00
|
|
|
body, err := latest.Codec.Encode(controller)
|
2014-08-03 23:51:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-08-06 03:10:48 +00:00
|
|
|
controllerOut := storage.New()
|
2014-09-16 19:56:26 +00:00
|
|
|
if err := latest.Codec.DecodeInto(body, controllerOut); err != nil {
|
2014-08-03 23:51:34 +00:00
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-06-09 04:00:59 +00:00
|
|
|
if !reflect.DeepEqual(controller, controllerOut) {
|
2014-06-06 23:40:48 +00:00
|
|
|
t.Errorf("Expected %#v, found %#v", controller, controllerOut)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestControllerParsing(t *testing.T) {
|
2014-06-12 20:17:34 +00:00
|
|
|
expectedController := api.ReplicationController{
|
|
|
|
JSONBase: api.JSONBase{
|
2014-06-06 23:40:48 +00:00
|
|
|
ID: "nginxController",
|
|
|
|
},
|
2014-06-12 20:17:34 +00:00
|
|
|
DesiredState: api.ReplicationControllerState{
|
2014-06-06 23:40:48 +00:00
|
|
|
Replicas: 2,
|
2014-06-19 00:20:34 +00:00
|
|
|
ReplicaSelector: map[string]string{
|
2014-06-06 23:40:48 +00:00
|
|
|
"name": "nginx",
|
|
|
|
},
|
2014-06-12 20:17:34 +00:00
|
|
|
PodTemplate: api.PodTemplate{
|
|
|
|
DesiredState: api.PodState{
|
|
|
|
Manifest: api.ContainerManifest{
|
|
|
|
Containers: []api.Container{
|
2014-06-12 21:09:40 +00:00
|
|
|
{
|
2014-06-06 23:40:48 +00:00
|
|
|
Image: "dockerfile/nginx",
|
2014-06-12 20:17:34 +00:00
|
|
|
Ports: []api.Port{
|
2014-06-12 21:09:40 +00:00
|
|
|
{
|
2014-06-06 23:40:48 +00:00
|
|
|
ContainerPort: 80,
|
|
|
|
HostPort: 8080,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Labels: map[string]string{
|
|
|
|
"name": "nginx",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Labels: map[string]string{
|
|
|
|
"name": "nginx",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
file, err := ioutil.TempFile("", "controller")
|
|
|
|
fileName := file.Name()
|
2014-08-03 23:51:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-06-06 23:40:48 +00:00
|
|
|
data, err := json.Marshal(expectedController)
|
2014-08-03 23:51:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-06-06 23:40:48 +00:00
|
|
|
_, err = file.Write(data)
|
2014-08-03 23:51:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-06-06 23:40:48 +00:00
|
|
|
err = file.Close()
|
2014-08-03 23:51:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-06-06 23:40:48 +00:00
|
|
|
data, err = ioutil.ReadFile(fileName)
|
2014-08-03 23:51:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-06-12 20:17:34 +00:00
|
|
|
var controller api.ReplicationController
|
2014-06-06 23:40:48 +00:00
|
|
|
err = json.Unmarshal(data, &controller)
|
2014-08-03 23:51:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
2014-06-06 23:40:48 +00:00
|
|
|
|
|
|
|
if !reflect.DeepEqual(controller, expectedController) {
|
|
|
|
t.Errorf("Parsing failed: %s %#v %#v", string(data), controller, expectedController)
|
|
|
|
}
|
|
|
|
}
|
2014-06-26 19:43:08 +00:00
|
|
|
|
2014-07-25 16:18:48 +00:00
|
|
|
var validPodTemplate = api.PodTemplate{
|
|
|
|
DesiredState: api.PodState{
|
|
|
|
Manifest: api.ContainerManifest{
|
|
|
|
Version: "v1beta1",
|
|
|
|
Containers: []api.Container{
|
|
|
|
{
|
|
|
|
Name: "test",
|
|
|
|
Image: "test_image",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2014-08-22 00:02:39 +00:00
|
|
|
Labels: map[string]string{"a": "b"},
|
2014-07-25 16:18:48 +00:00
|
|
|
}
|
|
|
|
|
2014-06-26 19:43:08 +00:00
|
|
|
func TestCreateController(t *testing.T) {
|
2014-08-11 07:34:59 +00:00
|
|
|
mockRegistry := registrytest.ControllerRegistry{}
|
|
|
|
mockPodRegistry := registrytest.PodRegistry{
|
2014-08-29 00:48:07 +00:00
|
|
|
Pods: &api.PodList{
|
|
|
|
Items: []api.Pod{
|
|
|
|
{
|
|
|
|
JSONBase: api.JSONBase{ID: "foo"},
|
|
|
|
Labels: map[string]string{"a": "b"},
|
|
|
|
},
|
2014-06-26 19:43:08 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2014-09-08 21:40:56 +00:00
|
|
|
storage := REST{
|
|
|
|
registry: &mockRegistry,
|
|
|
|
podLister: &mockPodRegistry,
|
|
|
|
pollPeriod: time.Millisecond * 1,
|
2014-06-26 19:43:08 +00:00
|
|
|
}
|
2014-08-06 03:10:48 +00:00
|
|
|
controller := &api.ReplicationController{
|
2014-06-26 19:43:08 +00:00
|
|
|
JSONBase: api.JSONBase{ID: "test"},
|
|
|
|
DesiredState: api.ReplicationControllerState{
|
2014-07-25 16:18:48 +00:00
|
|
|
Replicas: 2,
|
|
|
|
ReplicaSelector: map[string]string{"a": "b"},
|
|
|
|
PodTemplate: validPodTemplate,
|
2014-06-26 19:43:08 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
channel, err := storage.Create(controller)
|
2014-07-25 16:18:48 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error: %v", err)
|
|
|
|
}
|
2014-08-03 23:51:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
2014-06-26 19:43:08 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-channel:
|
2014-08-18 21:42:08 +00:00
|
|
|
// expected case
|
|
|
|
case <-time.After(time.Millisecond * 100):
|
|
|
|
t.Error("Unexpected timeout from async channel")
|
2014-06-26 19:43:08 +00:00
|
|
|
}
|
|
|
|
}
|
2014-07-25 16:18:48 +00:00
|
|
|
|
|
|
|
func TestControllerStorageValidatesCreate(t *testing.T) {
|
2014-08-11 07:34:59 +00:00
|
|
|
mockRegistry := registrytest.ControllerRegistry{}
|
2014-09-08 21:40:56 +00:00
|
|
|
storage := REST{
|
|
|
|
registry: &mockRegistry,
|
|
|
|
podLister: nil,
|
|
|
|
pollPeriod: time.Millisecond * 1,
|
2014-07-25 16:18:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
failureCases := map[string]api.ReplicationController{
|
2014-07-20 19:00:52 +00:00
|
|
|
"empty ID": {
|
2014-07-25 16:18:48 +00:00
|
|
|
JSONBase: api.JSONBase{ID: ""},
|
|
|
|
DesiredState: api.ReplicationControllerState{
|
|
|
|
ReplicaSelector: map[string]string{"bar": "baz"},
|
|
|
|
},
|
|
|
|
},
|
2014-07-20 19:00:52 +00:00
|
|
|
"empty selector": {
|
2014-07-25 16:18:48 +00:00
|
|
|
JSONBase: api.JSONBase{ID: "abc"},
|
|
|
|
DesiredState: api.ReplicationControllerState{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, failureCase := range failureCases {
|
2014-08-06 03:10:48 +00:00
|
|
|
c, err := storage.Create(&failureCase)
|
2014-07-25 16:18:48 +00:00
|
|
|
if c != nil {
|
|
|
|
t.Errorf("Expected nil channel")
|
|
|
|
}
|
2014-09-03 21:16:00 +00:00
|
|
|
if !errors.IsInvalid(err) {
|
2014-08-22 00:24:53 +00:00
|
|
|
t.Errorf("Expected to get an invalid resource error, got %v", err)
|
2014-07-25 16:18:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestControllerStorageValidatesUpdate(t *testing.T) {
|
2014-08-11 07:34:59 +00:00
|
|
|
mockRegistry := registrytest.ControllerRegistry{}
|
2014-09-08 21:40:56 +00:00
|
|
|
storage := REST{
|
|
|
|
registry: &mockRegistry,
|
|
|
|
podLister: nil,
|
|
|
|
pollPeriod: time.Millisecond * 1,
|
2014-07-25 16:18:48 +00:00
|
|
|
}
|
|
|
|
failureCases := map[string]api.ReplicationController{
|
2014-07-20 19:00:52 +00:00
|
|
|
"empty ID": {
|
2014-07-25 16:18:48 +00:00
|
|
|
JSONBase: api.JSONBase{ID: ""},
|
|
|
|
DesiredState: api.ReplicationControllerState{
|
|
|
|
ReplicaSelector: map[string]string{"bar": "baz"},
|
|
|
|
},
|
|
|
|
},
|
2014-07-20 19:00:52 +00:00
|
|
|
"empty selector": {
|
2014-07-25 16:18:48 +00:00
|
|
|
JSONBase: api.JSONBase{ID: "abc"},
|
|
|
|
DesiredState: api.ReplicationControllerState{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, failureCase := range failureCases {
|
2014-08-06 03:10:48 +00:00
|
|
|
c, err := storage.Update(&failureCase)
|
2014-07-25 16:18:48 +00:00
|
|
|
if c != nil {
|
|
|
|
t.Errorf("Expected nil channel")
|
|
|
|
}
|
2014-09-03 21:16:00 +00:00
|
|
|
if !errors.IsInvalid(err) {
|
2014-08-22 00:24:53 +00:00
|
|
|
t.Errorf("Expected to get an invalid resource error, got %v", err)
|
2014-07-25 16:18:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-09-09 22:45:31 +00:00
|
|
|
|
|
|
|
type fakePodLister struct {
|
|
|
|
e error
|
|
|
|
l api.PodList
|
|
|
|
s labels.Selector
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *fakePodLister) ListPods(s labels.Selector) (*api.PodList, error) {
|
|
|
|
f.s = s
|
|
|
|
return &f.l, f.e
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFillCurrentState(t *testing.T) {
|
|
|
|
fakeLister := fakePodLister{
|
|
|
|
l: api.PodList{
|
|
|
|
Items: []api.Pod{
|
|
|
|
{JSONBase: api.JSONBase{ID: "foo"}},
|
|
|
|
{JSONBase: api.JSONBase{ID: "bar"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
mockRegistry := registrytest.ControllerRegistry{}
|
|
|
|
storage := REST{
|
|
|
|
registry: &mockRegistry,
|
|
|
|
podLister: &fakeLister,
|
|
|
|
}
|
|
|
|
controller := api.ReplicationController{
|
|
|
|
DesiredState: api.ReplicationControllerState{
|
|
|
|
ReplicaSelector: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
storage.fillCurrentState(&controller)
|
|
|
|
if controller.CurrentState.Replicas != 2 {
|
|
|
|
t.Errorf("expected 2, got: %d", controller.CurrentState.Replicas)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(fakeLister.s, labels.Set(controller.DesiredState.ReplicaSelector).AsSelector()) {
|
|
|
|
t.Errorf("unexpected output: %#v %#v", labels.Set(controller.DesiredState.ReplicaSelector).AsSelector(), fakeLister.s)
|
|
|
|
}
|
|
|
|
}
|