Skip to content

Commit 13dbb09

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

File tree

2 files changed

+466
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)