mirror of
https://github.com/optim-enterprises-bv/kubernetes.git
synced 2025-11-01 18:58:18 +00:00
Implements ControllerRevision API Object without codec and code
generation
This commit is contained in:
@@ -21,6 +21,7 @@ import (
|
||||
"testing"
|
||||
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/apis/apps"
|
||||
)
|
||||
@@ -432,3 +433,159 @@ func TestValidateStatefulSetUpdate(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateControllerRevision(t *testing.T) {
|
||||
newControllerRevision := func(name, namespace string, data runtime.Object, revision int64) apps.ControllerRevision {
|
||||
return apps.ControllerRevision{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: name,
|
||||
Namespace: namespace,
|
||||
},
|
||||
Data: data,
|
||||
Revision: revision,
|
||||
}
|
||||
}
|
||||
|
||||
ss := apps.StatefulSet{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "abc", Namespace: metav1.NamespaceDefault},
|
||||
Spec: apps.StatefulSetSpec{
|
||||
Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"foo": "bar"}},
|
||||
Template: api.PodTemplateSpec{
|
||||
Spec: api.PodSpec{
|
||||
RestartPolicy: api.RestartPolicyAlways,
|
||||
DNSPolicy: api.DNSClusterFirst,
|
||||
},
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Labels: map[string]string{"foo": "bar"},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
var (
|
||||
valid = newControllerRevision("validname", "validns", &ss, 0)
|
||||
badRevision = newControllerRevision("validname", "validns", &ss, -1)
|
||||
emptyName = newControllerRevision("", "validns", &ss, 0)
|
||||
invalidName = newControllerRevision("NoUppercaseOrSpecialCharsLike=Equals", "validns", &ss, 0)
|
||||
emptyNs = newControllerRevision("validname", "", &ss, 100)
|
||||
invalidNs = newControllerRevision("validname", "NoUppercaseOrSpecialCharsLike=Equals", &ss, 100)
|
||||
nilData = newControllerRevision("validname", "NoUppercaseOrSpecialCharsLike=Equals", nil, 100)
|
||||
)
|
||||
|
||||
tests := map[string]struct {
|
||||
history apps.ControllerRevision
|
||||
isValid bool
|
||||
}{
|
||||
"valid": {valid, true},
|
||||
"negative revision": {badRevision, false},
|
||||
"empty name": {emptyName, false},
|
||||
"invalid name": {invalidName, false},
|
||||
"empty namespace": {emptyNs, false},
|
||||
"invalid namespace": {invalidNs, false},
|
||||
"nil data": {nilData, false},
|
||||
}
|
||||
|
||||
for name, tc := range tests {
|
||||
errs := ValidateControllerRevision(&tc.history)
|
||||
if tc.isValid && len(errs) > 0 {
|
||||
t.Errorf("%v: unexpected error: %v", name, errs)
|
||||
}
|
||||
if !tc.isValid && len(errs) == 0 {
|
||||
t.Errorf("%v: unexpected non-error", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateControllerRevisionUpdate(t *testing.T) {
|
||||
newControllerRevision := func(version, name, namespace string, data runtime.Object, revision int64) apps.ControllerRevision {
|
||||
return apps.ControllerRevision{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: name,
|
||||
Namespace: namespace,
|
||||
ResourceVersion: version,
|
||||
},
|
||||
Data: data,
|
||||
Revision: revision,
|
||||
}
|
||||
}
|
||||
|
||||
ss := apps.StatefulSet{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "abc", Namespace: metav1.NamespaceDefault},
|
||||
Spec: apps.StatefulSetSpec{
|
||||
Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"foo": "bar"}},
|
||||
Template: api.PodTemplateSpec{
|
||||
Spec: api.PodSpec{
|
||||
RestartPolicy: api.RestartPolicyAlways,
|
||||
DNSPolicy: api.DNSClusterFirst,
|
||||
},
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Labels: map[string]string{"foo": "bar"},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
modifiedss := apps.StatefulSet{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "cdf", Namespace: metav1.NamespaceDefault},
|
||||
Spec: apps.StatefulSetSpec{
|
||||
Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"foo": "bar"}},
|
||||
Template: api.PodTemplateSpec{
|
||||
Spec: api.PodSpec{
|
||||
RestartPolicy: api.RestartPolicyAlways,
|
||||
DNSPolicy: api.DNSClusterFirst,
|
||||
},
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Labels: map[string]string{"foo": "bar"},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
var (
|
||||
valid = newControllerRevision("1", "validname", "validns", &ss, 0)
|
||||
noVersion = newControllerRevision("", "validname", "validns", &ss, 0)
|
||||
changedData = newControllerRevision("1", "validname", "validns", &modifiedss, 0)
|
||||
changedRevision = newControllerRevision("1", "validname", "validns", &ss, 1)
|
||||
)
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
newHistory apps.ControllerRevision
|
||||
oldHistory apps.ControllerRevision
|
||||
isValid bool
|
||||
}{
|
||||
{
|
||||
name: "valid",
|
||||
newHistory: valid,
|
||||
oldHistory: valid,
|
||||
isValid: true,
|
||||
},
|
||||
{
|
||||
name: "invalid",
|
||||
newHistory: noVersion,
|
||||
oldHistory: valid,
|
||||
isValid: false,
|
||||
},
|
||||
{
|
||||
name: "changed data",
|
||||
newHistory: changedData,
|
||||
oldHistory: valid,
|
||||
isValid: false,
|
||||
},
|
||||
{
|
||||
name: "changed revision",
|
||||
newHistory: changedRevision,
|
||||
oldHistory: valid,
|
||||
isValid: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
errs := ValidateControllerRevisionUpdate(&tc.newHistory, &tc.oldHistory)
|
||||
if tc.isValid && len(errs) > 0 {
|
||||
t.Errorf("%v: unexpected error: %v", tc.name, errs)
|
||||
}
|
||||
if !tc.isValid && len(errs) == 0 {
|
||||
t.Errorf("%v: unexpected non-error", tc.name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user