@@ -18,6 +18,7 @@ import (
18
18
19
19
crdv1 "github.com/kubernetes-csi/external-snapshotter/pkg/apis/volumesnapshot/v1beta1"
20
20
"github.com/kubernetes-csi/external-snapshotter/pkg/utils"
21
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
21
22
"k8s.io/client-go/tools/cache"
22
23
)
23
24
@@ -89,3 +90,53 @@ func TestControllerCacheParsingError(t *testing.T) {
89
90
t .Errorf ("Expected parsing error, got nil instead" )
90
91
}
91
92
}
93
+
94
+ // TestShouldDelete tests logic for deleting VolumeSnapshotContent objects.
95
+ func TestShouldDelete (t * testing.T ) {
96
+ // Use an empty controller, since there's no struct
97
+ // state we need to use in this test.
98
+ ctrl := & csiSnapshotSideCarController {}
99
+
100
+ tests := []struct {
101
+ name string
102
+ expectedReturn bool
103
+ content * crdv1.VolumeSnapshotContent
104
+ }{
105
+ {
106
+ name : "DeletionTimeStamp is nil" ,
107
+ expectedReturn : false ,
108
+ content : newContent ("test-content" , "snap-uuid" , "snapName" , "desiredHandle" , "default" , "desiredHandle" , "volHandle" , crdv1 .VolumeSnapshotContentDelete , nil , & defaultSize , false , nil ),
109
+ },
110
+ {
111
+ name : "Content is not bound" ,
112
+ expectedReturn : true ,
113
+ content : newContent ("test-content-not-bound" , "" , "" , "snapshotHandle" , "" , "" , "" , crdv1 .VolumeSnapshotContentDelete , nil , & defaultSize , false , & timeNowMetav1 ),
114
+ },
115
+ {
116
+ name : "AnnVolumeSnapshotBeingDeleted annotation is set. " ,
117
+ expectedReturn : true ,
118
+ // DeletionTime means that annotation is set, and being bound means the other cases are skipped.
119
+ content : newContent ("test-content" , "snap-uuid" , "snapName" , "desiredHandle" , "default" , "desiredHandle" , "volHandle" , crdv1 .VolumeSnapshotContentDelete , nil , & defaultSize , false , & timeNowMetav1 ),
120
+ },
121
+ {
122
+ name : "If no other cases match, then should not delete" ,
123
+ expectedReturn : false ,
124
+ // Use an object that does not conform to newContent's logic in order to skip the conditionals inside shouldDelete
125
+ content : & crdv1.VolumeSnapshotContent {
126
+ ObjectMeta : metav1.ObjectMeta {
127
+ Name : "test-content" ,
128
+ DeletionTimestamp : & timeNowMetav1 ,
129
+ },
130
+ },
131
+ },
132
+ }
133
+
134
+ for _ , test := range tests {
135
+ result := ctrl .shouldDelete (test .content )
136
+
137
+ if result != test .expectedReturn {
138
+ t .Errorf ("Got %t but expected %t for test: %s" , result , test .expectedReturn , test .name )
139
+ }
140
+
141
+ }
142
+ }
0 commit comments