Skip to content

Commit

Permalink
add ut
Browse files Browse the repository at this point in the history
  • Loading branch information
umagnus committed Oct 17, 2024
1 parent ce80e4b commit 4eef11b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
3 changes: 2 additions & 1 deletion vertical-pod-autoscaler/pkg/admission-controller/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ func selfRegistration(clientset kubernetes.Interface, caCert []byte, webHookDela
}
webhookLabelsMap, err := convertLabelsToMap(webHookLabels)
if err != nil {
klog.Fatal(err)
klog.Warning(err)
webhookLabelsMap = map[string]string{}
}
webhookConfig := &admissionregistration.MutatingWebhookConfiguration{
ObjectMeta: metav1.ObjectMeta{
Expand Down
57 changes: 55 additions & 2 deletions vertical-pod-autoscaler/pkg/admission-controller/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func TestSelfRegistrationBase(t *testing.T) {

assert.NoError(t, err, "expected no error fetching webhook configuration")
assert.Equal(t, webhookConfigName, webhookConfig.Name, "expected webhook configuration name to match")
assert.Equal(t, webhookConfig.Labels, map[string]string{"key1": "value1", "key2": "value2"}, "expected webhook configuration name labels to match")
assert.Equal(t, webhookConfig.Labels, map[string]string{"key1": "value1", "key2": "value2"}, "expected webhook configuration labels to match")

assert.Len(t, webhookConfig.Webhooks, 1, "expected one webhook configuration")
webhook := webhookConfig.Webhooks[0]
Expand Down Expand Up @@ -246,6 +246,50 @@ func TestSelfRegistrationWithOutFailurePolicy(t *testing.T) {
assert.Equal(t, *webhook.FailurePolicy, admissionregistration.Ignore, "expected failurePolicy to be Ignore")
}

func TestSelfRegistrationWithInvalidLabels(t *testing.T) {

testClientSet := fake.NewSimpleClientset()
caCert := []byte("fake")
webHookDelay := 0 * time.Second
namespace := "default"
serviceName := "vpa-service"
url := "http://example.com/"
registerByURL := true
timeoutSeconds := int32(32)
selectedNamespace := ""
ignoredNamespaces := []string{}

selfRegistration(testClientSet, caCert, webHookDelay, namespace, serviceName, url, registerByURL, timeoutSeconds, selectedNamespace, ignoredNamespaces, false, "foo,bar")

webhookConfigInterface := testClientSet.AdmissionregistrationV1().MutatingWebhookConfigurations()
webhookConfig, err := webhookConfigInterface.Get(context.TODO(), webhookConfigName, metav1.GetOptions{})

assert.NoError(t, err, "expected invalid labels error fetching webhook configuration")
assert.Equal(t, webhookConfigName, webhookConfig.Name, "expected webhook configuration name to match")
assert.Equal(t, webhookConfig.Labels, map[string]string{}, "expected invalid webhook configuration labels to match")

assert.Len(t, webhookConfig.Webhooks, 1, "expected one webhook configuration")
webhook := webhookConfig.Webhooks[0]
assert.Equal(t, "vpa.k8s.io", webhook.Name, "expected webhook name to match")

PodRule := webhook.Rules[0]
assert.Equal(t, []admissionregistration.OperationType{admissionregistration.Create}, PodRule.Operations, "expected operations to match")
assert.Equal(t, []string{""}, PodRule.APIGroups, "expected API groups to match")
assert.Equal(t, []string{"v1"}, PodRule.APIVersions, "expected API versions to match")
assert.Equal(t, []string{"pods"}, PodRule.Resources, "expected resources to match")

VPARule := webhook.Rules[1]
assert.Equal(t, []admissionregistration.OperationType{admissionregistration.Create, admissionregistration.Update}, VPARule.Operations, "expected operations to match")
assert.Equal(t, []string{"autoscaling.k8s.io"}, VPARule.APIGroups, "expected API groups to match")
assert.Equal(t, []string{"*"}, VPARule.APIVersions, "ehook.Rulxpected API versions to match")
assert.Equal(t, []string{"verticalpodautoscalers"}, VPARule.Resources, "expected resources to match")

assert.Equal(t, admissionregistration.SideEffectClassNone, *webhook.SideEffects, "expected side effects to match")
assert.Equal(t, admissionregistration.Ignore, *webhook.FailurePolicy, "expected failure policy to match")
assert.Equal(t, caCert, webhook.ClientConfig.CABundle, "expected CA bundle to match")
assert.Equal(t, timeoutSeconds, *webhook.TimeoutSeconds, "expected timeout seconds to match")
}

func TestConvertLabelsToMap(t *testing.T) {
testCases := []struct {
desc string
Expand All @@ -260,7 +304,7 @@ func TestConvertLabelsToMap(t *testing.T) {
expectedError: false,
},
{
desc: "sing valid tag should be converted",
desc: "single valid tag should be converted",
labels: "key:value",
expectedOutput: map[string]string{
"key": "value",
Expand All @@ -285,6 +329,15 @@ func TestConvertLabelsToMap(t *testing.T) {
},
expectedError: false,
},
{
desc: "whitespaces between keys and values should be trimmed",
labels: "key1 : value1,key2 : value2",
expectedOutput: map[string]string{
"key1": "value1",
"key2": "value2",
},
expectedError: false,
},
{
desc: "should return error for invalid format",
labels: "foo,bar",
Expand Down

0 comments on commit 4eef11b

Please sign in to comment.