mirror of https://github.com/k3s-io/k3s
143 lines
4.3 KiB
Go
143 lines
4.3 KiB
Go
/*
|
|
Copyright 2016 The Kubernetes Authors.
|
|
|
|
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"
|
|
|
|
"k8s.io/kubernetes/pkg/api"
|
|
"k8s.io/kubernetes/pkg/api/unversioned"
|
|
"k8s.io/kubernetes/pkg/apis/extensions"
|
|
)
|
|
|
|
func TestReplicaSetStrategy(t *testing.T) {
|
|
ctx := api.NewDefaultContext()
|
|
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{
|
|
ObjectMeta: api.ObjectMeta{
|
|
Labels: validSelector,
|
|
},
|
|
Spec: api.PodSpec{
|
|
RestartPolicy: api.RestartPolicyAlways,
|
|
DNSPolicy: api.DNSClusterFirst,
|
|
Containers: []api.Container{{Name: "abc", Image: "image", ImagePullPolicy: "IfNotPresent"}},
|
|
},
|
|
},
|
|
}
|
|
rs := &extensions.ReplicaSet{
|
|
ObjectMeta: api.ObjectMeta{Name: "abc", Namespace: api.NamespaceDefault},
|
|
Spec: extensions.ReplicaSetSpec{
|
|
Selector: &unversioned.LabelSelector{MatchLabels: validSelector},
|
|
Template: validPodTemplate.Template,
|
|
},
|
|
Status: extensions.ReplicaSetStatus{
|
|
Replicas: 1,
|
|
ObservedGeneration: int64(10),
|
|
},
|
|
}
|
|
|
|
Strategy.PrepareForCreate(ctx, rs)
|
|
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{
|
|
ObjectMeta: api.ObjectMeta{Name: "bar", ResourceVersion: "4"},
|
|
}
|
|
Strategy.PrepareForUpdate(ctx, invalidRc, rs)
|
|
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) {
|
|
ctx := api.NewDefaultContext()
|
|
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{
|
|
ObjectMeta: api.ObjectMeta{
|
|
Labels: validSelector,
|
|
},
|
|
Spec: api.PodSpec{
|
|
RestartPolicy: api.RestartPolicyAlways,
|
|
DNSPolicy: api.DNSClusterFirst,
|
|
Containers: []api.Container{{Name: "abc", Image: "image", ImagePullPolicy: "IfNotPresent"}},
|
|
},
|
|
},
|
|
}
|
|
oldRS := &extensions.ReplicaSet{
|
|
ObjectMeta: api.ObjectMeta{Name: "abc", Namespace: api.NamespaceDefault, ResourceVersion: "10"},
|
|
Spec: extensions.ReplicaSetSpec{
|
|
Replicas: 3,
|
|
Selector: &unversioned.LabelSelector{MatchLabels: validSelector},
|
|
Template: validPodTemplate.Template,
|
|
},
|
|
Status: extensions.ReplicaSetStatus{
|
|
Replicas: 1,
|
|
ObservedGeneration: int64(10),
|
|
},
|
|
}
|
|
newRS := &extensions.ReplicaSet{
|
|
ObjectMeta: api.ObjectMeta{Name: "abc", Namespace: api.NamespaceDefault, ResourceVersion: "9"},
|
|
Spec: extensions.ReplicaSetSpec{
|
|
Replicas: 1,
|
|
Selector: &unversioned.LabelSelector{MatchLabels: validSelector},
|
|
Template: validPodTemplate.Template,
|
|
},
|
|
Status: extensions.ReplicaSetStatus{
|
|
Replicas: 3,
|
|
ObservedGeneration: int64(11),
|
|
},
|
|
}
|
|
StatusStrategy.PrepareForUpdate(ctx, newRS, oldRS)
|
|
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)
|
|
}
|
|
}
|