2015-08-06 16:53:34 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 The Kubernetes Authors 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package etcd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"k8s.io/kubernetes/pkg/api"
|
2015-08-20 09:17:04 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/testapi"
|
2015-09-09 22:46:06 +00:00
|
|
|
"k8s.io/kubernetes/pkg/apis/experimental"
|
2015-09-03 13:27:01 +00:00
|
|
|
"k8s.io/kubernetes/pkg/registry/registrytest"
|
2015-08-06 16:53:34 +00:00
|
|
|
"k8s.io/kubernetes/pkg/runtime"
|
|
|
|
"k8s.io/kubernetes/pkg/tools"
|
|
|
|
"k8s.io/kubernetes/pkg/tools/etcdtest"
|
|
|
|
"k8s.io/kubernetes/pkg/util"
|
|
|
|
)
|
|
|
|
|
2015-09-03 13:27:01 +00:00
|
|
|
func newStorage(t *testing.T) (*ScaleREST, *tools.FakeEtcdClient) {
|
2015-09-17 05:15:05 +00:00
|
|
|
etcdStorage, fakeClient := registrytest.NewEtcdStorage(t, "")
|
2015-09-03 13:27:01 +00:00
|
|
|
return NewStorage(etcdStorage).Scale, fakeClient
|
2015-08-06 16:53:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var validPodTemplate = api.PodTemplate{
|
|
|
|
Template: api.PodTemplateSpec{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Labels: map[string]string{"a": "b"},
|
|
|
|
},
|
|
|
|
Spec: api.PodSpec{
|
|
|
|
Containers: []api.Container{
|
|
|
|
{
|
|
|
|
Name: "test",
|
|
|
|
Image: "test_image",
|
|
|
|
ImagePullPolicy: api.PullIfNotPresent,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
RestartPolicy: api.RestartPolicyAlways,
|
|
|
|
DNSPolicy: api.DNSClusterFirst,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var validReplicas = 8
|
|
|
|
|
|
|
|
var validControllerSpec = api.ReplicationControllerSpec{
|
|
|
|
Replicas: validReplicas,
|
|
|
|
Selector: validPodTemplate.Template.Labels,
|
|
|
|
Template: &validPodTemplate.Template,
|
|
|
|
}
|
|
|
|
|
|
|
|
var validController = api.ReplicationController{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: "foo", Namespace: "test", ResourceVersion: "1"},
|
|
|
|
Spec: validControllerSpec,
|
|
|
|
}
|
|
|
|
|
2015-09-09 22:46:06 +00:00
|
|
|
var validScale = experimental.Scale{
|
2015-08-06 16:53:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "foo", Namespace: "test"},
|
2015-09-09 22:46:06 +00:00
|
|
|
Spec: experimental.ScaleSpec{
|
2015-08-06 16:53:34 +00:00
|
|
|
Replicas: validReplicas,
|
|
|
|
},
|
2015-09-09 22:46:06 +00:00
|
|
|
Status: experimental.ScaleStatus{
|
2015-08-06 16:53:34 +00:00
|
|
|
Replicas: 0,
|
|
|
|
Selector: validPodTemplate.Template.Labels,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGet(t *testing.T) {
|
2015-09-03 13:27:01 +00:00
|
|
|
storage, fakeClient := newStorage(t)
|
2015-08-06 16:53:34 +00:00
|
|
|
|
2015-09-03 13:27:01 +00:00
|
|
|
ctx := api.WithNamespace(api.NewContext(), "test")
|
2015-08-06 16:53:34 +00:00
|
|
|
key := etcdtest.AddPrefix("/controllers/test/foo")
|
2015-09-17 05:15:05 +00:00
|
|
|
if _, err := fakeClient.Set(key, runtime.EncodeOrDie(testapi.Default.Codec(), &validController), 0); err != nil {
|
2015-09-03 13:27:01 +00:00
|
|
|
t.Fatalf("unexpected error: %v", err)
|
2015-08-06 16:53:34 +00:00
|
|
|
}
|
|
|
|
|
2015-09-03 13:27:01 +00:00
|
|
|
expect := &validScale
|
|
|
|
obj, err := storage.Get(ctx, "foo")
|
2015-09-09 22:46:06 +00:00
|
|
|
scale := obj.(*experimental.Scale)
|
2015-08-06 16:53:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
2015-08-11 09:42:48 +00:00
|
|
|
if e, a := expect, scale; !api.Semantic.DeepEqual(e, a) {
|
2015-09-03 13:27:01 +00:00
|
|
|
t.Errorf("unexpected scale: %s", util.ObjectDiff(e, a))
|
2015-08-06 16:53:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdate(t *testing.T) {
|
2015-09-03 13:27:01 +00:00
|
|
|
storage, fakeClient := newStorage(t)
|
2015-08-06 16:53:34 +00:00
|
|
|
|
2015-09-03 13:27:01 +00:00
|
|
|
ctx := api.WithNamespace(api.NewContext(), "test")
|
2015-08-06 16:53:34 +00:00
|
|
|
key := etcdtest.AddPrefix("/controllers/test/foo")
|
2015-09-17 05:15:05 +00:00
|
|
|
if _, err := fakeClient.Set(key, runtime.EncodeOrDie(testapi.Default.Codec(), &validController), 0); err != nil {
|
2015-09-03 13:27:01 +00:00
|
|
|
t.Fatalf("unexpected error: %v", err)
|
2015-08-06 16:53:34 +00:00
|
|
|
}
|
|
|
|
replicas := 12
|
2015-09-09 22:46:06 +00:00
|
|
|
update := experimental.Scale{
|
2015-08-06 16:53:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "foo", Namespace: "test"},
|
2015-09-09 22:46:06 +00:00
|
|
|
Spec: experimental.ScaleSpec{
|
2015-08-06 16:53:34 +00:00
|
|
|
Replicas: replicas,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-09-03 13:27:01 +00:00
|
|
|
if _, _, err := storage.Update(ctx, &update); err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
2015-08-06 16:53:34 +00:00
|
|
|
}
|
2015-09-03 13:27:01 +00:00
|
|
|
response, err := fakeClient.Get(key, false, false)
|
2015-08-06 16:53:34 +00:00
|
|
|
if err != nil {
|
2015-09-03 13:27:01 +00:00
|
|
|
t.Fatalf("unexpected error: %v", err)
|
2015-08-06 16:53:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var controller api.ReplicationController
|
2015-09-04 07:06:01 +00:00
|
|
|
testapi.Experimental.Codec().DecodeInto([]byte(response.Node.Value), &controller)
|
2015-08-06 16:53:34 +00:00
|
|
|
if controller.Spec.Replicas != replicas {
|
|
|
|
t.Errorf("wrong replicas count expected: %d got: %d", replicas, controller.Spec.Replicas)
|
|
|
|
}
|
|
|
|
}
|