Skip to content

Commit 352446e

Browse files
Added unit tests for resourceinterpreter webhook configmanager
Signed-off-by: Anuj Agrawal <[email protected]>
1 parent a53c8bc commit 352446e

File tree

2 files changed

+484
-0
lines changed

2 files changed

+484
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
/*
2+
Copyright 2024 The Karmada Authors.
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 configmanager
18+
19+
import (
20+
"testing"
21+
22+
"github.com/stretchr/testify/assert"
23+
admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
24+
webhookutil "k8s.io/apiserver/pkg/util/webhook"
25+
"k8s.io/utils/pointer"
26+
27+
configv1alpha1 "github.com/karmada-io/karmada/pkg/apis/config/v1alpha1"
28+
)
29+
30+
func TestNewResourceExploringAccessor(t *testing.T) {
31+
testCases := []struct {
32+
name string
33+
uid string
34+
configurationName string
35+
webhook *configv1alpha1.ResourceInterpreterWebhook
36+
}{
37+
{
38+
name: "basic webhook accessor",
39+
uid: "test-uid-1",
40+
configurationName: "test-config-1",
41+
webhook: &configv1alpha1.ResourceInterpreterWebhook{
42+
Name: "test-webhook",
43+
},
44+
},
45+
{
46+
name: "webhook accessor with empty fields",
47+
uid: "",
48+
configurationName: "",
49+
webhook: &configv1alpha1.ResourceInterpreterWebhook{},
50+
},
51+
}
52+
53+
for _, tc := range testCases {
54+
t.Run(tc.name, func(t *testing.T) {
55+
accessor := NewResourceExploringAccessor(tc.uid, tc.configurationName, tc.webhook)
56+
57+
assert.NotNil(t, accessor, "accessor should not be nil")
58+
assert.Equal(t, tc.uid, accessor.GetUID(), "uid should match")
59+
assert.Equal(t, tc.configurationName, accessor.GetConfigurationName(), "configuration name should match")
60+
assert.Equal(t, tc.webhook.Name, accessor.GetName(), "webhook name should match")
61+
})
62+
}
63+
}
64+
65+
func TestResourceExploringAccessor_Getters(t *testing.T) {
66+
timeoutSeconds := int32(10)
67+
testWebhook := &configv1alpha1.ResourceInterpreterWebhook{
68+
Name: "test-webhook",
69+
ClientConfig: admissionregistrationv1.WebhookClientConfig{
70+
URL: pointer.String("https://test-webhook.example.com"),
71+
Service: &admissionregistrationv1.ServiceReference{
72+
Name: "test-service",
73+
Namespace: "test-namespace",
74+
Port: pointer.Int32(443),
75+
Path: pointer.String("/validate"),
76+
},
77+
CABundle: []byte("test-ca-bundle"),
78+
},
79+
Rules: []configv1alpha1.RuleWithOperations{
80+
{
81+
Operations: []configv1alpha1.InterpreterOperation{"interpret"},
82+
Rule: configv1alpha1.Rule{
83+
APIGroups: []string{""},
84+
APIVersions: []string{"v1"},
85+
Kinds: []string{"Pod"},
86+
},
87+
},
88+
},
89+
TimeoutSeconds: &timeoutSeconds,
90+
InterpreterContextVersions: []string{"v1", "v2"},
91+
}
92+
93+
testCases := []struct {
94+
name string
95+
uid string
96+
configurationName string
97+
webhook *configv1alpha1.ResourceInterpreterWebhook
98+
}{
99+
{
100+
name: "complete webhook configuration",
101+
uid: "test-uid-1",
102+
configurationName: "test-config-1",
103+
webhook: testWebhook,
104+
},
105+
}
106+
107+
for _, tc := range testCases {
108+
t.Run(tc.name, func(t *testing.T) {
109+
accessor := NewResourceExploringAccessor(tc.uid, tc.configurationName, tc.webhook)
110+
111+
// Test all getters
112+
assert.Equal(t, tc.webhook.ClientConfig, accessor.GetClientConfig(), "client config should match")
113+
assert.Equal(t, tc.webhook.Rules, accessor.GetRules(), "rules should match")
114+
assert.Equal(t, tc.webhook.TimeoutSeconds, accessor.GetTimeoutSeconds(), "timeout seconds should match")
115+
assert.Equal(t, tc.webhook.InterpreterContextVersions, accessor.GetInterpreterContextVersions(), "interpreter context versions should match")
116+
})
117+
}
118+
}
119+
120+
func TestHookClientConfigForWebhook(t *testing.T) {
121+
testCases := []struct {
122+
name string
123+
hookName string
124+
clientConfig admissionregistrationv1.WebhookClientConfig
125+
expected webhookutil.ClientConfig
126+
}{
127+
{
128+
name: "URL configuration",
129+
hookName: "test-webhook",
130+
clientConfig: admissionregistrationv1.WebhookClientConfig{
131+
URL: pointer.String("https://test-webhook.example.com"),
132+
CABundle: []byte("test-ca-bundle"),
133+
},
134+
expected: webhookutil.ClientConfig{
135+
Name: "test-webhook",
136+
URL: "https://test-webhook.example.com",
137+
CABundle: []byte("test-ca-bundle"),
138+
},
139+
},
140+
{
141+
name: "Service configuration with defaults",
142+
hookName: "test-webhook",
143+
clientConfig: admissionregistrationv1.WebhookClientConfig{
144+
Service: &admissionregistrationv1.ServiceReference{
145+
Name: "test-service",
146+
Namespace: "test-namespace",
147+
},
148+
CABundle: []byte("test-ca-bundle"),
149+
},
150+
expected: webhookutil.ClientConfig{
151+
Name: "test-webhook",
152+
CABundle: []byte("test-ca-bundle"),
153+
Service: &webhookutil.ClientConfigService{
154+
Name: "test-service",
155+
Namespace: "test-namespace",
156+
Port: 443,
157+
},
158+
},
159+
},
160+
{
161+
name: "Service configuration with custom values",
162+
hookName: "test-webhook",
163+
clientConfig: admissionregistrationv1.WebhookClientConfig{
164+
Service: &admissionregistrationv1.ServiceReference{
165+
Name: "test-service",
166+
Namespace: "test-namespace",
167+
Port: pointer.Int32(8443),
168+
Path: pointer.String("/validate"),
169+
},
170+
CABundle: []byte("test-ca-bundle"),
171+
},
172+
expected: webhookutil.ClientConfig{
173+
Name: "test-webhook",
174+
CABundle: []byte("test-ca-bundle"),
175+
Service: &webhookutil.ClientConfigService{
176+
Name: "test-service",
177+
Namespace: "test-namespace",
178+
Port: 8443,
179+
Path: "/validate",
180+
},
181+
},
182+
},
183+
{
184+
name: "Empty service configuration",
185+
hookName: "test-webhook",
186+
clientConfig: admissionregistrationv1.WebhookClientConfig{
187+
Service: &admissionregistrationv1.ServiceReference{},
188+
CABundle: []byte("test-ca-bundle"),
189+
},
190+
expected: webhookutil.ClientConfig{
191+
Name: "test-webhook",
192+
CABundle: []byte("test-ca-bundle"),
193+
Service: &webhookutil.ClientConfigService{
194+
Port: 443,
195+
},
196+
},
197+
},
198+
{
199+
name: "Nil service and URL configuration",
200+
hookName: "test-webhook",
201+
clientConfig: admissionregistrationv1.WebhookClientConfig{
202+
CABundle: []byte("test-ca-bundle"),
203+
},
204+
expected: webhookutil.ClientConfig{
205+
Name: "test-webhook",
206+
CABundle: []byte("test-ca-bundle"),
207+
},
208+
},
209+
}
210+
211+
for _, tc := range testCases {
212+
t.Run(tc.name, func(t *testing.T) {
213+
result := hookClientConfigForWebhook(tc.hookName, tc.clientConfig)
214+
assert.Equal(t, tc.expected, result, "webhook client config should match expected configuration")
215+
})
216+
}
217+
}

0 commit comments

Comments
 (0)