Skip to content

Commit

Permalink
test: added tests for mutating webhook analyzer (#995)
Browse files Browse the repository at this point in the history
This commit introduces comprehensive tests for the mutating webhook
analyzer defined in the `pkg/analyzer` package.

Adding these tests increases the code coverage of the
`mutating_webhook.go` file to almost 95%.

Partially addresses: #889

Signed-off-by: VaibhavMalik4187 <vaibhavmalik2018@gmail.com>
  • Loading branch information
VaibhavMalik4187 committed Mar 8, 2024
1 parent 425f33b commit 5199dad
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 3 deletions.
140 changes: 140 additions & 0 deletions pkg/analyzer/mutating_webhook_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
Copyright 2024 The K8sGPT 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 analyzer

import (
"context"
"testing"

"github.com/k8sgpt-ai/k8sgpt/pkg/common"
"github.com/k8sgpt-ai/k8sgpt/pkg/kubernetes"
"github.com/stretchr/testify/require"
admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/fake"
)

func TestMutatingWebhookAnalyzer(t *testing.T) {
config := common.Analyzer{
Client: &kubernetes.Client{
Client: fake.NewSimpleClientset(
&v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "Pod1",
Namespace: "default",
Labels: map[string]string{
"pod": "Pod1",
},
},
},
&v1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: "test-service1",
Namespace: "default",
},
Spec: v1.ServiceSpec{
Selector: map[string]string{
"pod": "Pod1",
},
},
},
&v1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: "test-service2",
Namespace: "test",
},
Spec: v1.ServiceSpec{
// No such pod exists in the test namespace
Selector: map[string]string{
"pod": "Pod2",
},
},
},
&v1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: "test-service3",
Namespace: "test",
},
Spec: v1.ServiceSpec{
// len(service.Spec.Selector) == 0
Selector: map[string]string{},
},
},
&admissionregistrationv1.MutatingWebhookConfiguration{
ObjectMeta: metav1.ObjectMeta{
Name: "test-mutating-webhook-config",
Namespace: "test",
},
Webhooks: []admissionregistrationv1.MutatingWebhook{
{
// Failure: Pointing to an inactive receiver pod
Name: "webhook1",
ClientConfig: admissionregistrationv1.WebhookClientConfig{
Service: &admissionregistrationv1.ServiceReference{
Name: "test-service1",
Namespace: "default",
},
},
},
{
// Failure: No active pods found in the test namespace
Name: "webhook2",
ClientConfig: admissionregistrationv1.WebhookClientConfig{
Service: &admissionregistrationv1.ServiceReference{
Name: "test-service2",
Namespace: "test",
},
},
},
{
Name: "webhook3",
ClientConfig: admissionregistrationv1.WebhookClientConfig{
Service: &admissionregistrationv1.ServiceReference{
Name: "test-service3",
Namespace: "test",
},
},
},
{
// Failure: Service doesn't exist.
Name: "webhook4",
ClientConfig: admissionregistrationv1.WebhookClientConfig{
Service: &admissionregistrationv1.ServiceReference{
Name: "test-service4-doesn't-exist",
Namespace: "test",
},
},
},
{
// Service is nil.
Name: "webhook5",
ClientConfig: admissionregistrationv1.WebhookClientConfig{},
},
},
},
),
},
Context: context.Background(),
Namespace: "default",
}

mwAnalyzer := MutatingWebhookAnalyzer{}
results, err := mwAnalyzer.Analyze(config)
require.NoError(t, err)

// The results should contain: webhook1, webhook2, and webhook4
resultsLen := 3
require.Equal(t, resultsLen, len(results))
}
9 changes: 6 additions & 3 deletions pkg/analyzer/validating_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func TestValidatingWebhookAnalyzer(t *testing.T) {
},
Webhooks: []admissionregistrationv1.ValidatingWebhook{
{
// Failure: Pointing to an inactive receiver pod
Name: "webhook1",
ClientConfig: admissionregistrationv1.WebhookClientConfig{
Service: &admissionregistrationv1.ServiceReference{
Expand All @@ -88,7 +89,7 @@ func TestValidatingWebhookAnalyzer(t *testing.T) {
},
},
{
// No pod exists in the test namespace
// Failure: No active pods found in the test namespace
Name: "webhook2",
ClientConfig: admissionregistrationv1.WebhookClientConfig{
Service: &admissionregistrationv1.ServiceReference{
Expand All @@ -107,7 +108,7 @@ func TestValidatingWebhookAnalyzer(t *testing.T) {
},
},
{
// Service doesn't exist.
// Failure: Service doesn't exist.
Name: "webhook4",
ClientConfig: admissionregistrationv1.WebhookClientConfig{
Service: &admissionregistrationv1.ServiceReference{
Expand All @@ -128,10 +129,12 @@ func TestValidatingWebhookAnalyzer(t *testing.T) {
Context: context.Background(),
Namespace: "default",
}
resultsLen := 3

vwAnalyzer := ValidatingWebhookAnalyzer{}
results, err := vwAnalyzer.Analyze(config)
require.NoError(t, err)

// The results should contain: webhook1, webhook2, and webhook4
resultsLen := 3
require.Equal(t, resultsLen, len(results))
}

0 comments on commit 5199dad

Please sign in to comment.