@@ -17,32 +17,168 @@ limitations under the License.
1717package sidecar_controller
1818
1919import (
20+ "errors"
2021 "testing"
2122 "time"
23+
24+ crdv1 "github.com/kubernetes-csi/external-snapshotter/v2/pkg/apis/volumesnapshot/v1beta1"
25+ "github.com/kubernetes-csi/external-snapshotter/v2/pkg/utils"
26+ v1 "k8s.io/api/core/v1"
2227)
2328
2429func TestSyncContent (t * testing.T ) {
25- var tests []controllerTest
26-
27- tests = append (tests , controllerTest {
28- name : "Basic content update ready to use" ,
29- initialContents : newContentArrayWithReadyToUse ("content1-1" , "snapuid1-1" , "snap1-1" , "sid1-1" , defaultClass , "" , "volume-handle-1-1" , retainPolicy , nil , & defaultSize , & False , true ),
30- expectedContents : newContentArrayWithReadyToUse ("content1-1" , "snapuid1-1" , "snap1-1" , "sid1-1" , defaultClass , "" , "volume-handle-1-1" , retainPolicy , nil , & defaultSize , & True , true ),
31- expectedEvents : noevents ,
32- expectedCreateCalls : []createCall {
33- {
34- volumeHandle : "volume-handle-1-1" ,
35- snapshotName : "snapshot-snapuid1-1" ,
36- driverName : mockDriverName ,
37- snapshotId : "snapuid1-1" ,
38- creationTime : timeNow ,
39- readyToUse : true ,
30+
31+ tests := []controllerTest {
32+ {
33+ name : "1-1: Basic content update ready to use" ,
34+ initialContents : newContentArrayWithReadyToUse ("content1-1" , "snapuid1-1" , "snap1-1" , "sid1-1" , defaultClass , "" , "volume-handle-1-1" , retainPolicy , nil , & defaultSize , & False , true ),
35+ expectedContents : newContentArrayWithReadyToUse ("content1-1" , "snapuid1-1" , "snap1-1" , "sid1-1" , defaultClass , "" , "volume-handle-1-1" , retainPolicy , nil , & defaultSize , & True , true ),
36+ expectedEvents : noevents ,
37+ expectedCreateCalls : []createCall {
38+ {
39+ volumeHandle : "volume-handle-1-1" ,
40+ snapshotName : "snapshot-snapuid1-1" ,
41+ driverName : mockDriverName ,
42+ snapshotId : "snapuid1-1" ,
43+ creationTime : timeNow ,
44+ readyToUse : true ,
45+ },
46+ },
47+ expectedListCalls : []listCall {{"sid1-1" , map [string ]string {}, true , time .Now (), 1 , nil }},
48+ errors : noerrors ,
49+ test : testSyncContent ,
50+ },
51+ {
52+ name : "1-2: Basic sync content create snapshot" ,
53+ initialContents : withContentStatus (newContentArray ("content1-2" , "snapuid1-2" , "snap1-2" , "sid1-2" , defaultClass , "" , "volume-handle-1-2" , retainPolicy , nil , & defaultSize , true ),
54+ nil ),
55+ expectedContents : withContentStatus (newContentArray ("content1-2" , "snapuid1-2" , "snap1-2" , "sid1-2" , defaultClass , "" , "volume-handle-1-2" , retainPolicy , nil , & defaultSize , true ),
56+ & crdv1.VolumeSnapshotContentStatus {SnapshotHandle : toStringPointer ("snapuid1-2" ), RestoreSize : & defaultSize , ReadyToUse : & True }),
57+ expectedEvents : noevents ,
58+ expectedCreateCalls : []createCall {
59+ {
60+ volumeHandle : "volume-handle-1-2" ,
61+ snapshotName : "snapshot-snapuid1-2" ,
62+ driverName : mockDriverName ,
63+ snapshotId : "snapuid1-2" ,
64+ creationTime : timeNow ,
65+ readyToUse : true ,
66+ size : defaultSize ,
67+ },
68+ },
69+ expectedListCalls : []listCall {{"sid1-2" , map [string ]string {}, true , time .Now (), 1 , nil }},
70+ errors : noerrors ,
71+ test : testSyncContent ,
72+ },
73+ {
74+ name : "1-3: Basic sync content create snapshot with non-existent secret" ,
75+ initialContents : withContentAnnotations (withContentStatus (newContentArray ("content1-3" , "snapuid1-3" , "snap1-3" , "sid1-3" , invalidSecretClass , "" , "volume-handle-1-3" , retainPolicy , nil , & defaultSize , true ),
76+ nil ), map [string ]string {
77+ utils .AnnDeletionSecretRefName : "" ,
78+ utils .AnnDeletionSecretRefNamespace : "" ,
79+ }),
80+ expectedContents : withContentAnnotations (withContentStatus (newContentArray ("content1-3" , "snapuid1-3" , "snap1-3" , "sid1-3" , invalidSecretClass , "" , "volume-handle-1-3" , retainPolicy , nil , & defaultSize , true ),
81+ & crdv1.VolumeSnapshotContentStatus {
82+ SnapshotHandle : nil ,
83+ RestoreSize : nil ,
84+ ReadyToUse : & False ,
85+ Error : newSnapshotError ("Failed to create snapshot: failed to get input parameters to create snapshot for content content1-3: \" cannot retrieve secrets for snapshot content \\ \" content1-3\\ \" , err: secret name or namespace not specified\" " ),
86+ }), map [string ]string {
87+ utils .AnnDeletionSecretRefName : "" ,
88+ utils .AnnDeletionSecretRefNamespace : "" ,
89+ }), initialSecrets : []* v1.Secret {}, // no initial secret created
90+ expectedEvents : []string {"Warning SnapshotCreationFailed" },
91+ errors : noerrors ,
92+ test : testSyncContent ,
93+ },
94+ {
95+ name : "1-4: Basic sync content create snapshot with valid secret" ,
96+ initialContents : withContentAnnotations (withContentStatus (newContentArray ("content1-4" , "snapuid1-4" , "snap1-4" , "sid1-4" , validSecretClass , "" , "volume-handle-1-4" , retainPolicy , nil , & defaultSize , true ),
97+ nil ), map [string ]string {
98+ utils .AnnDeletionSecretRefName : "secret" ,
99+ utils .AnnDeletionSecretRefNamespace : "default" ,
100+ }),
101+ expectedContents : withContentAnnotations (withContentStatus (newContentArray ("content1-4" , "snapuid1-4" , "snap1-4" , "sid1-4" , validSecretClass , "" , "volume-handle-1-4" , retainPolicy , nil , & defaultSize , true ),
102+ & crdv1.VolumeSnapshotContentStatus {
103+ SnapshotHandle : toStringPointer ("snapuid1-4" ),
104+ RestoreSize : & defaultSize ,
105+ ReadyToUse : & True ,
106+ Error : nil ,
107+ }), map [string ]string {
108+ utils .AnnDeletionSecretRefName : "secret" ,
109+ utils .AnnDeletionSecretRefNamespace : "default" ,
110+ }),
111+ expectedCreateCalls : []createCall {
112+ {
113+ volumeHandle : "volume-handle-1-4" ,
114+ snapshotName : "snapshot-snapuid1-4" ,
115+ parameters : class5Parameters ,
116+ secrets : map [string ]string {
117+ "foo" : "bar" ,
118+ },
119+ driverName : mockDriverName ,
120+ snapshotId : "snapuid1-4" ,
121+ creationTime : timeNow ,
122+ readyToUse : true ,
123+ size : defaultSize ,
124+ },
125+ },
126+ initialSecrets : []* v1.Secret {secret ()},
127+ expectedEvents : noevents ,
128+ errors : noerrors ,
129+ test : testSyncContent ,
130+ },
131+ {
132+ name : "1-5: Basic sync content create snapshot with failed secret call" ,
133+ initialContents : withContentAnnotations (withContentStatus (newContentArray ("content1-5" , "snapuid1-5" , "snap1-5" , "sid1-5" , invalidSecretClass , "" , "volume-handle-1-5" , retainPolicy , nil , & defaultSize , true ),
134+ nil ), map [string ]string {
135+ utils .AnnDeletionSecretRefName : "secret" ,
136+ utils .AnnDeletionSecretRefNamespace : "default" ,
137+ }),
138+ expectedContents : withContentAnnotations (withContentStatus (newContentArray ("content1-5" , "snapuid1-5" , "snap1-5" , "sid1-5" , invalidSecretClass , "" , "volume-handle-1-5" , retainPolicy , nil , & defaultSize , true ),
139+ & crdv1.VolumeSnapshotContentStatus {
140+ SnapshotHandle : nil ,
141+ RestoreSize : nil ,
142+ ReadyToUse : & False ,
143+ Error : newSnapshotError ("Failed to create snapshot: failed to get input parameters to create snapshot for content content1-5: \" cannot get credentials for snapshot content \\ \" content1-5\\ \" \" " ),
144+ }), map [string ]string {
145+ utils .AnnDeletionSecretRefName : "secret" ,
146+ utils .AnnDeletionSecretRefNamespace : "default" ,
147+ }), initialSecrets : []* v1.Secret {}, // no initial secret created
148+ expectedEvents : []string {"Warning SnapshotCreationFailed" },
149+ errors : []reactorError {
150+ // Inject error to the first client.VolumesnapshotV1beta1().VolumeSnapshots().Update call.
151+ // All other calls will succeed.
152+ {"get" , "secrets" , errors .New ("mock secrets error" )},
153+ },
154+ test : testSyncContent ,
155+ },
156+ {
157+ name : "1-6: Basic content update ready to use bad snapshot class" ,
158+ initialContents : newContentArrayWithReadyToUse ("content1-6" , "snapuid1-6" , "snap1-6" , "sid1-6" , "bad-class" , "" , "volume-handle-1-6" , retainPolicy , nil , & defaultSize , & False , true ),
159+ expectedContents : withContentStatus (newContentArray ("content1-6" , "snapuid1-6" , "snap1-6" , "sid1-6" , "bad-class" , "" , "volume-handle-1-6" , retainPolicy , nil , & defaultSize , true ),
160+ & crdv1.VolumeSnapshotContentStatus {
161+ SnapshotHandle : toStringPointer ("sid1-6" ),
162+ RestoreSize : & defaultSize ,
163+ ReadyToUse : & False ,
164+ Error : newSnapshotError ("Failed to check and update snapshot content: failed to get input parameters to create snapshot content1-6: \" failed to retrieve snapshot class bad-class from the informer: \\ \" volumesnapshotclass.snapshot.storage.k8s.io \\ \\ \\ \" bad-class\\ \\ \\ \" not found\\ \" \" " ),
165+ }),
166+ expectedEvents : []string {"Warning SnapshotContentCheckandUpdateFailed" },
167+ expectedCreateCalls : []createCall {
168+ {
169+ volumeHandle : "volume-handle-1-6" ,
170+ snapshotName : "snapshot-snapuid1-6" ,
171+ driverName : mockDriverName ,
172+ snapshotId : "snapuid1-6" ,
173+ creationTime : timeNow ,
174+ readyToUse : true ,
175+ },
40176 },
177+ expectedListCalls : []listCall {{"sid1-6" , map [string ]string {}, true , time .Now (), 1 , nil }},
178+ errors : noerrors ,
179+ test : testSyncContent ,
41180 },
42- expectedListCalls : []listCall {{"sid1-1" , map [string ]string {}, true , time .Now (), 1 , nil }},
43- errors : noerrors ,
44- test : testSyncContent ,
45- })
181+ }
46182
47183 runSyncContentTests (t , tests , snapshotClasses )
48184}
0 commit comments