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