Skip to content

Commit

Permalink
update defaultor test
Browse files Browse the repository at this point in the history
Signed-off-by: AnyISalIn <anyisalin@gmail.com>
  • Loading branch information
AnyISalIn committed May 16, 2022
1 parent 61e3738 commit 23d7686
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions pkg/webhook/admission/defaulter_test.go
@@ -0,0 +1,68 @@
package admission

import (
"context"
"net/http"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

admissionv1 "k8s.io/api/admission/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
)

var _ = Describe("Defaulter Handler", func() {

It("should return ok if received delete verb in defaulter handler", func() {
obj := &TestDefaulter{}
handler := DefaultingWebhookFor(obj)

resp := handler.Handle(context.TODO(), Request{
AdmissionRequest: admissionv1.AdmissionRequest{
Operation: admissionv1.Delete,
OldObject: runtime.RawExtension{
Raw: []byte("{}"),
},
},
})
Expect(resp.Allowed).Should(BeTrue())
Expect(resp.Result.Code).Should(Equal(int32(http.StatusOK)))
})

})

// TestDefaulter.
var _ runtime.Object = &TestDefaulter{}

type TestDefaulter struct {
Replica int `json:"replica,omitempty"`
}

var testDefaulterGVK = schema.GroupVersionKind{Group: "foo.test.org", Version: "v1", Kind: "TestDefaulter"}

func (d *TestDefaulter) GetObjectKind() schema.ObjectKind { return d }
func (d *TestDefaulter) DeepCopyObject() runtime.Object {
return &TestDefaulter{
Replica: d.Replica,
}
}

func (d *TestDefaulter) GroupVersionKind() schema.GroupVersionKind {
return testDefaulterGVK
}

func (d *TestDefaulter) SetGroupVersionKind(gvk schema.GroupVersionKind) {}

var _ runtime.Object = &TestDefaulterList{}

type TestDefaulterList struct{}

func (*TestDefaulterList) GetObjectKind() schema.ObjectKind { return nil }
func (*TestDefaulterList) DeepCopyObject() runtime.Object { return nil }

func (d *TestDefaulter) Default() {
if d.Replica < 2 {
d.Replica = 2
}
}

0 comments on commit 23d7686

Please sign in to comment.