-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4452 from randomvariable/automated-cherry-pick-of…
…-#4448-release-0.3 🐛 Adds a test helper to all mutating webhooks to ensure defaulting passes validation (0.3 backport)
- Loading branch information
Showing
10 changed files
with
102 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
Copyright 2021 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 defaulting | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/onsi/gomega" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
) | ||
|
||
// DefaultingValidator interface is for objects that define both defaulting | ||
// and validating webhooks. | ||
type DefaultingValidator interface { | ||
runtime.Object | ||
Default() | ||
ValidateCreate() error | ||
ValidateUpdate(old runtime.Object) error | ||
ValidateDelete() error | ||
} | ||
|
||
// DefaultValidateTest returns a new testing function to be used in tests to | ||
// make sure defaulting webhooks also pass validation tests on create, | ||
// update and delete. | ||
func DefaultValidateTest(object DefaultingValidator) func(*testing.T) { | ||
return func(t *testing.T) { | ||
createCopy := object.DeepCopyObject().(DefaultingValidator) | ||
updateCopy := object.DeepCopyObject().(DefaultingValidator) | ||
deleteCopy := object.DeepCopyObject().(DefaultingValidator) | ||
defaultingUpdateCopy := updateCopy.DeepCopyObject().(DefaultingValidator) | ||
|
||
t.Run("validate-on-create", func(t *testing.T) { | ||
g := gomega.NewWithT(t) | ||
createCopy.Default() | ||
g.Expect(createCopy.ValidateCreate()).To(gomega.Succeed()) | ||
}) | ||
t.Run("validate-on-update", func(t *testing.T) { | ||
g := gomega.NewWithT(t) | ||
defaultingUpdateCopy.Default() | ||
g.Expect(defaultingUpdateCopy.ValidateUpdate(updateCopy)).To(gomega.Succeed()) | ||
}) | ||
t.Run("validate-on-delete", func(t *testing.T) { | ||
g := gomega.NewWithT(t) | ||
deleteCopy.Default() | ||
g.Expect(deleteCopy.ValidateDelete()).To(gomega.Succeed()) | ||
}) | ||
} | ||
} |