Skip to content

Commit 86eacce

Browse files
authored
Handling NotFoundError in LunExists
1 parent c27a286 commit 86eacce

File tree

4 files changed

+95
-0
lines changed

4 files changed

+95
-0
lines changed

storage_drivers/ontap/api/abstraction_rest.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2346,6 +2346,11 @@ func (d OntapAPIREST) LunExists(ctx context.Context, name string) (bool, error)
23462346

23472347
lunResponse, err := d.api.LunGetByName(ctx, name, make([]string, 0))
23482348
if err != nil {
2349+
if errors.IsNotFoundError(err) {
2350+
Logc(ctx).WithField("LUN", name).Debug("LUN does not exist.")
2351+
return false, nil
2352+
}
2353+
23492354
return false, err
23502355
}
23512356

storage_drivers/ontap/api/abstraction_rest_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3846,6 +3846,49 @@ func TestLunGetByName(t *testing.T) {
38463846
assert.Error(t, err, "no error returned while getting a LUN by name")
38473847
}
38483848

3849+
func TestOntapAPIREST_LunExists(t *testing.T) {
3850+
ctrl := gomock.NewController(t)
3851+
defer ctrl.Finish()
3852+
3853+
mockAPI := mockapi.NewMockRestClientInterface(ctrl)
3854+
oapi, err := api.NewOntapAPIRESTFromRestClientInterface(mockAPI)
3855+
assert.NoError(t, err)
3856+
3857+
ctx := context.Background()
3858+
lunPath := "/vol/vol1/lun0"
3859+
3860+
// Mock ClientConfig to avoid unexpected call
3861+
clientConfig := api.ClientConfig{
3862+
DebugTraceFlags: map[string]bool{"method": true},
3863+
}
3864+
mockAPI.EXPECT().ClientConfig().Return(clientConfig).AnyTimes()
3865+
3866+
// Case 1: LUN exists
3867+
mockAPI.EXPECT().LunGetByName(ctx, lunPath, gomock.Any()).Return(&models.Lun{}, nil).Times(1)
3868+
exists, err := oapi.LunExists(ctx, lunPath)
3869+
assert.NoError(t, err, "expected no error when LUN exists")
3870+
assert.True(t, exists, "expected LUN to exist")
3871+
3872+
// Case 2: LUN does not exist
3873+
mockAPI.EXPECT().LunGetByName(ctx, lunPath, gomock.Any()).Return(nil, nil).Times(1)
3874+
exists, err = oapi.LunExists(ctx, lunPath)
3875+
assert.NoError(t, err, "expected no error when LUN does not exist")
3876+
assert.False(t, exists, "expected LUN to not exist")
3877+
3878+
// Case 3: NotFoundError from REST client
3879+
mockAPI.EXPECT().LunGetByName(ctx, lunPath, gomock.Any()).Return(
3880+
nil, errors.NotFoundError("LUN not found")).Times(1)
3881+
exists, err = oapi.LunExists(ctx, lunPath)
3882+
assert.NoError(t, err, "expected no error when NotFoundError is returned")
3883+
assert.False(t, exists, "expected LUN to not exist")
3884+
3885+
// Case 4: Unexpected error
3886+
mockAPI.EXPECT().LunGetByName(ctx, lunPath, gomock.Any()).Return(nil, fmt.Errorf("unexpected error")).Times(1)
3887+
exists, err = oapi.LunExists(ctx, lunPath)
3888+
assert.Error(t, err, "expected error when unexpected error is returned")
3889+
assert.False(t, exists, "expected LUN to not exist")
3890+
}
3891+
38493892
func TestLunRename(t *testing.T) {
38503893
oapi, rsi := newMockOntapAPIREST(t)
38513894

storage_drivers/ontap/api/abstraction_zapi.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,11 @@ func (d OntapAPIZAPI) LunExists(ctx context.Context, name string) (bool, error)
627627

628628
lunResponse, err := d.api.LunGet(name)
629629
if err != nil {
630+
if errors.IsNotFoundError(err) {
631+
Logc(ctx).WithField("LUN", name).Debug("LUN does not exist.")
632+
return false, nil
633+
}
634+
630635
return false, err
631636
}
632637

storage_drivers/ontap/api/abstraction_zapi_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/netapp/trident/pkg/convert"
1414
"github.com/netapp/trident/storage_drivers/ontap/api"
1515
"github.com/netapp/trident/storage_drivers/ontap/api/azgo"
16+
"github.com/netapp/trident/utils/errors"
1617
)
1718

1819
func TestOntapAPIZAPI_LunGetFSType(t *testing.T) {
@@ -292,6 +293,47 @@ func TestExportRuleList_Zapi_NilPayload(t *testing.T) {
292293
assert.Empty(t, rules)
293294
}
294295

296+
func TestOntapAPIZAPI_LunExists(t *testing.T) {
297+
ctrl := gomock.NewController(t)
298+
defer ctrl.Finish()
299+
300+
mock := mockapi.NewMockZapiClientInterface(ctrl)
301+
oapi, err := api.NewOntapAPIZAPIFromZapiClientInterface(mock)
302+
assert.NoError(t, err)
303+
304+
tempLunPath := "/vol/vol1/lun0"
305+
306+
// Mock ClientConfig to avoid unexpected call
307+
clientConfig := api.ClientConfig{
308+
DebugTraceFlags: map[string]bool{"method": true},
309+
}
310+
mock.EXPECT().ClientConfig().Return(clientConfig).AnyTimes()
311+
312+
// Case 1: LUN exists
313+
mock.EXPECT().LunGet(tempLunPath).Return(&azgo.LunInfoType{}, nil).Times(1)
314+
exists, err := oapi.LunExists(ctx, tempLunPath)
315+
assert.NoError(t, err, "expected no error when LUN exists")
316+
assert.True(t, exists, "expected LUN to exist")
317+
318+
// Case 2: LUN does not exist
319+
mock.EXPECT().LunGet(tempLunPath).Return(nil, nil).Times(1)
320+
exists, err = oapi.LunExists(ctx, tempLunPath)
321+
assert.NoError(t, err, "expected no error when LUN does not exist")
322+
assert.False(t, exists, "expected LUN to not exist")
323+
324+
// Case 3: NotFoundError from ZAPI client
325+
mock.EXPECT().LunGet(tempLunPath).Return(nil, errors.NotFoundError("LUN not found")).Times(1)
326+
exists, err = oapi.LunExists(ctx, tempLunPath)
327+
assert.NoError(t, err, "expected no error when NotFoundError occurs")
328+
assert.False(t, exists, "expected LUN to not exist")
329+
330+
// Case 4: Error from ZAPI client
331+
mock.EXPECT().LunGet(tempLunPath).Return(nil, fmt.Errorf("error fetching LUN")).Times(1)
332+
exists, err = oapi.LunExists(ctx, tempLunPath)
333+
assert.Error(t, err, "expected error when ZAPI client returns an error")
334+
assert.False(t, exists, "expected LUN to not exist")
335+
}
336+
295337
func intPtr(i int) *int {
296338
return &i
297339
}

0 commit comments

Comments
 (0)