@@ -1555,3 +1555,111 @@ func TestVolumeOperationConcurrency(t *testing.T) {
1555
1555
t .Errorf ("Unexpected error: %v" , err )
1556
1556
}
1557
1557
}
1558
+
1559
+ func TestCreateVolumeDiskReady (t * testing.T ) {
1560
+ // Define test cases
1561
+ testCases := []struct {
1562
+ name string
1563
+ diskStatus string
1564
+ req * csi.CreateVolumeRequest
1565
+ expVol * csi.Volume
1566
+ expErrCode codes.Code
1567
+ }{
1568
+ {
1569
+ name : "disk status RESTORING" ,
1570
+ diskStatus : "RESTORING" ,
1571
+ req : & csi.CreateVolumeRequest {
1572
+ Name : "test-name" ,
1573
+ CapacityRange : stdCapRange ,
1574
+ VolumeCapabilities : stdVolCaps ,
1575
+ Parameters : stdParams ,
1576
+ },
1577
+ expErrCode : codes .Internal ,
1578
+ },
1579
+ {
1580
+ name : "disk status CREATING" ,
1581
+ diskStatus : "CREATING" ,
1582
+ req : & csi.CreateVolumeRequest {
1583
+ Name : "test-name" ,
1584
+ CapacityRange : stdCapRange ,
1585
+ VolumeCapabilities : stdVolCaps ,
1586
+ Parameters : stdParams ,
1587
+ },
1588
+ expErrCode : codes .Internal ,
1589
+ },
1590
+ {
1591
+ name : "disk status DELETING" ,
1592
+ diskStatus : "DELETING" ,
1593
+ req : & csi.CreateVolumeRequest {
1594
+ Name : "test-name" ,
1595
+ CapacityRange : stdCapRange ,
1596
+ VolumeCapabilities : stdVolCaps ,
1597
+ Parameters : stdParams ,
1598
+ },
1599
+ expErrCode : codes .Internal ,
1600
+ },
1601
+ {
1602
+ name : "disk status FAILED" ,
1603
+ diskStatus : "FAILED" ,
1604
+ req : & csi.CreateVolumeRequest {
1605
+ Name : "test-name" ,
1606
+ CapacityRange : stdCapRange ,
1607
+ VolumeCapabilities : stdVolCaps ,
1608
+ Parameters : stdParams ,
1609
+ },
1610
+ expErrCode : codes .Internal ,
1611
+ },
1612
+ {
1613
+ name : "success default" ,
1614
+ diskStatus : "READY" ,
1615
+ req : & csi.CreateVolumeRequest {
1616
+ Name : "test-name" ,
1617
+ CapacityRange : stdCapRange ,
1618
+ VolumeCapabilities : stdVolCaps ,
1619
+ Parameters : stdParams ,
1620
+ },
1621
+ expVol : & csi.Volume {
1622
+ CapacityBytes : common .GbToBytes (20 ),
1623
+ VolumeId : testVolumeID ,
1624
+ VolumeContext : nil ,
1625
+ AccessibleTopology : stdTopology ,
1626
+ },
1627
+ },
1628
+ }
1629
+
1630
+ // Run test cases
1631
+ for _ , tc := range testCases {
1632
+ t .Run (tc .name , func (t * testing.T ) {
1633
+ fcp , err := gce .CreateFakeCloudProvider (project , zone , nil )
1634
+ if err != nil {
1635
+ t .Fatalf ("Failed to create fake cloud provider: %v" , err )
1636
+ }
1637
+
1638
+ // Setup hook to create new disks with given status.
1639
+ fcp .UpdateDiskStatus (tc .diskStatus )
1640
+ // Setup new driver each time so no interference
1641
+ gceDriver := initGCEDriverWithCloudProvider (t , fcp )
1642
+ // Start Test
1643
+ resp , err := gceDriver .cs .CreateVolume (context .Background (), tc .req )
1644
+ //check response
1645
+ if err != nil {
1646
+ serverError , ok := status .FromError (err )
1647
+ if ! ok {
1648
+ t .Fatalf ("Could not get error status code from err: %v" , serverError )
1649
+ }
1650
+ if serverError .Code () != tc .expErrCode {
1651
+ t .Fatalf ("Expected error code: %v, got: %v. err : %v" , tc .expErrCode , serverError .Code (), err )
1652
+ }
1653
+ return
1654
+ }
1655
+ if tc .expErrCode != codes .OK {
1656
+ t .Fatalf ("Expected error: %v, got no error" , tc .expErrCode )
1657
+ }
1658
+
1659
+ vol := resp .GetVolume ()
1660
+ if ! reflect .DeepEqual (vol , tc .expVol ) {
1661
+ t .Fatalf ("Mismatch in expected vol %v, current volume: %v\n " , tc .expVol , vol )
1662
+ }
1663
+ })
1664
+ }
1665
+ }
0 commit comments