From 9bb8dd9ca58b256021289c685a4da86779076b8a Mon Sep 17 00:00:00 2001 From: Tim Allclair Date: Wed, 1 Aug 2018 19:22:52 -0700 Subject: [PATCH] Fix coordination.Lease validation --- .../coordination/validation/validation.go | 6 +++-- .../validation/validation_test.go | 22 ++++++++++++++++++- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/pkg/apis/coordination/validation/validation.go b/pkg/apis/coordination/validation/validation.go index df267351c52..e3eedcf5a83 100644 --- a/pkg/apis/coordination/validation/validation.go +++ b/pkg/apis/coordination/validation/validation.go @@ -24,14 +24,16 @@ import ( // ValidateLease validates a Lease. func ValidateLease(lease *coordination.Lease) field.ErrorList { - allErrs := validation.ValidateObjectMeta(&lease.ObjectMeta, true, validation.NameIsDNSSubdomain, field.NewPath("objectMeta")) + allErrs := validation.ValidateObjectMeta(&lease.ObjectMeta, true, validation.NameIsDNSSubdomain, field.NewPath("metadata")) allErrs = append(allErrs, ValidateLeaseSpec(&lease.Spec, field.NewPath("spec"))...) return allErrs } // ValidateLeaseUpdate validates an update of Lease object. func ValidateLeaseUpdate(lease, oldLease *coordination.Lease) field.ErrorList { - return ValidateLease(lease) + allErrs := validation.ValidateObjectMetaUpdate(&lease.ObjectMeta, &oldLease.ObjectMeta, field.NewPath("metadata")) + allErrs = append(allErrs, ValidateLeaseSpec(&lease.Spec, field.NewPath("spec"))...) + return allErrs } // ValidateLeaseSpec validates spec of Lease. diff --git a/pkg/apis/coordination/validation/validation_test.go b/pkg/apis/coordination/validation/validation_test.go index f005fab291c..2d102c6a154 100644 --- a/pkg/apis/coordination/validation/validation_test.go +++ b/pkg/apis/coordination/validation/validation_test.go @@ -71,6 +71,10 @@ func TestValidateLeaseSpecUpdate(t *testing.T) { oldLeaseDuration := int32(3) oldLeaseTransitions := int32(3) oldLease := &coordination.Lease{ + ObjectMeta: metav1.ObjectMeta{ + Name: "holder", + Namespace: "holder-namespace", + }, Spec: coordination.LeaseSpec{ HolderIdentity: &oldHolder, LeaseDurationSeconds: &oldLeaseDuration, @@ -78,7 +82,23 @@ func TestValidateLeaseSpecUpdate(t *testing.T) { }, } errs := ValidateLeaseUpdate(lease, oldLease) - if len(errs) != 2 { + 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()) + } }