Skip to content

Commit 263bc79

Browse files
author
Erusso7
committed
Implement unit-test for the first validation case
1 parent 3f6a6c7 commit 263bc79

File tree

3 files changed

+94
-139
lines changed

3 files changed

+94
-139
lines changed

api/v1beta1/module_webhook.go

+23-7
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ limitations under the License.
1717
package v1beta1
1818

1919
import (
20+
"errors"
2021
"k8s.io/apimachinery/pkg/runtime"
22+
"regexp"
2123
ctrl "sigs.k8s.io/controller-runtime"
2224
logf "sigs.k8s.io/controller-runtime/pkg/log"
2325
"sigs.k8s.io/controller-runtime/pkg/webhook"
@@ -32,9 +34,6 @@ func (r *Module) SetupWebhookWithManager(mgr ctrl.Manager) error {
3234
Complete()
3335
}
3436

35-
// TODO(user): EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
36-
37-
// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation.
3837
//+kubebuilder:webhook:path=/validate-kmm-sigs-x-k8s-io-v1beta1-module,mutating=false,failurePolicy=fail,sideEffects=None,groups=kmm.sigs.x-k8s.io,resources=modules,verbs=create;update,versions=v1beta1,name=vmodule.kb.io,admissionReviewVersions=v1
3938

4039
var _ webhook.Validator = &Module{}
@@ -43,22 +42,39 @@ var _ webhook.Validator = &Module{}
4342
func (r *Module) ValidateCreate() error {
4443
modulelog.Info("validate create", "name", r.Name)
4544

46-
// TODO(user): fill in your validation logic upon object creation.
45+
if err := r.validateKernelMappingRegex(); err != nil {
46+
return err
47+
}
48+
4749
return nil
4850
}
4951

5052
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
5153
func (r *Module) ValidateUpdate(old runtime.Object) error {
5254
modulelog.Info("validate update", "name", r.Name)
5355

54-
// TODO(user): fill in your validation logic upon object update.
56+
if err := r.validateKernelMappingRegex(); err != nil {
57+
return err
58+
}
59+
5560
return nil
5661
}
5762

5863
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
5964
func (r *Module) ValidateDelete() error {
60-
modulelog.Info("validate delete", "name", r.Name)
65+
return nil
66+
}
67+
68+
func (r *Module) validateKernelMappingRegex() error {
69+
if r.Spec.ModuleLoader.Container.KernelMappings == nil {
70+
return nil
71+
}
72+
73+
for _, km := range r.Spec.ModuleLoader.Container.KernelMappings {
74+
if _, rErr := regexp.Compile(km.Regexp); rErr != nil {
75+
return errors.New("invalid regex: " + rErr.Error())
76+
}
77+
}
6178

62-
// TODO(user): fill in your validation logic upon object deletion.
6379
return nil
6480
}

api/v1beta1/module_webhook_test.go

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
Copyright 2022.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1beta1
18+
19+
import (
20+
. "github.com/onsi/ginkgo/v2"
21+
. "github.com/onsi/gomega"
22+
"testing"
23+
)
24+
25+
func TestV1beta1(t *testing.T) {
26+
RegisterFailHandler(Fail)
27+
RunSpecs(t, "V1beta1 Suite")
28+
}
29+
30+
var module = &Module{
31+
Spec: ModuleSpec{
32+
ModuleLoader: ModuleLoaderSpec{
33+
Container: ModuleLoaderContainerSpec{
34+
KernelMappings: []KernelMapping{
35+
{Regexp: "valid-regex"},
36+
},
37+
},
38+
},
39+
},
40+
}
41+
42+
var _ = Describe("ValidateCreate", func() {
43+
It("should pass when all conditions are met", func() {
44+
e := module.ValidateCreate()
45+
Expect(e).To(BeNil())
46+
})
47+
48+
It("should fail when an invalid regex is found", func() {
49+
m := module
50+
m.Spec.ModuleLoader.Container.KernelMappings = []KernelMapping{
51+
{Regexp: "*invalid-regex"},
52+
}
53+
e := module.ValidateCreate()
54+
Expect(e).To(HaveOccurred())
55+
Expect(e.Error()).To(ContainSubstring("invalid regex:"))
56+
})
57+
})
58+
59+
var _ = Describe("ValidateUpdate", func() {
60+
It("should pass when all conditions are met", func() {
61+
e := module.ValidateUpdate(nil)
62+
Expect(e).To(BeNil())
63+
})
64+
})
65+
66+
var _ = Describe("ValidateDelete", func() {
67+
It("should do nothing and return always nil", func() {
68+
e := module.ValidateDelete()
69+
Expect(e).To(BeNil())
70+
})
71+
})

api/v1beta1/webhook_suite_test.go

-132
This file was deleted.

0 commit comments

Comments
 (0)