Skip to content

Commit 6043d3a

Browse files
committed
add test helper funcs
1 parent afc43ab commit 6043d3a

4 files changed

Lines changed: 50 additions & 30 deletions

File tree

internal/webhook/breaktheglass/breakrequest_webhook_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,15 @@ var _ = Describe("BreakRequest Webhook", func() {
5454
reader.EXPECT().
5555
Get(gm.Any(), client.ObjectKey{Name: br.Spec.TemplateName}, gm.Any(), gm.Any()).
5656
Return(errors.New("not found"))
57-
response := validator.OnCreate(nil, reader, decoder, nil)(ctx, admission.Request{})
57+
response := test.RunWebhook(ctx, validator.OnCreate, reader, decoder)
5858
test.VerifyResponse(response, http.StatusInternalServerError, "error loading template foo: not found")
5959
})
6060
It("Should allow creation if the referenced template is available", func() {
6161
By("simulating an invalid creation scenario")
6262
br.Spec.TemplateName = "bar"
6363
reader.EXPECT().
6464
Get(gm.Any(), client.ObjectKey{Name: br.Spec.TemplateName}, gm.Any(), gm.Any())
65-
response := validator.OnCreate(nil, reader, decoder, nil)(ctx, admission.Request{})
65+
response := test.RunWebhook(ctx, validator.OnCreate, reader, decoder)
6666
Expect(response).To(BeNil())
6767
})
6868
It("Should deny creation if duration exceeds the template max duration", func() {
@@ -73,7 +73,7 @@ var _ = Describe("BreakRequest Webhook", func() {
7373
brt.Spec.MaxDuration.Duration = time.Minute
7474
})
7575

76-
response := validator.OnCreate(nil, reader, decoder, nil)(ctx, admission.Request{})
76+
response := test.RunWebhook(ctx, validator.OnCreate, reader, decoder)
7777
test.VerifyResponse(response, http.StatusForbidden, "requested duration 1h0m0s exceeds template maxDuration 1m0s")
7878
})
7979
})
@@ -91,14 +91,14 @@ var _ = Describe("BreakRequest Webhook", func() {
9191
It("Should be valid if the template name is not changed", func() {
9292
oldBr.Spec.TemplateName = defaultTemplateName
9393
br.Spec.TemplateName = defaultTemplateName
94-
response := validator.OnUpdate(nil, reader, decoder, nil)(ctx, admission.Request{})
94+
response := test.RunWebhook(ctx, validator.OnUpdate, reader, decoder)
9595
Expect(response).To(BeNil())
9696
})
9797
It("Should not be allowed to change the templateName", func() {
9898
oldBr.Spec.TemplateName = defaultTemplateName
9999
br.Spec.TemplateName = alternateTemplateName
100100

101-
response := validator.OnUpdate(nil, reader, decoder, nil)(ctx, admission.Request{})
101+
response := test.RunWebhook(ctx, validator.OnUpdate, reader, decoder)
102102
test.VerifyResponse(response, http.StatusForbidden, "templateName cannot be changed. old: foo, new: bar")
103103
})
104104
})

internal/webhook/breaktheglass/breakrequesttemplate_webhook_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@ var _ = Describe("BreakRequestTemplate Webhook", func() {
5656
It("Should deny creation", func() {
5757
By("simulating an invalid creation scenario")
5858

59-
response := validator.OnCreate(nil, reader, decoder, nil)(ctx, admission.Request{})
59+
response := test.RunWebhook(ctx, validator.OnCreate, reader, decoder)
6060
test.VerifyResponse(response, http.StatusForbidden, "approvalCondition should not be set when autoApprove is false")
6161
})
6262
It("Should deny update", func() {
6363
By("simulating an invalid update scenario")
6464

65-
response := validator.OnUpdate(nil, reader, decoder, nil)(ctx, admission.Request{})
65+
response := test.RunWebhook(ctx, validator.OnUpdate, reader, decoder)
6666
test.VerifyResponse(response, http.StatusForbidden, "approvalCondition should not be set when autoApprove is false")
6767
})
6868
})
@@ -73,12 +73,12 @@ var _ = Describe("BreakRequestTemplate Webhook", func() {
7373
})
7474
It("Should allow creation", func() {
7575
By("simulating an valid creation scenario")
76-
response := validator.OnCreate(nil, reader, decoder, nil)(ctx, admission.Request{})
76+
response := test.RunWebhook(ctx, validator.OnCreate, reader, decoder)
7777
Expect(response).To(BeNil())
7878
})
7979
It("Should allow update", func() {
8080
By("simulating an valid update scenario")
81-
response := validator.OnUpdate(nil, reader, decoder, nil)(ctx, admission.Request{})
81+
response := test.RunWebhook(ctx, validator.OnUpdate, reader, decoder)
8282
Expect(response).To(BeNil())
8383
})
8484
})
@@ -90,12 +90,12 @@ var _ = Describe("BreakRequestTemplate Webhook", func() {
9090
})
9191
It("Should deny creation", func() {
9292
By("simulating an invalid creation scenario")
93-
response := validator.OnCreate(nil, reader, decoder, nil)(ctx, admission.Request{})
93+
response := test.RunWebhook(ctx, validator.OnCreate, reader, decoder)
9494
test.VerifyResponse(response, http.StatusForbidden, "approvalCondition is invalid: ERROR: <input>:1:1: undeclared reference to 'foo' (in container '')\n | foo.spec.reason == 'test'\n | ^")
9595
})
9696
It("Should deny update", func() {
9797
By("simulating an invalid update scenario")
98-
response := validator.OnUpdate(nil, reader, decoder, nil)(ctx, admission.Request{})
98+
response := test.RunWebhook(ctx, validator.OnUpdate, reader, decoder)
9999
test.VerifyResponse(response, http.StatusForbidden, "approvalCondition is invalid: ERROR: <input>:1:1: undeclared reference to 'foo' (in container '')\n | foo.spec.reason == 'test'\n | ^")
100100
})
101101
})
@@ -107,12 +107,12 @@ var _ = Describe("BreakRequestTemplate Webhook", func() {
107107
})
108108
It("Should allow creation", func() {
109109
By("simulating an valid creation scenario")
110-
response := validator.OnCreate(nil, reader, decoder, nil)(ctx, admission.Request{})
110+
response := test.RunWebhook(ctx, validator.OnCreate, reader, decoder)
111111
Expect(response).To(BeNil())
112112
})
113113
It("Should allow update", func() {
114114
By("simulating an valid update scenario")
115-
response := validator.OnUpdate(nil, reader, decoder, nil)(ctx, admission.Request{})
115+
response := test.RunWebhook(ctx, validator.OnUpdate, reader, decoder)
116116
Expect(response).To(BeNil())
117117
})
118118
})

internal/webhook/test/response.go

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

internal/webhook/test/webhook.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2020-2026 Project Capsule Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package test
5+
6+
import (
7+
"context"
8+
9+
"github.com/onsi/ginkgo/v2"
10+
"github.com/onsi/gomega"
11+
"k8s.io/client-go/tools/events"
12+
"sigs.k8s.io/controller-runtime/pkg/client"
13+
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
14+
15+
"github.com/projectcapsule/capsule/pkg/runtime/handlers"
16+
)
17+
18+
// RunWebhook is a helper function to run a webhook function.
19+
// the admission request is always empty. request objects are provided by the provided decoder.
20+
func RunWebhook(
21+
ctx context.Context,
22+
validatorFunc func(client.Client, client.Reader, admission.Decoder, events.EventRecorder) handlers.Func,
23+
reader client.Reader,
24+
decoder admission.Decoder,
25+
) *admission.Response {
26+
ginkgo.GinkgoT().Helper()
27+
28+
return validatorFunc(nil, reader, decoder, nil)(ctx, admission.Request{})
29+
}
30+
31+
// VerifyResponse is a helper function to verify the response of a webhook function.
32+
func VerifyResponse(response *admission.Response, status int32, message string) {
33+
ginkgo.GinkgoT().Helper()
34+
gomega.Expect(response.Result).NotTo(gomega.BeNil())
35+
gomega.Expect(response.Result.Code).To(gomega.Equal(status))
36+
gomega.Expect(response.Result.Message).To(gomega.Equal(message))
37+
}

0 commit comments

Comments
 (0)