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"
|
2015-03-06 00:52:43 +00:00
|
|
|
"reflect"
|
2014-10-08 20:26:57 +00:00
|
|
|
"strings"
|
2014-06-06 23:40:48 +00:00
|
|
|
"testing"
|
|
|
|
|
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-11 17:02:53 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
|
2015-01-27 23:56:38 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/rest/resttest"
|
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-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-09-25 18:35:10 +00:00
|
|
|
ctx := api.NewContext()
|
|
|
|
controllers, err := storage.List(ctx, labels.Everything(), labels.Everything())
|
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) {
|
2015-01-29 04:11:29 +00:00
|
|
|
mockRegistry := registrytest.ControllerRegistry{
|
|
|
|
Controllers: &api.ReplicationControllerList{ListMeta: api.ListMeta{ResourceVersion: "1"}},
|
|
|
|
}
|
2014-09-08 21:40:56 +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())
|
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-10-07 20:51:28 +00:00
|
|
|
if controllers.(*api.ReplicationControllerList).ResourceVersion != "1" {
|
2014-08-29 00:48:07 +00:00
|
|
|
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{
|
|
|
|
{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{
|
2014-10-22 17:02:02 +00:00
|
|
|
Name: "foo",
|
2014-08-29 00:48:07 +00:00
|
|
|
},
|
2014-06-06 23:40:48 +00:00
|
|
|
},
|
2014-08-29 00:48:07 +00:00
|
|
|
{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{
|
2014-10-22 17:02:02 +00:00
|
|
|
Name: "bar",
|
2014-08-29 00:48:07 +00:00
|
|
|
},
|
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-09-25 18:35:10 +00:00
|
|
|
ctx := api.NewContext()
|
|
|
|
controllersObj, err := storage.List(ctx, labels.Everything(), 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)
|
|
|
|
}
|
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])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-27 23:56:38 +00:00
|
|
|
// TODO: remove, this is sufficiently covered by other tests
|
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-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{
|
2014-10-22 17:02:02 +00:00
|
|
|
Name: "foo",
|
2014-06-06 23:40:48 +00:00
|
|
|
},
|
2014-11-07 02:09:46 +00:00
|
|
|
Spec: api.ReplicationControllerSpec{
|
|
|
|
Template: &api.PodTemplateSpec{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Labels: map[string]string{
|
|
|
|
"name": "nginx",
|
|
|
|
},
|
|
|
|
},
|
2015-01-26 17:52:50 +00:00
|
|
|
Spec: api.PodSpec{
|
|
|
|
RestartPolicy: api.RestartPolicy{Always: &api.RestartPolicyAlways{}},
|
|
|
|
DNSPolicy: api.DNSClusterFirst,
|
|
|
|
},
|
2014-11-07 02:09:46 +00:00
|
|
|
},
|
|
|
|
},
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
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)
|
|
|
|
}
|
|
|
|
|
2015-01-05 22:37:07 +00:00
|
|
|
if !api.Semantic.DeepEqual(controller, controllerOut) {
|
2014-06-06 23:40:48 +00:00
|
|
|
t.Errorf("Expected %#v, found %#v", controller, controllerOut)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-27 23:56:38 +00:00
|
|
|
// TODO: this is sufficiently covered by other tetss
|
2014-06-06 23:40:48 +00:00
|
|
|
func TestControllerParsing(t *testing.T) {
|
2014-06-12 20:17:34 +00:00
|
|
|
expectedController := api.ReplicationController{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{
|
2015-01-20 19:42:00 +00:00
|
|
|
Name: "nginx-controller",
|
2014-10-23 20:51:34 +00:00
|
|
|
Labels: map[string]string{
|
|
|
|
"name": "nginx",
|
|
|
|
},
|
2014-06-06 23:40:48 +00:00
|
|
|
},
|
2014-11-07 02:09:46 +00:00
|
|
|
Spec: api.ReplicationControllerSpec{
|
2014-06-06 23:40:48 +00:00
|
|
|
Replicas: 2,
|
2014-11-07 02:09:46 +00:00
|
|
|
Selector: map[string]string{
|
2014-06-06 23:40:48 +00:00
|
|
|
"name": "nginx",
|
|
|
|
},
|
2014-11-07 02:09:46 +00:00
|
|
|
Template: &api.PodTemplateSpec{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Labels: map[string]string{
|
|
|
|
"name": "nginx",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Spec: api.PodSpec{
|
|
|
|
Containers: []api.Container{
|
|
|
|
{
|
|
|
|
Image: "dockerfile/nginx",
|
2015-02-23 22:25:56 +00:00
|
|
|
Ports: []api.ContainerPort{
|
2014-11-07 02:09:46 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2015-01-05 22:37:07 +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)
|
|
|
|
}
|
|
|
|
}
|
2014-06-26 19:43:08 +00:00
|
|
|
|
2014-07-25 16:18:48 +00:00
|
|
|
var validPodTemplate = api.PodTemplate{
|
2014-11-07 02:09:46 +00:00
|
|
|
Spec: api.PodTemplateSpec{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Labels: map[string]string{"a": "b"},
|
|
|
|
},
|
|
|
|
Spec: api.PodSpec{
|
2014-07-25 16:18:48 +00:00
|
|
|
Containers: []api.Container{
|
|
|
|
{
|
2015-01-26 17:52:50 +00:00
|
|
|
Name: "test",
|
|
|
|
Image: "test_image",
|
|
|
|
ImagePullPolicy: api.PullIfNotPresent,
|
2014-07-25 16:18:48 +00:00
|
|
|
},
|
|
|
|
},
|
2015-01-26 17:52:50 +00:00
|
|
|
RestartPolicy: api.RestartPolicy{Always: &api.RestartPolicyAlways{}},
|
|
|
|
DNSPolicy: api.DNSClusterFirst,
|
2014-07-25 16:18:48 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-01-27 23:56:38 +00:00
|
|
|
// TODO: remove, this is sufficiently covered by other tests
|
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{
|
|
|
|
{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
Labels: map[string]string{"a": "b"},
|
|
|
|
},
|
2014-08-29 00:48:07 +00:00
|
|
|
},
|
2014-06-26 19:43:08 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2014-09-08 21:40:56 +00:00
|
|
|
storage := REST{
|
2015-01-29 04:41:37 +00:00
|
|
|
registry: &mockRegistry,
|
|
|
|
podLister: &mockPodRegistry,
|
2014-06-26 19:43:08 +00:00
|
|
|
}
|
2014-08-06 03:10:48 +00:00
|
|
|
controller := &api.ReplicationController{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "test"},
|
2014-11-07 02:09:46 +00:00
|
|
|
Spec: api.ReplicationControllerSpec{
|
|
|
|
Replicas: 2,
|
|
|
|
Selector: map[string]string{"a": "b"},
|
|
|
|
Template: &validPodTemplate.Spec,
|
2014-06-26 19:43:08 +00:00
|
|
|
},
|
|
|
|
}
|
2014-09-29 21:18:18 +00:00
|
|
|
ctx := api.NewDefaultContext()
|
2015-02-10 17:49:56 +00:00
|
|
|
obj, err := storage.Create(ctx, controller)
|
2014-07-25 16:18:48 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error: %v", err)
|
|
|
|
}
|
2015-02-10 17:49:56 +00:00
|
|
|
if obj == nil {
|
|
|
|
t.Errorf("unexpected object")
|
2014-08-03 23:51:34 +00:00
|
|
|
}
|
2014-11-12 21:27:10 +00:00
|
|
|
if !api.HasObjectMetaSystemFieldValues(&controller.ObjectMeta) {
|
|
|
|
t.Errorf("storage did not populate object meta field values")
|
|
|
|
}
|
2014-06-26 19:43:08 +00:00
|
|
|
|
|
|
|
}
|
2014-07-25 16:18:48 +00:00
|
|
|
|
2015-01-27 23:56:38 +00:00
|
|
|
// TODO: remove, covered by TestCreate
|
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{
|
2015-01-29 04:41:37 +00:00
|
|
|
registry: &mockRegistry,
|
|
|
|
podLister: nil,
|
2014-07-25 16:18:48 +00:00
|
|
|
}
|
|
|
|
failureCases := map[string]api.ReplicationController{
|
2014-07-20 19:00:52 +00:00
|
|
|
"empty ID": {
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: ""},
|
2014-11-07 02:09:46 +00:00
|
|
|
Spec: api.ReplicationControllerSpec{
|
|
|
|
Selector: map[string]string{"bar": "baz"},
|
2014-07-25 16:18:48 +00:00
|
|
|
},
|
|
|
|
},
|
2014-07-20 19:00:52 +00:00
|
|
|
"empty selector": {
|
2014-11-07 02:09:46 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "abc"},
|
|
|
|
Spec: api.ReplicationControllerSpec{},
|
2014-07-25 16:18:48 +00:00
|
|
|
},
|
|
|
|
}
|
2014-09-29 21:18:18 +00:00
|
|
|
ctx := api.NewDefaultContext()
|
2014-07-25 16:18:48 +00:00
|
|
|
for _, failureCase := range failureCases {
|
2014-09-25 18:35:10 +00:00
|
|
|
c, err := storage.Create(ctx, &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{
|
2015-01-29 04:41:37 +00:00
|
|
|
registry: &mockRegistry,
|
|
|
|
podLister: nil,
|
2014-07-25 16:18:48 +00:00
|
|
|
}
|
|
|
|
failureCases := map[string]api.ReplicationController{
|
2014-07-20 19:00:52 +00:00
|
|
|
"empty ID": {
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: ""},
|
2014-11-07 02:09:46 +00:00
|
|
|
Spec: api.ReplicationControllerSpec{
|
|
|
|
Selector: map[string]string{"bar": "baz"},
|
2014-07-25 16:18:48 +00:00
|
|
|
},
|
|
|
|
},
|
2014-07-20 19:00:52 +00:00
|
|
|
"empty selector": {
|
2014-11-07 02:09:46 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "abc"},
|
|
|
|
Spec: api.ReplicationControllerSpec{},
|
2014-07-25 16:18:48 +00:00
|
|
|
},
|
|
|
|
}
|
2014-09-29 21:18:18 +00:00
|
|
|
ctx := api.NewDefaultContext()
|
2014-07-25 16:18:48 +00:00
|
|
|
for _, failureCase := range failureCases {
|
2015-02-10 17:49:56 +00:00
|
|
|
c, created, err := storage.Update(ctx, &failureCase)
|
|
|
|
if c != nil || created {
|
|
|
|
t.Errorf("Expected nil object and not created")
|
2014-07-25 16:18:48 +00:00
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
2014-09-26 15:46:04 +00:00
|
|
|
func (f *fakePodLister) ListPods(ctx api.Context, s labels.Selector) (*api.PodList, error) {
|
2014-09-09 22:45:31 +00:00
|
|
|
f.s = s
|
|
|
|
return &f.l, f.e
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFillCurrentState(t *testing.T) {
|
|
|
|
fakeLister := fakePodLister{
|
|
|
|
l: api.PodList{
|
|
|
|
Items: []api.Pod{
|
2014-10-23 20:51:34 +00:00
|
|
|
{ObjectMeta: api.ObjectMeta{Name: "foo"}},
|
|
|
|
{ObjectMeta: api.ObjectMeta{Name: "bar"}},
|
2014-09-09 22:45:31 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
mockRegistry := registrytest.ControllerRegistry{}
|
|
|
|
storage := REST{
|
|
|
|
registry: &mockRegistry,
|
|
|
|
podLister: &fakeLister,
|
|
|
|
}
|
|
|
|
controller := api.ReplicationController{
|
2014-11-07 02:09:46 +00:00
|
|
|
Spec: api.ReplicationControllerSpec{
|
|
|
|
Selector: map[string]string{
|
2014-09-09 22:45:31 +00:00
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2014-09-25 18:35:10 +00:00
|
|
|
ctx := api.NewContext()
|
|
|
|
storage.fillCurrentState(ctx, &controller)
|
2014-11-07 02:09:46 +00:00
|
|
|
if controller.Status.Replicas != 2 {
|
|
|
|
t.Errorf("expected 2, got: %d", controller.Status.Replicas)
|
2014-09-09 22:45:31 +00:00
|
|
|
}
|
2015-03-06 00:52:43 +00:00
|
|
|
if !reflect.DeepEqual(fakeLister.s, labels.Set(controller.Spec.Selector).AsSelector()) {
|
2014-11-07 02:09:46 +00:00
|
|
|
t.Errorf("unexpected output: %#v %#v", labels.Set(controller.Spec.Selector).AsSelector(), fakeLister.s)
|
2014-09-09 22:45:31 +00:00
|
|
|
}
|
|
|
|
}
|
2014-10-08 20:26:57 +00:00
|
|
|
|
2015-01-27 23:56:38 +00:00
|
|
|
// TODO: remove, covered by TestCreate
|
|
|
|
func TestCreateControllerWithGeneratedName(t *testing.T) {
|
|
|
|
storage := NewREST(®istrytest.ControllerRegistry{}, nil)
|
|
|
|
controller := &api.ReplicationController{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Namespace: api.NamespaceDefault,
|
|
|
|
GenerateName: "rc-",
|
|
|
|
},
|
|
|
|
Spec: api.ReplicationControllerSpec{
|
|
|
|
Replicas: 2,
|
|
|
|
Selector: map[string]string{"a": "b"},
|
|
|
|
Template: &validPodTemplate.Spec,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := api.NewDefaultContext()
|
|
|
|
_, err := storage.Create(ctx, controller)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
if controller.Name == "rc-" || !strings.HasPrefix(controller.Name, "rc-") {
|
|
|
|
t.Errorf("unexpected name: %#v", controller)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: remove, covered by TestCreate
|
2014-10-08 20:26:57 +00:00
|
|
|
func TestCreateControllerWithConflictingNamespace(t *testing.T) {
|
|
|
|
storage := REST{}
|
|
|
|
controller := &api.ReplicationController{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "test", Namespace: "not-default"},
|
2014-10-08 20:26:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
2015-01-27 23:56:38 +00:00
|
|
|
} else if strings.Contains(err.Error(), "Controller.Namespace does not match the provided context") {
|
2014-10-08 20:26:57 +00:00
|
|
|
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{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "test", Namespace: "not-default"},
|
2014-10-08 20:26:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx := api.NewDefaultContext()
|
2015-02-10 17:49:56 +00:00
|
|
|
obj, created, err := storage.Update(ctx, controller)
|
|
|
|
if obj != nil || created {
|
|
|
|
t.Error("Expected a nil object, but we got a value or created was true")
|
2014-10-08 20:26:57 +00:00
|
|
|
}
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|
2015-01-27 23:56:38 +00:00
|
|
|
|
|
|
|
func TestCreate(t *testing.T) {
|
2015-01-29 04:11:29 +00:00
|
|
|
registry := ®istrytest.ControllerRegistry{}
|
|
|
|
test := resttest.New(t, NewREST(registry, nil), registry.SetError)
|
2015-01-27 23:56:38 +00:00
|
|
|
test.TestCreate(
|
|
|
|
// valid
|
|
|
|
&api.ReplicationController{
|
|
|
|
Spec: api.ReplicationControllerSpec{
|
|
|
|
Replicas: 2,
|
|
|
|
Selector: map[string]string{"a": "b"},
|
|
|
|
Template: &validPodTemplate.Spec,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// invalid
|
|
|
|
&api.ReplicationController{
|
|
|
|
Spec: api.ReplicationControllerSpec{
|
|
|
|
Replicas: 2,
|
|
|
|
Selector: map[string]string{},
|
|
|
|
Template: &validPodTemplate.Spec,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|