k3s/pkg/registry/controller/rest_test.go

417 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-23 18:32:11 +00:00
package controller
2014-06-06 23:40:48 +00:00
import (
"encoding/json"
"fmt"
"io/ioutil"
"strings"
2014-06-06 23:40:48 +00:00
"testing"
"time"
2014-06-06 23:40:48 +00:00
2014-06-12 20:17:34 +00:00
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
_ "github.com/GoogleCloudPlatform/kubernetes/pkg/api/v1beta1"
2014-06-17 02:10:43 +00:00
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/registrytest"
2014-06-06 23:40:48 +00:00
)
func TestListControllersError(t *testing.T) {
mockRegistry := registrytest.ControllerRegistry{
Err: fmt.Errorf("test error"),
2014-06-06 23:40:48 +00:00
}
storage := REST{
2014-06-06 23:40:48 +00:00
registry: &mockRegistry,
}
2014-09-25 18:35:10 +00:00
ctx := api.NewContext()
controllers, err := storage.List(ctx, labels.Everything(), labels.Everything())
if err != mockRegistry.Err {
t.Errorf("Expected %#v, Got %#v", mockRegistry.Err, err)
2014-06-06 23:40:48 +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) {
mockRegistry := registrytest.ControllerRegistry{nil, &api.ReplicationControllerList{ListMeta: api.ListMeta{ResourceVersion: "1"}}}
storage := REST{
2014-06-06 23:40:48 +00:00
registry: &mockRegistry,
}
2014-09-25 18:35:10 +00:00
ctx := api.NewContext()
controllers, err := storage.List(ctx, labels.Everything(), labels.Everything())
2014-08-03 23:51:34 +00:00
if err != nil {
t.Errorf("unexpected error: %v", err)
}
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
}
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) {
mockRegistry := registrytest.ControllerRegistry{
Controllers: &api.ReplicationControllerList{
Items: []api.ReplicationController{
{
ObjectMeta: api.ObjectMeta{
2014-10-22 17:02:02 +00:00
Name: "foo",
},
2014-06-06 23:40:48 +00:00
},
{
ObjectMeta: api.ObjectMeta{
2014-10-22 17:02:02 +00:00
Name: "bar",
},
2014-06-06 23:40:48 +00:00
},
},
},
}
storage := REST{
2014-06-06 23:40:48 +00:00
registry: &mockRegistry,
}
2014-09-25 18:35:10 +00:00
ctx := api.NewContext()
controllersObj, err := storage.List(ctx, labels.Everything(), labels.Everything())
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)
}
2014-10-22 17:02:02 +00:00
if controllers.Items[0].Name != "foo" {
2014-06-06 23:40:48 +00:00
t.Errorf("Unexpected controller: %#v", controllers.Items[0])
}
2014-10-22 17:02:02 +00:00
if controllers.Items[1].Name != "bar" {
2014-06-06 23:40:48 +00:00
t.Errorf("Unexpected controller: %#v", controllers.Items[1])
}
}
func TestControllerDecode(t *testing.T) {
mockRegistry := registrytest.ControllerRegistry{}
storage := REST{
2014-06-06 23:40:48 +00:00
registry: &mockRegistry,
}
controller := &api.ReplicationController{
ObjectMeta: api.ObjectMeta{
2014-10-22 17:02:02 +00:00
Name: "foo",
2014-06-06 23:40:48 +00:00
},
Spec: api.ReplicationControllerSpec{
Template: &api.PodTemplateSpec{
ObjectMeta: api.ObjectMeta{
Labels: map[string]string{
"name": "nginx",
},
},
},
},
2014-06-06 23:40:48 +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)
}
controllerOut := storage.New()
if err := latest.Codec.DecodeInto(body, controllerOut); err != nil {
2014-08-03 23:51:34 +00:00
t.Errorf("unexpected error: %v", err)
}
if !api.Semantic.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{
ObjectMeta: api.ObjectMeta{
2014-10-22 17:02:02 +00:00
Name: "nginxController",
Labels: map[string]string{
"name": "nginx",
},
2014-06-06 23:40:48 +00:00
},
Spec: api.ReplicationControllerSpec{
2014-06-06 23:40:48 +00:00
Replicas: 2,
Selector: map[string]string{
2014-06-06 23:40:48 +00:00
"name": "nginx",
},
Template: &api.PodTemplateSpec{
ObjectMeta: api.ObjectMeta{
Labels: map[string]string{
"name": "nginx",
},
},
Spec: api.PodSpec{
Containers: []api.Container{
{
Image: "dockerfile/nginx",
Ports: []api.Port{
{
ContainerPort: 80,
HostPort: 8080,
2014-06-06 23:40:48 +00:00
},
},
},
},
},
},
},
}
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 !api.Semantic.DeepEqual(controller, expectedController) {
2014-06-06 23:40:48 +00:00
t.Errorf("Parsing failed: %s %#v %#v", string(data), controller, expectedController)
}
}
var validPodTemplate = api.PodTemplate{
Spec: api.PodTemplateSpec{
ObjectMeta: api.ObjectMeta{
Labels: map[string]string{"a": "b"},
},
Spec: api.PodSpec{
Containers: []api.Container{
{
Name: "test",
Image: "test_image",
},
},
},
},
}
func TestCreateController(t *testing.T) {
mockRegistry := registrytest.ControllerRegistry{}
mockPodRegistry := registrytest.PodRegistry{
Pods: &api.PodList{
Items: []api.Pod{
{
ObjectMeta: api.ObjectMeta{
Name: "foo",
Labels: map[string]string{"a": "b"},
},
},
},
},
}
storage := REST{
registry: &mockRegistry,
podLister: &mockPodRegistry,
pollPeriod: time.Millisecond * 1,
}
controller := &api.ReplicationController{
ObjectMeta: api.ObjectMeta{Name: "test"},
Spec: api.ReplicationControllerSpec{
Replicas: 2,
Selector: map[string]string{"a": "b"},
Template: &validPodTemplate.Spec,
},
}
ctx := api.NewDefaultContext()
2014-09-25 18:35:10 +00:00
channel, err := storage.Create(ctx, controller)
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-11-12 21:27:10 +00:00
if !api.HasObjectMetaSystemFieldValues(&controller.ObjectMeta) {
t.Errorf("storage did not populate object meta field values")
}
select {
case <-channel:
// expected case
case <-time.After(time.Millisecond * 100):
t.Error("Unexpected timeout from async channel")
}
}
func TestControllerStorageValidatesCreate(t *testing.T) {
mockRegistry := registrytest.ControllerRegistry{}
storage := REST{
registry: &mockRegistry,
podLister: nil,
pollPeriod: time.Millisecond * 1,
}
failureCases := map[string]api.ReplicationController{
"empty ID": {
ObjectMeta: api.ObjectMeta{Name: ""},
Spec: api.ReplicationControllerSpec{
Selector: map[string]string{"bar": "baz"},
},
},
"empty selector": {
ObjectMeta: api.ObjectMeta{Name: "abc"},
Spec: api.ReplicationControllerSpec{},
},
}
ctx := api.NewDefaultContext()
for _, failureCase := range failureCases {
2014-09-25 18:35:10 +00:00
c, err := storage.Create(ctx, &failureCase)
if c != nil {
t.Errorf("Expected nil channel")
}
if !errors.IsInvalid(err) {
t.Errorf("Expected to get an invalid resource error, got %v", err)
}
}
}
func TestControllerStorageValidatesUpdate(t *testing.T) {
mockRegistry := registrytest.ControllerRegistry{}
storage := REST{
registry: &mockRegistry,
podLister: nil,
pollPeriod: time.Millisecond * 1,
}
failureCases := map[string]api.ReplicationController{
"empty ID": {
ObjectMeta: api.ObjectMeta{Name: ""},
Spec: api.ReplicationControllerSpec{
Selector: map[string]string{"bar": "baz"},
},
},
"empty selector": {
ObjectMeta: api.ObjectMeta{Name: "abc"},
Spec: api.ReplicationControllerSpec{},
},
}
ctx := api.NewDefaultContext()
for _, failureCase := range failureCases {
2014-09-25 18:35:10 +00:00
c, err := storage.Update(ctx, &failureCase)
if c != nil {
t.Errorf("Expected nil channel")
}
if !errors.IsInvalid(err) {
t.Errorf("Expected to get an invalid resource error, got %v", err)
}
}
}
type fakePodLister struct {
e error
l api.PodList
s labels.Selector
}
2014-09-26 15:46:04 +00:00
func (f *fakePodLister) ListPods(ctx api.Context, 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{
{ObjectMeta: api.ObjectMeta{Name: "foo"}},
{ObjectMeta: api.ObjectMeta{Name: "bar"}},
},
},
}
mockRegistry := registrytest.ControllerRegistry{}
storage := REST{
registry: &mockRegistry,
podLister: &fakeLister,
}
controller := api.ReplicationController{
Spec: api.ReplicationControllerSpec{
Selector: map[string]string{
"foo": "bar",
},
},
}
2014-09-25 18:35:10 +00:00
ctx := api.NewContext()
storage.fillCurrentState(ctx, &controller)
if controller.Status.Replicas != 2 {
t.Errorf("expected 2, got: %d", controller.Status.Replicas)
}
if !api.Semantic.DeepEqual(fakeLister.s, labels.Set(controller.Spec.Selector).AsSelector()) {
t.Errorf("unexpected output: %#v %#v", labels.Set(controller.Spec.Selector).AsSelector(), fakeLister.s)
}
}
func TestCreateControllerWithConflictingNamespace(t *testing.T) {
storage := REST{}
controller := &api.ReplicationController{
ObjectMeta: api.ObjectMeta{Name: "test", Namespace: "not-default"},
}
ctx := api.NewDefaultContext()
channel, err := storage.Create(ctx, controller)
if channel != nil {
t.Error("Expected a nil channel, but we got a value")
}
if err == nil {
t.Errorf("Expected an error, but we didn't get one")
} else if strings.Index(err.Error(), "Controller.Namespace does not match the provided context") == -1 {
t.Errorf("Expected 'Controller.Namespace does not match the provided context' error, got '%v'", err.Error())
}
}
func TestUpdateControllerWithConflictingNamespace(t *testing.T) {
storage := REST{}
controller := &api.ReplicationController{
ObjectMeta: api.ObjectMeta{Name: "test", Namespace: "not-default"},
}
ctx := api.NewDefaultContext()
channel, err := storage.Update(ctx, controller)
if channel != nil {
t.Error("Expected a nil channel, but we got a value")
}
if err == nil {
t.Errorf("Expected an error, but we didn't get one")
} else if strings.Index(err.Error(), "Controller.Namespace does not match the provided context") == -1 {
t.Errorf("Expected 'Controller.Namespace does not match the provided context' error, got '%v'", err.Error())
}
}