Skip to content

Commit aee0895

Browse files
committed
Add a test and let the defaulter return error
1 parent 6d5c67f commit aee0895

File tree

2 files changed

+47
-24
lines changed

2 files changed

+47
-24
lines changed

internal/webhook/v1alpha2/iut_webhook.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,13 @@ import (
3232
)
3333

3434
// nolint:unused
35-
// log is for logging in this package.
36-
var (
37-
iutlog = logf.Log.WithName("iut-resource")
38-
cli client.Client
39-
)
35+
// iutlog is for logging in this package.
36+
var iutlog = logf.Log.WithName("iut-resource")
4037

4138
// SetupIutWebhookWithManager registers the webhook for Iut in the manager.
4239
func SetupIutWebhookWithManager(mgr ctrl.Manager) error {
43-
if cli == nil {
44-
cli = mgr.GetClient()
45-
}
4640
return ctrl.NewWebhookManagedBy(mgr).For(&etosv1alpha2.Iut{}).
47-
WithDefaulter(&IutCustomDefaulter{}).
41+
WithDefaulter(&IutCustomDefaulter{mgr.GetClient()}).
4842
Complete()
4943
}
5044

@@ -56,6 +50,7 @@ func SetupIutWebhookWithManager(mgr ctrl.Manager) error {
5650
// NOTE: The +kubebuilder:object:generate=false marker prevents controller-gen from generating DeepCopy methods,
5751
// as it is used only for temporary operations and does not need to be deeply copied.
5852
type IutCustomDefaulter struct {
53+
client.Reader
5954
}
6055

6156
var _ webhook.CustomDefaulter = &IutCustomDefaulter{}
@@ -71,11 +66,15 @@ func (d *IutCustomDefaulter) Default(ctx context.Context, obj runtime.Object) er
7166

7267
environmentrequest := &v1alpha1.EnvironmentRequest{}
7368
namespacedName := types.NamespacedName{Name: iut.Spec.EnvironmentRequest, Namespace: iut.Namespace}
74-
if err := cli.Get(ctx, namespacedName, environmentrequest); err != nil {
69+
if err := d.Get(ctx, namespacedName, environmentrequest); err != nil {
7570
iutlog.Error(err, "name", iut.Name, "namespace", iut.Namespace, "environmentRequest", namespacedName.Name,
7671
"Failed to get environmentrequest in namespace")
72+
return err
7773
}
7874

75+
if iut.Labels == nil {
76+
iut.Labels = make(map[string]string)
77+
}
7978
iut.Labels["etos.eiffel-community.github.io/environment-request"] = environmentrequest.Spec.Name
8079
iut.Labels["etos.eiffel-community.github.io/environment-request-id"] = environmentrequest.Spec.ID
8180
iut.Labels["etos.eiffel-community.github.io/provider"] = iut.Spec.ProviderID

internal/webhook/v1alpha2/iut_webhook_test.go

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,43 +19,67 @@ package v1alpha2
1919
import (
2020
. "github.com/onsi/ginkgo/v2"
2121
. "github.com/onsi/gomega"
22+
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2223

24+
etosv1alpha1 "github.com/eiffel-community/etos/api/v1alpha1"
2325
etosv1alpha2 "github.com/eiffel-community/etos/api/v1alpha2"
24-
// TODO (user): Add any additional imports if needed
2526
)
2627

2728
var _ = Describe("Iut Webhook", func() {
2829
var (
2930
obj *etosv1alpha2.Iut
3031
oldObj *etosv1alpha2.Iut
3132
defaulter IutCustomDefaulter
33+
34+
requestID = "89224612-3851-45c9-95c0-72b719ae46ea"
35+
requestIdentifier = "fbb4096d-6529-4c39-bac3-08a7e45bf69a"
36+
requestName = "test-environment-request"
37+
requestLabelCluster = "cluster-sample"
38+
providerID = "iut-provider-sample"
3239
)
3340

3441
BeforeEach(func() {
42+
environmentRequest := etosv1alpha1.EnvironmentRequest{
43+
ObjectMeta: v1.ObjectMeta{
44+
Labels: map[string]string{
45+
"etos.eiffel-community.github.io/cluster": requestLabelCluster,
46+
},
47+
},
48+
Spec: etosv1alpha1.EnvironmentRequestSpec{
49+
Identifier: requestIdentifier,
50+
Name: requestName,
51+
ID: requestID,
52+
},
53+
}
3554
obj = &etosv1alpha2.Iut{}
3655
oldObj = &etosv1alpha2.Iut{}
37-
defaulter = IutCustomDefaulter{}
56+
defaulter = IutCustomDefaulter{FakeReader(&environmentRequest, nil)}
3857
Expect(defaulter).NotTo(BeNil(), "Expected defaulter to be initialized")
3958
Expect(oldObj).NotTo(BeNil(), "Expected oldObj to be initialized")
4059
Expect(obj).NotTo(BeNil(), "Expected obj to be initialized")
41-
// TODO (user): Add any setup logic common to all tests
4260
})
4361

4462
AfterEach(func() {
45-
// TODO (user): Add any teardown logic common to all tests
4663
})
4764

4865
Context("When creating Iut under Defaulting Webhook", func() {
49-
// TODO (user): Add logic for defaulting webhooks
50-
// Example:
51-
// It("Should apply defaults when a required field is empty", func() {
52-
// By("simulating a scenario where defaults should be applied")
53-
// obj.SomeFieldWithDefault = ""
54-
// By("calling the Default method to apply defaults")
55-
// defaulter.Default(ctx, obj)
56-
// By("checking that the default values are set")
57-
// Expect(obj.SomeFieldWithDefault).To(Equal("default_value"))
58-
// })
66+
It("Should apply defaults when a required field is empty", func() {
67+
By("simulating a scenario where defaults should be applied")
68+
obj.Labels = nil
69+
obj.Spec.ProviderID = providerID
70+
By("calling the Default method to apply defaults")
71+
defaulter.Default(ctx, obj)
72+
By("checking that the default values are set")
73+
Expect(obj.Labels).To(Equal(map[string]string{
74+
"etos.eiffel-community.github.io/environment-request": requestName,
75+
"etos.eiffel-community.github.io/environment-request-id": requestID,
76+
"etos.eiffel-community.github.io/cluster": requestLabelCluster,
77+
"etos.eiffel-community.github.io/provider": providerID,
78+
"etos.eiffel-community.github.io/id": requestIdentifier,
79+
"app.kubernetes.io/part-of": "etos",
80+
"app.kubernetes.io/name": "iut-provider",
81+
}))
82+
})
5983
})
6084

6185
})

0 commit comments

Comments
 (0)