blob: 2d102c6a1542f6de4b7554297dbbbc320a25fd99 [file] [log] [blame]
/*
Copyright 2018 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 validation
import (
"testing"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/kubernetes/pkg/apis/coordination"
)
func TestValidateLease(t *testing.T) {
lease := &coordination.Lease{
ObjectMeta: metav1.ObjectMeta{
Name: "invalidName++",
Namespace: "==invalid_Namespace==",
},
}
errs := ValidateLease(lease)
if len(errs) != 2 {
t.Errorf("unexpected list of errors: %#v", errs.ToAggregate().Error())
}
}
func TestValidateLeaseSpec(t *testing.T) {
holder := "holder"
leaseDuration := int32(0)
leaseTransitions := int32(-1)
spec := &coordination.LeaseSpec{
HolderIdentity: &holder,
LeaseDurationSeconds: &leaseDuration,
LeaseTransitions: &leaseTransitions,
}
errs := ValidateLeaseSpec(spec, field.NewPath("foo"))
if len(errs) != 2 {
t.Errorf("unexpected list of errors: %#v", errs.ToAggregate().Error())
}
}
func TestValidateLeaseSpecUpdate(t *testing.T) {
holder := "holder"
leaseDuration := int32(0)
leaseTransitions := int32(-1)
lease := &coordination.Lease{
ObjectMeta: metav1.ObjectMeta{
Name: "holder",
Namespace: "holder-namespace",
},
Spec: coordination.LeaseSpec{
HolderIdentity: &holder,
LeaseDurationSeconds: &leaseDuration,
LeaseTransitions: &leaseTransitions,
},
}
oldHolder := "oldHolder"
oldLeaseDuration := int32(3)
oldLeaseTransitions := int32(3)
oldLease := &coordination.Lease{
ObjectMeta: metav1.ObjectMeta{
Name: "holder",
Namespace: "holder-namespace",
},
Spec: coordination.LeaseSpec{
HolderIdentity: &oldHolder,
LeaseDurationSeconds: &oldLeaseDuration,
LeaseTransitions: &oldLeaseTransitions,
},
}
errs := ValidateLeaseUpdate(lease, oldLease)
if len(errs) != 3 {
t.Errorf("unexpected list of errors: %#v", errs.ToAggregate().Error())
}
validLeaseDuration := int32(10)
validLeaseTransitions := int32(20)
validLease := &coordination.Lease{
ObjectMeta: oldLease.ObjectMeta,
Spec: coordination.LeaseSpec{
HolderIdentity: &holder,
LeaseDurationSeconds: &validLeaseDuration,
LeaseTransitions: &validLeaseTransitions,
},
}
validLease.ObjectMeta.ResourceVersion = "2"
errs = ValidateLeaseUpdate(validLease, oldLease)
if len(errs) != 0 {
t.Errorf("unexpected list of errors for valid update: %#v", errs.ToAggregate().Error())
}
}