@@ -17,8 +17,10 @@ package tests
1717import (
1818 "fmt"
1919 "strings"
20+ "time"
2021
2122 "k8s.io/apimachinery/pkg/util/uuid"
23+ "k8s.io/apimachinery/pkg/util/wait"
2224 "sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/common"
2325 gce "sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/gce-cloud-provider/compute"
2426
@@ -189,6 +191,151 @@ var _ = Describe("GCE PD CSI Driver", func() {
189191 // Test volume already exists idempotency
190192
191193 // Test volume with op pending
194+
195+ It ("Should create and delete snapshot for the volume with default zone" , func () {
196+ // Create new driver and client
197+ Expect (testInstances ).NotTo (BeEmpty ())
198+ testContext , err := testutils .GCEClientAndDriverSetup (testInstances [0 ])
199+ Expect (err ).To (BeNil (), "Set up new Driver and Client failed with error" )
200+ defer func () {
201+ err := remote .TeardownDriverAndClient (testContext )
202+ Expect (err ).To (BeNil (), "Teardown Driver and Client failed with error" )
203+ }()
204+
205+ p , z , _ := testContext .Instance .GetIdentity ()
206+ client := testContext .Client
207+
208+ // Create Disk
209+ volName := testNamePrefix + string (uuid .NewUUID ())
210+ volId , err := client .CreateVolume (volName , nil , defaultSizeGb , nil )
211+ Expect (err ).To (BeNil (), "CreateVolume failed with error: %v" , err )
212+
213+ // Validate Disk Created
214+ cloudDisk , err := computeService .Disks .Get (p , z , volName ).Do ()
215+ Expect (err ).To (BeNil (), "Could not get disk from cloud directly" )
216+ Expect (cloudDisk .Type ).To (ContainSubstring (standardDiskType ))
217+ Expect (cloudDisk .Status ).To (Equal (readyState ))
218+ Expect (cloudDisk .SizeGb ).To (Equal (defaultSizeGb ))
219+ Expect (cloudDisk .Name ).To (Equal (volName ))
220+
221+ // Create Snapshot
222+ snapshotName := testNamePrefix + string (uuid .NewUUID ())
223+ snapshotId , err := client .CreateSnapshot (snapshotName , volId , nil )
224+ Expect (err ).To (BeNil (), "CreateSnapshot failed with error: %v" , err )
225+
226+ // Validate Snapshot Created
227+ snapshot , err := computeService .Snapshots .Get (p , snapshotName ).Do ()
228+ Expect (err ).To (BeNil (), "Could not get snapshot from cloud directly" )
229+ Expect (snapshot .Name ).To (Equal (snapshotName ))
230+
231+ err = wait .Poll (10 * time .Second , 3 * time .Minute , func () (bool , error ) {
232+ snapshot , err := computeService .Snapshots .Get (p , snapshotName ).Do ()
233+ Expect (err ).To (BeNil (), "Could not get snapshot from cloud directly" )
234+ if snapshot .Status == "READY" {
235+ return true , nil
236+ }
237+ return false , nil
238+ })
239+ Expect (err ).To (BeNil (), "Could not wait for snapshot be ready" )
240+
241+ defer func () {
242+ // Delete Disk
243+ err := client .DeleteVolume (volId )
244+ Expect (err ).To (BeNil (), "DeleteVolume failed" )
245+
246+ // Validate Disk Deleted
247+ _ , err = computeService .Disks .Get (p , z , volName ).Do ()
248+ Expect (gce .IsGCEError (err , "notFound" )).To (BeTrue (), "Expected disk to not be found" )
249+
250+ // Delete Snapshot
251+ err = client .DeleteSnapshot (snapshotId )
252+ Expect (err ).To (BeNil (), "DeleteSnapshot failed" )
253+
254+ // Validate Snapshot Deleted
255+ _ , err = computeService .Snapshots .Get (p , snapshotName ).Do ()
256+ Expect (gce .IsGCEError (err , "notFound" )).To (BeTrue (), "Expected snapshot to not be found" )
257+ }()
258+ })
259+
260+ It ("Should create and delete snapshot for RePD in two zones " , func () {
261+ // Create new driver and client
262+ Expect (testInstances ).NotTo (BeEmpty ())
263+ testContext , err := testutils .GCEClientAndDriverSetup (testInstances [0 ])
264+ Expect (err ).To (BeNil (), "Failed to set up new driver and client" )
265+ defer func () {
266+ err := remote .TeardownDriverAndClient (testContext )
267+ Expect (err ).To (BeNil (), "Teardown Driver and Client failed with error" )
268+ }()
269+
270+ controllerInstance := testContext .Instance
271+ controllerClient := testContext .Client
272+
273+ p , z , _ := controllerInstance .GetIdentity ()
274+
275+ region , err := common .GetRegionFromZones ([]string {z })
276+ Expect (err ).To (BeNil (), "Failed to get region from zones" )
277+
278+ // Create Disk
279+ volName := testNamePrefix + string (uuid .NewUUID ())
280+ volId , err := controllerClient .CreateVolume (volName , map [string ]string {
281+ common .ParameterKeyReplicationType : "regional-pd" ,
282+ }, defaultSizeGb , nil )
283+ Expect (err ).To (BeNil (), "CreateVolume failed with error: %v" , err )
284+
285+ // Validate Disk Created
286+ cloudDisk , err := betaComputeService .RegionDisks .Get (p , region , volName ).Do ()
287+ Expect (err ).To (BeNil (), "Could not get disk from cloud directly" )
288+ Expect (cloudDisk .Type ).To (ContainSubstring (standardDiskType ))
289+ Expect (cloudDisk .Status ).To (Equal (readyState ))
290+ Expect (cloudDisk .SizeGb ).To (Equal (defaultSizeGb ))
291+ Expect (cloudDisk .Name ).To (Equal (volName ))
292+ Expect (len (cloudDisk .ReplicaZones )).To (Equal (2 ))
293+ for _ , replicaZone := range cloudDisk .ReplicaZones {
294+ tokens := strings .Split (replicaZone , "/" )
295+ actualZone := tokens [len (tokens )- 1 ]
296+ gotRegion , err := common .GetRegionFromZones ([]string {actualZone })
297+ Expect (err ).To (BeNil (), "failed to get region from actual zone %v" , actualZone )
298+ Expect (gotRegion ).To (Equal (region ), "Got region from replica zone that did not match supplied region" )
299+ }
300+
301+ // Create Snapshot
302+ snapshotName := testNamePrefix + string (uuid .NewUUID ())
303+ snapshotId , err := controllerClient .CreateSnapshot (snapshotName , volId , nil )
304+ Expect (err ).To (BeNil (), "CreateSnapshot failed with error: %v" , err )
305+
306+ // Validate Snapshot Created
307+ snapshot , err := computeService .Snapshots .Get (p , snapshotName ).Do ()
308+ Expect (err ).To (BeNil (), "Could not get snapshot from cloud directly" )
309+ Expect (snapshot .Name ).To (Equal (snapshotName ))
310+
311+ err = wait .Poll (10 * time .Second , 3 * time .Minute , func () (bool , error ) {
312+ snapshot , err := computeService .Snapshots .Get (p , snapshotName ).Do ()
313+ Expect (err ).To (BeNil (), "Could not get snapshot from cloud directly" )
314+ if snapshot .Status == "READY" {
315+ return true , nil
316+ }
317+ return false , nil
318+ })
319+ Expect (err ).To (BeNil (), "Could not wait for snapshot be ready" )
320+
321+ defer func () {
322+ // Delete Disk
323+ err := controllerClient .DeleteVolume (volId )
324+ Expect (err ).To (BeNil (), "DeleteVolume failed" )
325+
326+ // Validate Disk Deleted
327+ _ , err = betaComputeService .RegionDisks .Get (p , region , volName ).Do ()
328+ Expect (gce .IsGCEError (err , "notFound" )).To (BeTrue (), "Expected disk to not be found" )
329+
330+ // Delete Snapshot
331+ err = controllerClient .DeleteSnapshot (snapshotId )
332+ Expect (err ).To (BeNil (), "DeleteSnapshot failed" )
333+
334+ // Validate Snapshot Deleted
335+ _ , err = computeService .Snapshots .Get (p , snapshotName ).Do ()
336+ Expect (gce .IsGCEError (err , "notFound" )).To (BeTrue (), "Expected snapshot to not be found" )
337+ }()
338+ })
192339})
193340
194341func Logf (format string , args ... interface {}) {
0 commit comments