Skip to content

Commit 1e1dd76

Browse files
committed
refactor: move and unexport FakeValidator for unit tests in admission webhook
Signed-off-by: STRRL <[email protected]>
1 parent 6fe2ffd commit 1e1dd76

File tree

3 files changed

+19
-39
lines changed

3 files changed

+19
-39
lines changed

pkg/webhook/admission/admissiontest/doc.go

Lines changed: 0 additions & 18 deletions
This file was deleted.

pkg/webhook/admission/admissiontest/util.go renamed to pkg/webhook/admission/fake_validator_test.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,53 +14,53 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package admissiontest
17+
package admission
1818

1919
import (
2020
"k8s.io/apimachinery/pkg/runtime"
2121
"k8s.io/apimachinery/pkg/runtime/schema"
2222
)
2323

24-
// FakeValidator provides fake validating webhook functionality for testing
24+
// fakeValidator provides fake validating webhook functionality for testing
2525
// It implements the admission.Validator interface and
2626
// rejects all requests with the same configured error
2727
// or passes if ErrorToReturn is nil.
2828
// And it would always return configured warning messages WarningsToReturn.
29-
type FakeValidator struct {
30-
// ErrorToReturn is the error for which the FakeValidator rejects all requests
29+
type fakeValidator struct {
30+
// ErrorToReturn is the error for which the fakeValidator rejects all requests
3131
ErrorToReturn error `json:"errorToReturn,omitempty"`
3232
// GVKToReturn is the GroupVersionKind that the webhook operates on
3333
GVKToReturn schema.GroupVersionKind
34-
// WarningsToReturn is the warnings for FakeValidator returns to all requests
34+
// WarningsToReturn is the warnings for fakeValidator returns to all requests
3535
WarningsToReturn []string
3636
}
3737

38-
func (v *FakeValidator) ValidateCreate() (warnings []string, err error) {
38+
func (v *fakeValidator) ValidateCreate() (warnings []string, err error) {
3939
return v.WarningsToReturn, v.ErrorToReturn
4040
}
4141

42-
func (v *FakeValidator) ValidateUpdate(old runtime.Object) (warnings []string, err error) {
42+
func (v *fakeValidator) ValidateUpdate(old runtime.Object) (warnings []string, err error) {
4343
return v.WarningsToReturn, v.ErrorToReturn
4444
}
4545

46-
func (v *FakeValidator) ValidateDelete() (warnings []string, err error) {
46+
func (v *fakeValidator) ValidateDelete() (warnings []string, err error) {
4747
return v.WarningsToReturn, v.ErrorToReturn
4848
}
4949

50-
func (v *FakeValidator) SetGroupVersionKind(gvk schema.GroupVersionKind) {
50+
func (v *fakeValidator) SetGroupVersionKind(gvk schema.GroupVersionKind) {
5151
v.GVKToReturn = gvk
5252
}
5353

54-
func (v *FakeValidator) GroupVersionKind() schema.GroupVersionKind {
54+
func (v *fakeValidator) GroupVersionKind() schema.GroupVersionKind {
5555
return v.GVKToReturn
5656
}
5757

58-
func (v *FakeValidator) GetObjectKind() schema.ObjectKind {
58+
func (v *fakeValidator) GetObjectKind() schema.ObjectKind {
5959
return v
6060
}
6161

62-
func (v *FakeValidator) DeepCopyObject() runtime.Object {
63-
return &FakeValidator{
62+
func (v *fakeValidator) DeepCopyObject() runtime.Object {
63+
return &fakeValidator{
6464
ErrorToReturn: v.ErrorToReturn,
6565
GVKToReturn: v.GVKToReturn,
6666
WarningsToReturn: v.WarningsToReturn,

pkg/webhook/admission/validator_test.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ import (
2323

2424
. "github.com/onsi/ginkgo/v2"
2525
. "github.com/onsi/gomega"
26-
"sigs.k8s.io/controller-runtime/pkg/webhook/admission/admissiontest"
27-
2826
admissionv1 "k8s.io/api/admission/v1"
2927
apierrors "k8s.io/apimachinery/pkg/api/errors"
3028
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -40,7 +38,7 @@ var _ = Describe("validatingHandler", func() {
4038
decoder := NewDecoder(scheme.Scheme)
4139

4240
Context("when dealing with successful results without warning", func() {
43-
f := &admissiontest.FakeValidator{ErrorToReturn: nil, GVKToReturn: fakeValidatorVK, WarningsToReturn: nil}
41+
f := &fakeValidator{ErrorToReturn: nil, GVKToReturn: fakeValidatorVK, WarningsToReturn: nil}
4442
handler := validatingHandler{validator: f, decoder: decoder}
4543

4644
It("should return 200 in response when create succeeds", func() {
@@ -97,7 +95,7 @@ var _ = Describe("validatingHandler", func() {
9795
const warningMessage = "warning message"
9896
const anotherWarningMessage = "another warning message"
9997
Context("when dealing with successful results with warning", func() {
100-
f := &admissiontest.FakeValidator{ErrorToReturn: nil, GVKToReturn: fakeValidatorVK, WarningsToReturn: []string{
98+
f := &fakeValidator{ErrorToReturn: nil, GVKToReturn: fakeValidatorVK, WarningsToReturn: []string{
10199
warningMessage,
102100
anotherWarningMessage,
103101
}}
@@ -167,7 +165,7 @@ var _ = Describe("validatingHandler", func() {
167165
Code: http.StatusUnprocessableEntity,
168166
},
169167
}
170-
f := &admissiontest.FakeValidator{ErrorToReturn: expectedError, GVKToReturn: fakeValidatorVK, WarningsToReturn: []string{warningMessage, anotherWarningMessage}}
168+
f := &fakeValidator{ErrorToReturn: expectedError, GVKToReturn: fakeValidatorVK, WarningsToReturn: []string{warningMessage, anotherWarningMessage}}
171169
handler := validatingHandler{validator: f, decoder: decoder}
172170

173171
It("should propagate the Status from ValidateCreate's return value to the HTTP response", func() {
@@ -241,7 +239,7 @@ var _ = Describe("validatingHandler", func() {
241239
Code: http.StatusUnprocessableEntity,
242240
},
243241
}
244-
f := &admissiontest.FakeValidator{ErrorToReturn: expectedError, GVKToReturn: fakeValidatorVK, WarningsToReturn: nil}
242+
f := &fakeValidator{ErrorToReturn: expectedError, GVKToReturn: fakeValidatorVK, WarningsToReturn: nil}
245243
handler := validatingHandler{validator: f, decoder: decoder}
246244

247245
It("should propagate the Status from ValidateCreate's return value to the HTTP response", func() {
@@ -307,7 +305,7 @@ var _ = Describe("validatingHandler", func() {
307305
Context("when dealing with non-status errors, without warning messages", func() {
308306

309307
expectedError := errors.New("some error")
310-
f := &admissiontest.FakeValidator{ErrorToReturn: expectedError, GVKToReturn: fakeValidatorVK}
308+
f := &fakeValidator{ErrorToReturn: expectedError, GVKToReturn: fakeValidatorVK}
311309
handler := validatingHandler{validator: f, decoder: decoder}
312310

313311
It("should return 403 response when ValidateCreate with error message embedded", func() {
@@ -370,7 +368,7 @@ var _ = Describe("validatingHandler", func() {
370368
Context("when dealing with non-status errors, with warning messages", func() {
371369

372370
expectedError := errors.New("some error")
373-
f := &admissiontest.FakeValidator{ErrorToReturn: expectedError, GVKToReturn: fakeValidatorVK, WarningsToReturn: []string{warningMessage, anotherWarningMessage}}
371+
f := &fakeValidator{ErrorToReturn: expectedError, GVKToReturn: fakeValidatorVK, WarningsToReturn: []string{warningMessage, anotherWarningMessage}}
374372
handler := validatingHandler{validator: f, decoder: decoder}
375373

376374
It("should return 403 response when ValidateCreate with error message embedded", func() {

0 commit comments

Comments
 (0)