-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6910 from adrianmoisey/add_tests
VPA: Add tests for selfRegistration() and filterVPAs()
- Loading branch information
Showing
3 changed files
with
165 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
vertical-pod-autoscaler/pkg/admission-controller/config_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
Copyright 2024 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
admissionregistration "k8s.io/api/admissionregistration/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes/fake" | ||
) | ||
|
||
func TestSelfRegistrationBase(t *testing.T) { | ||
|
||
testClientSet := fake.NewSimpleClientset() | ||
caCert := []byte("fake") | ||
namespace := "default" | ||
serviceName := "vpa-service" | ||
url := "http://example.com/" | ||
registerByURL := true | ||
timeoutSeconds := int32(32) | ||
|
||
selfRegistration(testClientSet, caCert, namespace, serviceName, url, registerByURL, timeoutSeconds) | ||
|
||
webhookConfigInterface := testClientSet.AdmissionregistrationV1().MutatingWebhookConfigurations() | ||
webhookConfig, err := webhookConfigInterface.Get(context.TODO(), webhookConfigName, metav1.GetOptions{}) | ||
|
||
assert.NoError(t, err, "expected no error fetching webhook configuration") | ||
assert.Equal(t, webhookConfigName, webhookConfig.Name, "expected webhook configuration name 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 TestSelfRegistrationWithURL(t *testing.T) { | ||
|
||
testClientSet := fake.NewSimpleClientset() | ||
caCert := []byte("fake") | ||
namespace := "default" | ||
serviceName := "vpa-service" | ||
url := "http://example.com/" | ||
registerByURL := true | ||
timeoutSeconds := int32(32) | ||
|
||
selfRegistration(testClientSet, caCert, namespace, serviceName, url, registerByURL, timeoutSeconds) | ||
|
||
webhookConfigInterface := testClientSet.AdmissionregistrationV1().MutatingWebhookConfigurations() | ||
webhookConfig, err := webhookConfigInterface.Get(context.TODO(), webhookConfigName, metav1.GetOptions{}) | ||
|
||
assert.NoError(t, err, "expected no error fetching webhook configuration") | ||
|
||
assert.Len(t, webhookConfig.Webhooks, 1, "expected one webhook configuration") | ||
webhook := webhookConfig.Webhooks[0] | ||
|
||
assert.Nil(t, webhook.ClientConfig.Service, "expected service reference to be nil") | ||
assert.NotNil(t, webhook.ClientConfig.URL, "expected URL to be set") | ||
assert.Equal(t, url, *webhook.ClientConfig.URL, "expected URL to match") | ||
} | ||
|
||
func TestSelfRegistrationWithOutURL(t *testing.T) { | ||
|
||
testClientSet := fake.NewSimpleClientset() | ||
caCert := []byte("fake") | ||
namespace := "default" | ||
serviceName := "vpa-service" | ||
url := "http://example.com/" | ||
registerByURL := false | ||
timeoutSeconds := int32(32) | ||
|
||
selfRegistration(testClientSet, caCert, namespace, serviceName, url, registerByURL, timeoutSeconds) | ||
|
||
webhookConfigInterface := testClientSet.AdmissionregistrationV1().MutatingWebhookConfigurations() | ||
webhookConfig, err := webhookConfigInterface.Get(context.TODO(), webhookConfigName, metav1.GetOptions{}) | ||
|
||
assert.NoError(t, err, "expected no error fetching webhook configuration") | ||
|
||
assert.Len(t, webhookConfig.Webhooks, 1, "expected one webhook configuration") | ||
webhook := webhookConfig.Webhooks[0] | ||
|
||
assert.NotNil(t, webhook.ClientConfig.Service, "expected service reference to be nil") | ||
assert.Equal(t, webhook.ClientConfig.Service.Name, serviceName, "expected service name to be equal") | ||
assert.Equal(t, webhook.ClientConfig.Service.Namespace, namespace, "expected service namespace to be equal") | ||
|
||
assert.Nil(t, webhook.ClientConfig.URL, "expected URL to be set") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters