k3s/pkg/registry/apps/petset/strategy_test.go

145 lines
4.5 KiB
Go
Raw Normal View History

2016-04-15 22:30:15 +00:00
/*
Copyright 2016 The Kubernetes Authors.
2016-04-15 22:30:15 +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 petset
import (
"testing"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/unversioned"
"k8s.io/kubernetes/pkg/apis/apps"
)
2016-10-26 20:44:07 +00:00
func TestStatefulSetStrategy(t *testing.T) {
2016-04-15 22:30:15 +00:00
ctx := api.NewDefaultContext()
if !Strategy.NamespaceScoped() {
2016-10-26 20:44:07 +00:00
t.Errorf("StatefulSet must be namespace scoped")
2016-04-15 22:30:15 +00:00
}
if Strategy.AllowCreateOnUpdate() {
2016-10-26 20:44:07 +00:00
t.Errorf("StatefulSet should not allow create on update")
2016-04-15 22:30:15 +00:00
}
validSelector := map[string]string{"a": "b"}
validPodTemplate := api.PodTemplate{
Template: api.PodTemplateSpec{
ObjectMeta: api.ObjectMeta{
Labels: validSelector,
},
Spec: api.PodSpec{
RestartPolicy: api.RestartPolicyAlways,
DNSPolicy: api.DNSClusterFirst,
Containers: []api.Container{{Name: "abc", Image: "image", ImagePullPolicy: "IfNotPresent"}},
},
},
}
2016-10-26 20:44:07 +00:00
ps := &apps.StatefulSet{
2016-04-15 22:30:15 +00:00
ObjectMeta: api.ObjectMeta{Name: "abc", Namespace: api.NamespaceDefault},
2016-10-26 20:44:07 +00:00
Spec: apps.StatefulSetSpec{
2016-04-15 22:30:15 +00:00
Selector: &unversioned.LabelSelector{MatchLabels: validSelector},
Template: validPodTemplate.Template,
},
2016-10-26 20:44:07 +00:00
Status: apps.StatefulSetStatus{Replicas: 3},
2016-04-15 22:30:15 +00:00
}
2016-08-08 20:15:33 +00:00
Strategy.PrepareForCreate(ctx, ps)
2016-04-15 22:30:15 +00:00
if ps.Status.Replicas != 0 {
2016-10-26 20:44:07 +00:00
t.Error("StatefulSet should not allow setting status.replicas on create")
2016-04-15 22:30:15 +00:00
}
errs := Strategy.Validate(ctx, ps)
if len(errs) != 0 {
t.Errorf("Unexpected error validating %v", errs)
}
2016-04-25 19:24:40 +00:00
// Just Spec.Replicas is allowed to change
2016-10-26 20:44:07 +00:00
validPs := &apps.StatefulSet{
ObjectMeta: api.ObjectMeta{Name: ps.Name, Namespace: ps.Namespace, ResourceVersion: "1", Generation: 1},
2016-10-26 20:44:07 +00:00
Spec: apps.StatefulSetSpec{
2016-04-15 22:30:15 +00:00
Selector: ps.Spec.Selector,
Template: validPodTemplate.Template,
},
2016-10-26 20:44:07 +00:00
Status: apps.StatefulSetStatus{Replicas: 4},
2016-04-15 22:30:15 +00:00
}
2016-08-08 20:15:33 +00:00
Strategy.PrepareForUpdate(ctx, validPs, ps)
2016-04-15 22:30:15 +00:00
errs = Strategy.ValidateUpdate(ctx, validPs, ps)
2016-04-25 19:24:40 +00:00
if len(errs) != 0 {
2016-10-26 20:44:07 +00:00
t.Errorf("Updating spec.Replicas is allowed on a statefulset: %v", errs)
2016-04-25 19:24:40 +00:00
}
validPs.Spec.Selector = &unversioned.LabelSelector{MatchLabels: map[string]string{"a": "bar"}}
2016-08-08 20:15:33 +00:00
Strategy.PrepareForUpdate(ctx, validPs, ps)
2016-04-25 19:24:40 +00:00
errs = Strategy.ValidateUpdate(ctx, validPs, ps)
2016-04-15 22:30:15 +00:00
if len(errs) == 0 {
2016-10-26 20:44:07 +00:00
t.Errorf("Expected a validation error since updates are disallowed on statefulsets.")
2016-04-15 22:30:15 +00:00
}
}
2016-10-26 20:44:07 +00:00
func TestStatefulSetStatusStrategy(t *testing.T) {
2016-04-15 22:30:15 +00:00
ctx := api.NewDefaultContext()
if !StatusStrategy.NamespaceScoped() {
2016-10-26 20:44:07 +00:00
t.Errorf("StatefulSet must be namespace scoped")
2016-04-15 22:30:15 +00:00
}
if StatusStrategy.AllowCreateOnUpdate() {
2016-10-26 20:44:07 +00:00
t.Errorf("StatefulSet should not allow create on update")
2016-04-15 22:30:15 +00:00
}
validSelector := map[string]string{"a": "b"}
validPodTemplate := api.PodTemplate{
Template: api.PodTemplateSpec{
ObjectMeta: api.ObjectMeta{
Labels: validSelector,
},
Spec: api.PodSpec{
RestartPolicy: api.RestartPolicyAlways,
DNSPolicy: api.DNSClusterFirst,
Containers: []api.Container{{Name: "abc", Image: "image", ImagePullPolicy: "IfNotPresent"}},
},
},
}
2016-10-26 20:44:07 +00:00
oldPS := &apps.StatefulSet{
2016-04-15 22:30:15 +00:00
ObjectMeta: api.ObjectMeta{Name: "abc", Namespace: api.NamespaceDefault, ResourceVersion: "10"},
2016-10-26 20:44:07 +00:00
Spec: apps.StatefulSetSpec{
2016-04-15 22:30:15 +00:00
Replicas: 3,
Selector: &unversioned.LabelSelector{MatchLabels: validSelector},
Template: validPodTemplate.Template,
},
2016-10-26 20:44:07 +00:00
Status: apps.StatefulSetStatus{
2016-04-15 22:30:15 +00:00
Replicas: 1,
},
}
2016-10-26 20:44:07 +00:00
newPS := &apps.StatefulSet{
2016-04-15 22:30:15 +00:00
ObjectMeta: api.ObjectMeta{Name: "abc", Namespace: api.NamespaceDefault, ResourceVersion: "9"},
2016-10-26 20:44:07 +00:00
Spec: apps.StatefulSetSpec{
2016-04-15 22:30:15 +00:00
Replicas: 1,
Selector: &unversioned.LabelSelector{MatchLabels: validSelector},
Template: validPodTemplate.Template,
},
2016-10-26 20:44:07 +00:00
Status: apps.StatefulSetStatus{
2016-04-15 22:30:15 +00:00
Replicas: 2,
},
}
2016-08-08 20:15:33 +00:00
StatusStrategy.PrepareForUpdate(ctx, newPS, oldPS)
2016-04-15 22:30:15 +00:00
if newPS.Status.Replicas != 2 {
2016-10-26 20:44:07 +00:00
t.Errorf("StatefulSet status updates should allow change of pods: %v", newPS.Status.Replicas)
2016-04-15 22:30:15 +00:00
}
if newPS.Spec.Replicas != 3 {
2016-10-26 20:44:07 +00:00
t.Errorf("StatefulSet status updates should not clobber spec: %v", newPS.Spec)
2016-04-15 22:30:15 +00:00
}
errs := StatusStrategy.ValidateUpdate(ctx, newPS, oldPS)
if len(errs) != 0 {
t.Errorf("Unexpected error %v", errs)
}
}