2016-01-18 20:32:44 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2016 The Kubernetes Authors.
|
2016-01-18 20:32:44 +00:00
|
|
|
|
|
|
|
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 replicaset
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2017-01-11 14:09:48 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2017-01-17 10:38:25 +00:00
|
|
|
genericapirequest "k8s.io/apiserver/pkg/endpoints/request"
|
2016-01-18 20:32:44 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api"
|
|
|
|
"k8s.io/kubernetes/pkg/apis/extensions"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestReplicaSetStrategy(t *testing.T) {
|
2017-01-02 14:07:36 +00:00
|
|
|
ctx := genericapirequest.NewDefaultContext()
|
2016-01-18 20:32:44 +00:00
|
|
|
if !Strategy.NamespaceScoped() {
|
|
|
|
t.Errorf("ReplicaSet must be namespace scoped")
|
|
|
|
}
|
|
|
|
if Strategy.AllowCreateOnUpdate() {
|
|
|
|
t.Errorf("ReplicaSet should not allow create on update")
|
|
|
|
}
|
|
|
|
|
|
|
|
validSelector := map[string]string{"a": "b"}
|
|
|
|
validPodTemplate := api.PodTemplate{
|
|
|
|
Template: api.PodTemplateSpec{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2016-01-18 20:32:44 +00:00
|
|
|
Labels: validSelector,
|
|
|
|
},
|
|
|
|
Spec: api.PodSpec{
|
|
|
|
RestartPolicy: api.RestartPolicyAlways,
|
|
|
|
DNSPolicy: api.DNSClusterFirst,
|
2017-01-16 04:56:22 +00:00
|
|
|
Containers: []api.Container{{Name: "abc", Image: "image", ImagePullPolicy: "IfNotPresent", TerminationMessagePolicy: api.TerminationMessageReadFile}},
|
2016-01-18 20:32:44 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
rs := &extensions.ReplicaSet{
|
2017-01-22 03:36:02 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "abc", Namespace: metav1.NamespaceDefault},
|
2016-01-18 20:32:44 +00:00
|
|
|
Spec: extensions.ReplicaSetSpec{
|
2016-12-03 18:57:26 +00:00
|
|
|
Selector: &metav1.LabelSelector{MatchLabels: validSelector},
|
2016-03-09 21:11:13 +00:00
|
|
|
Template: validPodTemplate.Template,
|
2016-01-18 20:32:44 +00:00
|
|
|
},
|
|
|
|
Status: extensions.ReplicaSetStatus{
|
|
|
|
Replicas: 1,
|
|
|
|
ObservedGeneration: int64(10),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-08-08 20:15:33 +00:00
|
|
|
Strategy.PrepareForCreate(ctx, rs)
|
2016-01-18 20:32:44 +00:00
|
|
|
if rs.Status.Replicas != 0 {
|
|
|
|
t.Error("ReplicaSet should not allow setting status.replicas on create")
|
|
|
|
}
|
|
|
|
if rs.Status.ObservedGeneration != int64(0) {
|
|
|
|
t.Error("ReplicaSet should not allow setting status.observedGeneration on create")
|
|
|
|
}
|
|
|
|
errs := Strategy.Validate(ctx, rs)
|
|
|
|
if len(errs) != 0 {
|
|
|
|
t.Errorf("Unexpected error validating %v", errs)
|
|
|
|
}
|
|
|
|
|
|
|
|
invalidRc := &extensions.ReplicaSet{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "bar", ResourceVersion: "4"},
|
2016-01-18 20:32:44 +00:00
|
|
|
}
|
2016-08-08 20:15:33 +00:00
|
|
|
Strategy.PrepareForUpdate(ctx, invalidRc, rs)
|
2016-01-18 20:32:44 +00:00
|
|
|
errs = Strategy.ValidateUpdate(ctx, invalidRc, rs)
|
|
|
|
if len(errs) == 0 {
|
|
|
|
t.Errorf("Expected a validation error")
|
|
|
|
}
|
|
|
|
if invalidRc.ResourceVersion != "4" {
|
|
|
|
t.Errorf("Incoming resource version on update should not be mutated")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReplicaSetStatusStrategy(t *testing.T) {
|
2017-01-02 14:07:36 +00:00
|
|
|
ctx := genericapirequest.NewDefaultContext()
|
2016-01-18 20:32:44 +00:00
|
|
|
if !StatusStrategy.NamespaceScoped() {
|
|
|
|
t.Errorf("ReplicaSet must be namespace scoped")
|
|
|
|
}
|
|
|
|
if StatusStrategy.AllowCreateOnUpdate() {
|
|
|
|
t.Errorf("ReplicaSet should not allow create on update")
|
|
|
|
}
|
|
|
|
validSelector := map[string]string{"a": "b"}
|
|
|
|
validPodTemplate := api.PodTemplate{
|
|
|
|
Template: api.PodTemplateSpec{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2016-01-18 20:32:44 +00:00
|
|
|
Labels: validSelector,
|
|
|
|
},
|
|
|
|
Spec: api.PodSpec{
|
|
|
|
RestartPolicy: api.RestartPolicyAlways,
|
|
|
|
DNSPolicy: api.DNSClusterFirst,
|
|
|
|
Containers: []api.Container{{Name: "abc", Image: "image", ImagePullPolicy: "IfNotPresent"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
oldRS := &extensions.ReplicaSet{
|
2017-01-22 03:36:02 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "abc", Namespace: metav1.NamespaceDefault, ResourceVersion: "10"},
|
2016-01-18 20:32:44 +00:00
|
|
|
Spec: extensions.ReplicaSetSpec{
|
|
|
|
Replicas: 3,
|
2016-12-03 18:57:26 +00:00
|
|
|
Selector: &metav1.LabelSelector{MatchLabels: validSelector},
|
2016-03-09 21:11:13 +00:00
|
|
|
Template: validPodTemplate.Template,
|
2016-01-18 20:32:44 +00:00
|
|
|
},
|
|
|
|
Status: extensions.ReplicaSetStatus{
|
|
|
|
Replicas: 1,
|
|
|
|
ObservedGeneration: int64(10),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
newRS := &extensions.ReplicaSet{
|
2017-01-22 03:36:02 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "abc", Namespace: metav1.NamespaceDefault, ResourceVersion: "9"},
|
2016-01-18 20:32:44 +00:00
|
|
|
Spec: extensions.ReplicaSetSpec{
|
|
|
|
Replicas: 1,
|
2016-12-03 18:57:26 +00:00
|
|
|
Selector: &metav1.LabelSelector{MatchLabels: validSelector},
|
2016-03-09 21:11:13 +00:00
|
|
|
Template: validPodTemplate.Template,
|
2016-01-18 20:32:44 +00:00
|
|
|
},
|
|
|
|
Status: extensions.ReplicaSetStatus{
|
|
|
|
Replicas: 3,
|
|
|
|
ObservedGeneration: int64(11),
|
|
|
|
},
|
|
|
|
}
|
2016-08-08 20:15:33 +00:00
|
|
|
StatusStrategy.PrepareForUpdate(ctx, newRS, oldRS)
|
2016-01-18 20:32:44 +00:00
|
|
|
if newRS.Status.Replicas != 3 {
|
|
|
|
t.Errorf("ReplicaSet status updates should allow change of replicas: %v", newRS.Status.Replicas)
|
|
|
|
}
|
|
|
|
if newRS.Spec.Replicas != 3 {
|
|
|
|
t.Errorf("PrepareForUpdate should have preferred spec")
|
|
|
|
}
|
|
|
|
errs := StatusStrategy.ValidateUpdate(ctx, newRS, oldRS)
|
|
|
|
if len(errs) != 0 {
|
|
|
|
t.Errorf("Unexpected error %v", errs)
|
|
|
|
}
|
|
|
|
}
|