Skip to content

Commit

Permalink
test: added tests for the PVC analyzer
Browse files Browse the repository at this point in the history
This commit introduces comprehensive tests fro the PersistentVolumeClaim
analyzer defined in the `pkg/analyzer` package.

Adding these tests increases the code coverage of the `pvc.go` file to
>95%.

Also made minor modifications in the ReplicaSet test to make sure that
sure that all the expectations are met.

Partially addresses: k8sgpt-ai#889

Signed-off-by: VaibhavMalik4187 <vaibhavmalik2018@gmail.com>
  • Loading branch information
VaibhavMalik4187 committed Mar 6, 2024
1 parent f5c3f18 commit b39ef39
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 4 deletions.
160 changes: 160 additions & 0 deletions pkg/analyzer/pvc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
/*
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"
"sort"
"testing"

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

func TestPersistentVolumeClaimAnalyzer(t *testing.T) {
config := common.Analyzer{
Client: &kubernetes.Client{
Client: fake.NewSimpleClientset(
&appsv1.Event{
// This is the latest event.
ObjectMeta: metav1.ObjectMeta{
Name: "Event1",
Namespace: "default",
},
Reason: "ProvisioningFailed",
Message: "PVC provisioning failed",
},
&appsv1.Event{
ObjectMeta: metav1.ObjectMeta{
// This event won't get selected.
Name: "Event2",
Namespace: "test",
},
},
&appsv1.Event{
ObjectMeta: metav1.ObjectMeta{
Name: "Event3",
Namespace: "default",
},
},
&appsv1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: "PVC1",
Namespace: "default",
},
Status: appsv1.PersistentVolumeClaimStatus{
Phase: appsv1.ClaimPending,
},
},
&appsv1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: "PVC2",
Namespace: "default",
},
Status: appsv1.PersistentVolumeClaimStatus{
// Won't contribute to failures.
Phase: appsv1.ClaimBound,
},
},
&appsv1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: "PVC3",
Namespace: "default",
},
Status: appsv1.PersistentVolumeClaimStatus{
// Won't contribute to failures.
Phase: appsv1.ClaimLost,
},
},
&appsv1.PersistentVolumeClaim{
// PVCs in namespace other than "default" won't be discovered.
ObjectMeta: metav1.ObjectMeta{
Name: "PVC4",
Namespace: "test",
},
Status: appsv1.PersistentVolumeClaimStatus{
Phase: appsv1.ClaimLost,
},
},
&appsv1.PersistentVolumeClaim{
// PVCs in namespace other than "default" won't be discovered.
ObjectMeta: metav1.ObjectMeta{
Name: "PVC5",
Namespace: "default",
},
Status: appsv1.PersistentVolumeClaimStatus{
Phase: appsv1.ClaimPending,
},
},
),
},
Context: context.Background(),
Namespace: "default",
}

pvcAnalyzer := PvcAnalyzer{}
results, err := pvcAnalyzer.Analyze(config)
require.NoError(t, err)

sort.Slice(results, func(i, j int) bool {
return results[i].Name < results[j].Name
})

expectations := []struct {
name string
}{
{
name: "default/PVC1",
},
{
name: "default/PVC5",
},
}

for i, expectation := range expectations {
require.Equal(t, expectation.name, results[i].Name)
for _, failure := range results[i].Error {
require.Equal(t, "PVC provisioning failed", failure.Text)
}
}
}

func TestPersistentVolumeClaimAnalyzerWithoutAnyEvents(t *testing.T) {
config := common.Analyzer{
Client: &kubernetes.Client{
Client: fake.NewSimpleClientset(
&appsv1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: "PVC1",
Namespace: "default",
},
Status: appsv1.PersistentVolumeClaimStatus{
Phase: appsv1.ClaimPending,
},
},
),
},
Context: context.Background(),
Namespace: "default",
}

pvcAnalyzer := PvcAnalyzer{}
results, err := pvcAnalyzer.Analyze(config)
require.NoError(t, err)
require.Equal(t, 0, len(results))
}
8 changes: 4 additions & 4 deletions pkg/analyzer/rs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,10 @@ func TestReplicaSetAnalyzer(t *testing.T) {
},
}

for i, result := range results {
require.Equal(t, expectations[i].name, result.Name)
for j, failure := range result.Error {
require.Equal(t, expectations[i].failuresText[j], failure.Text)
for i, expectation := range expectations {
require.Equal(t, expectation.name, results[i].Name)
for j, failure := range results[i].Error {
require.Equal(t, expectation.failuresText[j], failure.Text)
}
}
}

0 comments on commit b39ef39

Please sign in to comment.