@@ -6898,6 +6898,23 @@ func TestCloneFlexvol(t *testing.T) {
68986898 split : false ,
68996899 expectError : true ,
69006900 },
6901+ "Snapshot cleanup on vol create error" : {
6902+ configureOntapAPI : func (mockAPI * mockapi.MockOntapAPI ) {
6903+ mockAPI .EXPECT ().VolumeExists (ctx , internalName ).Return (false , nil )
6904+ mockAPI .EXPECT ().VolumeSnapshotCreate (ctx , gomock .Any (), "fakeSource" ).Return (nil )
6905+ mockAPI .EXPECT ().VolumeCloneCreate (
6906+ ctx , internalName , cloneSourceVolumeInternal , gomock .Any (), false ,
6907+ ).Return (fmt .Errorf ("error creating clone" ))
6908+ mockAPI .EXPECT ().VolumeSnapshotDelete (ctx , gomock .Any (), "fakeSource" ).Return (nil )
6909+ },
6910+ cloneVolumeConfig : storage.VolumeConfig {
6911+ InternalName : internalName ,
6912+ CloneSourceVolumeInternal : cloneSourceVolumeInternal ,
6913+ },
6914+ storageDriverConfig : storageDriverConfig ,
6915+ split : false ,
6916+ expectError : true ,
6917+ },
69016918 "No specific snapshot was requested" : {
69026919 configureOntapAPI : func (mockAPI * mockapi.MockOntapAPI ) {
69036920 mockAPI .EXPECT ().VolumeExists (ctx , internalName ).Return (false , nil )
@@ -6933,6 +6950,7 @@ func TestCloneFlexvol(t *testing.T) {
69336950 ).Return (nil )
69346951 mockAPI .EXPECT ().VolumeWaitForStates (ctx , internalName , gomock .Any (), gomock .Any (),
69356952 maxFlexvolCloneWait ).Return ("" , errors .New ("error waiting for NVMe clone" ))
6953+ mockAPI .EXPECT ().VolumeDestroy (ctx , "dummy" , true , true )
69366954 },
69376955 cloneVolumeConfig : cloneVolumeConfig ,
69386956 storageDriverConfig : storageDriverConfigNVMe ,
@@ -6948,6 +6966,7 @@ func TestCloneFlexvol(t *testing.T) {
69486966 mockAPI .EXPECT ().VolumeSetComment (ctx , internalName , internalName , label ).Return (errors .New ("error creating clone" ))
69496967 mockAPI .EXPECT ().VolumeWaitForStates (ctx , internalName , gomock .Any (), gomock .Any (),
69506968 maxFlexvolCloneWait ).Return ("online" , nil )
6969+ mockAPI .EXPECT ().VolumeDestroy (ctx , "dummy" , true , true )
69516970 },
69526971 cloneVolumeConfig : cloneVolumeConfig ,
69536972 storageDriverConfig : storageDriverConfig ,
@@ -6964,6 +6983,7 @@ func TestCloneFlexvol(t *testing.T) {
69646983 maxFlexvolCloneWait ).Return ("online" , nil )
69656984 mockAPI .EXPECT ().VolumeSetComment (ctx , internalName , internalName , label ).Return (nil )
69666985 mockAPI .EXPECT ().VolumeMount (ctx , internalName , "/" + internalName ).Return (errors .New ("error mounting volume" ))
6986+ mockAPI .EXPECT ().VolumeDestroy (ctx , "dummy" , true , true )
69676987 },
69686988 cloneVolumeConfig : cloneVolumeConfig ,
69696989 storageDriverConfig : storageDriverConfig ,
@@ -6983,6 +7003,7 @@ func TestCloneFlexvol(t *testing.T) {
69837003 mockAPI .EXPECT ().VolumeSetQosPolicyGroupName (
69847004 ctx , internalName , qosPolicyGroup ,
69857005 ).Return (errors .New ("error setting qos policy" ))
7006+ mockAPI .EXPECT ().VolumeDestroy (ctx , "dummy" , true , true )
69867007 },
69877008 cloneVolumeConfig : cloneVolumeConfig ,
69887009 storageDriverConfig : storageDriverConfig ,
@@ -7001,6 +7022,7 @@ func TestCloneFlexvol(t *testing.T) {
70017022 mockAPI .EXPECT ().VolumeMount (ctx , internalName , "/" + internalName ).Return (nil )
70027023 mockAPI .EXPECT ().VolumeSetQosPolicyGroupName (ctx , internalName , qosPolicyGroup ).Return (nil )
70037024 mockAPI .EXPECT ().VolumeCloneSplitStart (ctx , internalName ).Return (errors .New ("error splitting clone" ))
7025+ mockAPI .EXPECT ().VolumeDestroy (ctx , "dummy" , true , true )
70047026 },
70057027 cloneVolumeConfig : cloneVolumeConfig ,
70067028 storageDriverConfig : storageDriverConfig ,
@@ -10201,3 +10223,78 @@ func TestConstructGroupSnapshot(t *testing.T) {
1020110223 })
1020210224 }
1020310225}
10226+
10227+ func TestCleanupFailedCloneFlexVol (t * testing.T ) {
10228+ tests := map [string ]struct {
10229+ err error
10230+ expectErr bool
10231+ clonedVolName string
10232+ sourceVol string
10233+ snapshot string
10234+ setupMock func (mockAPI * mockapi.MockOntapAPI )
10235+ }{
10236+ "Clean up vol and snap" : {
10237+ err : errors .New ("error" ),
10238+ expectErr : false ,
10239+ clonedVolName : "testClone" ,
10240+ sourceVol : "testSourceVol" ,
10241+ snapshot : "testSnap" ,
10242+ setupMock : func (mockAPI * mockapi.MockOntapAPI ) {
10243+ mockAPI .EXPECT ().VolumeDestroy (ctx , "testClone" , true , true )
10244+ mockAPI .EXPECT ().VolumeSnapshotDelete (ctx , "testSnap" , "testSourceVol" )
10245+ },
10246+ },
10247+ "Clean up snapshot only" : {
10248+ err : errors .New ("error" ),
10249+ expectErr : false ,
10250+ clonedVolName : "" ,
10251+ sourceVol : "testSourceVol" ,
10252+ snapshot : "testSnap" ,
10253+ setupMock : func (mockAPI * mockapi.MockOntapAPI ) {
10254+ mockAPI .EXPECT ().VolumeSnapshotDelete (ctx , "testSnap" , "testSourceVol" )
10255+ },
10256+ },
10257+ "Clean up cloned vol only" : {
10258+ err : errors .New ("error" ),
10259+ expectErr : false ,
10260+ clonedVolName : "testClone" ,
10261+ sourceVol : "testSourceVol" ,
10262+ snapshot : "" ,
10263+ setupMock : func (mockAPI * mockapi.MockOntapAPI ) {
10264+ mockAPI .EXPECT ().VolumeDestroy (ctx , "testClone" , true , true )
10265+ },
10266+ },
10267+ "Skip cleanup no error" : {
10268+ err : nil ,
10269+ expectErr : false ,
10270+ clonedVolName : "clonedVol" ,
10271+ sourceVol : "testVol" ,
10272+ snapshot : "testSnap" ,
10273+ setupMock : func (mockAPI * mockapi.MockOntapAPI ) {
10274+ },
10275+ },
10276+ "Snapshot and vol destroy error" : {
10277+ err : errors .New ("error" ),
10278+ expectErr : false ,
10279+ clonedVolName : "clonedVol" ,
10280+ sourceVol : "testVol" ,
10281+ snapshot : "testSnap" ,
10282+ setupMock : func (mockAPI * mockapi.MockOntapAPI ) {
10283+ mockAPI .EXPECT ().VolumeDestroy (ctx , "clonedVol" , true , true ).Return (fmt .Errorf ("mock error" ))
10284+ mockAPI .EXPECT ().VolumeSnapshotDelete (ctx , "testSnap" , "testVol" ).Return (fmt .Errorf ("mock error" ))
10285+ },
10286+ },
10287+ }
10288+
10289+ for name , test := range tests {
10290+ t .Run (name , func (t * testing.T ) {
10291+ ctx := context .Background ()
10292+ mockCtrl := gomock .NewController (t )
10293+ defer mockCtrl .Finish ()
10294+ mockAPI := mockapi .NewMockOntapAPI (mockCtrl )
10295+ test .setupMock (mockAPI )
10296+
10297+ cleanupFailedCloneFlexVol (ctx , mockAPI , test .err , test .clonedVolName , test .sourceVol , test .snapshot )
10298+ })
10299+ }
10300+ }
0 commit comments