Skip to content

Commit b086628

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

File tree

3 files changed

+102
-139
lines changed

3 files changed

+102
-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

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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 validKMs = []KernelMapping{
31+
{Regexp: "valid-regex"},
32+
}
33+
34+
var invalidKMs = []KernelMapping{
35+
{Regexp: "*-invalid-regex"},
36+
}
37+
38+
var module = &Module{
39+
Spec: ModuleSpec{
40+
ModuleLoader: ModuleLoaderSpec{
41+
Container: ModuleLoaderContainerSpec{
42+
KernelMappings: nil,
43+
},
44+
},
45+
},
46+
}
47+
48+
var _ = BeforeEach(func() {
49+
module.Spec.ModuleLoader.Container.KernelMappings = validKMs
50+
})
51+
52+
var _ = Describe("ValidateCreate", func() {
53+
It("should pass when all conditions are met", func() {
54+
e := module.ValidateCreate()
55+
Expect(e).To(BeNil())
56+
})
57+
58+
It("should fail when an invalid regex is found", func() {
59+
m := module
60+
m.Spec.ModuleLoader.Container.KernelMappings = invalidKMs
61+
e := module.ValidateCreate()
62+
Expect(e).To(HaveOccurred())
63+
Expect(e.Error()).To(ContainSubstring("invalid regex:"))
64+
})
65+
})
66+
67+
var _ = Describe("ValidateUpdate", func() {
68+
It("should pass when all conditions are met", func() {
69+
e := module.ValidateUpdate(nil)
70+
Expect(e).To(BeNil())
71+
})
72+
})
73+
74+
var _ = Describe("ValidateDelete", func() {
75+
It("should do nothing and return always nil", func() {
76+
e := module.ValidateDelete()
77+
Expect(e).To(BeNil())
78+
})
79+
})

api/v1beta1/webhook_suite_test.go

-132
This file was deleted.

0 commit comments

Comments
 (0)