Skip to content

Commit

Permalink
Add test for PersistentVolume to determiner (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
micnncim committed Sep 19, 2020
1 parent e981b98 commit a70ff9e
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 1 deletion.
4 changes: 3 additions & 1 deletion pkg/determiner/determiner.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"github.com/micnncim/kubectl-prune/pkg/resource"
)

var checkVolumeSatisfyClaimFunc = resource.CheckVolumeSatisfyClaim

type Determiner interface {
DetermineDeletion(ctx context.Context, info *cliresource.Info) (bool, error)
}
Expand Down Expand Up @@ -130,7 +132,7 @@ func (d *determiner) DetermineDeletion(ctx context.Context, info *cliresource.In
}

for _, claim := range d.persistentVolumeClaims {
if ok := resource.CheckVolumeSatisfyClaim(volume, claim); ok {
if ok := checkVolumeSatisfyClaimFunc(volume, claim); ok {
return false, nil
}
}
Expand Down
115 changes: 115 additions & 0 deletions pkg/determiner/determiner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,121 @@ func Test_determiner_DetermineDeletion(t *testing.T) {
}
}

func Test_determiner_DetermineDeletion_PersistentVolume(t *testing.T) {
const (
fakePersistentVolume = "fake-pv"
fakePersistentVolumeClaim1 = "fake-pvc1"
fakePersistentVolumeClaim2 = "fake-pvc2"
fakeLabelKey = "fake-label-key"
fakeLabelValue = "fake-label-value"
)

var orgCheckVolumeSatisfyClaimFunc func(volume *corev1.PersistentVolume, claim *corev1.PersistentVolumeClaim) bool
orgCheckVolumeSatisfyClaimFunc, checkVolumeSatisfyClaimFunc =
checkVolumeSatisfyClaimFunc,
func(volume *corev1.PersistentVolume, claim *corev1.PersistentVolumeClaim) bool {
return volume.Labels[fakeLabelKey] == claim.Labels[fakeLabelKey]
}
t.Cleanup(func() {
checkVolumeSatisfyClaimFunc = orgCheckVolumeSatisfyClaimFunc
})

type fields struct {
persistentVolumeClaims []*corev1.PersistentVolumeClaim
}
type args struct {
info *cliresource.Info
}

tests := []struct {
name string
fields fields
args args
want bool
wantErr bool
}{
{
name: "PersistentVolume should be deleted when it is not used",
fields: fields{},
args: args{
info: &cliresource.Info{
Name: fakePersistentVolume,
Object: &corev1.PersistentVolume{
TypeMeta: metav1.TypeMeta{
Kind: resource.KindPersistentVolume,
},
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
fakeLabelKey: fakeLabelValue,
},
},
},
},
},
want: true,
wantErr: false,
},
{
name: "PersistentVolume should not be deleted when it is used",
fields: fields{
persistentVolumeClaims: []*corev1.PersistentVolumeClaim{
{
ObjectMeta: metav1.ObjectMeta{
Name: fakePersistentVolumeClaim1,
Labels: map[string]string{
fakeLabelKey: fakeLabelValue,
},
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: fakePersistentVolumeClaim2,
},
},
},
},
args: args{
info: &cliresource.Info{
Name: fakePersistentVolume,
Object: &corev1.PersistentVolume{
TypeMeta: metav1.TypeMeta{
Kind: resource.KindPersistentVolume,
},
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
fakeLabelKey: fakeLabelValue,
},
},
},
},
},
want: false,
wantErr: false,
},
}

for _, tt := range tests {
tt := tt

t.Run(tt.name, func(t *testing.T) {
t.Parallel()

d := &determiner{
persistentVolumeClaims: tt.fields.persistentVolumeClaims,
}

got, err := d.DetermineDeletion(context.Background(), tt.args.info)
if (err != nil) != tt.wantErr {
t.Errorf("determiner.DetermineDeletion() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("determiner.DetermineDeletion() = %v, want %v", got, tt.want)
}
})
}
}

func Test_determiner_DetermineDeletion_HorizontalPodAutoscaler(t *testing.T) {
const (
fakeNamespace = "fake-ns"
Expand Down

0 comments on commit a70ff9e

Please sign in to comment.