Skip to content

Commit 460cc4d

Browse files
authored
Fix zero record checks in ONTAP REST layer
1 parent d4ade71 commit 460cc4d

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

storage_drivers/ontap/api/ontap_rest.go

+6-10
Original file line numberDiff line numberDiff line change
@@ -2633,15 +2633,15 @@ func (c *RestClient) LunGet(ctx context.Context, uuid string) (*san.LunGetOK, er
26332633
// LunGetByName gets the LUN with the specified name
26342634
func (c *RestClient) LunGetByName(ctx context.Context, name string, fields []string) (*models.Lun, error) {
26352635
result, err := c.LunList(ctx, name, fields)
2636-
if err != nil || result.Payload == nil {
2636+
if err != nil {
26372637
return nil, err
26382638
}
26392639

2640-
if result == nil || result.Payload == nil || result.Payload.NumRecords == nil {
2641-
return nil, errors.NotFoundError(fmt.Sprintf("could not get LUN by name %v, error: %v", name, err))
2640+
if result == nil || result.Payload == nil || result.Payload.NumRecords == nil || *result.Payload.NumRecords == 0 {
2641+
return nil, errors.NotFoundError(fmt.Sprintf("could not get LUN by name %v", name))
26422642
}
26432643

2644-
if result.Payload.NumRecords != nil && *result.Payload.NumRecords == 1 && result.Payload.LunResponseInlineRecords != nil {
2644+
if *result.Payload.NumRecords == 1 && result.Payload.LunResponseInlineRecords != nil {
26452645
return result.Payload.LunResponseInlineRecords[0], nil
26462646
}
26472647
return nil, err
@@ -6101,14 +6101,10 @@ func (c *RestClient) JobScheduleExists(ctx context.Context, jobName string) (boo
61016101
if err != nil {
61026102
return false, err
61036103
}
6104-
if result == nil {
6104+
if result == nil || result.Payload == nil {
61056105
return false, fmt.Errorf("nil result finding job with name: %v", jobName)
61066106
}
6107-
6108-
if result.Payload.ScheduleResponseInlineRecords == nil {
6109-
return false, fmt.Errorf("could not find job with name: %v", jobName)
6110-
}
6111-
if result.Payload.NumRecords == nil {
6107+
if result.Payload.ScheduleResponseInlineRecords == nil || result.Payload.NumRecords == nil || *result.Payload.NumRecords == 0 {
61126108
return false, fmt.Errorf("could not find job with name: %v", jobName)
61136109
}
61146110
if result.Payload.NumRecords != nil && *result.Payload.NumRecords != 1 {

storage_drivers/ontap/api/ontap_rest_test.go

+34
Original file line numberDiff line numberDiff line change
@@ -1091,6 +1091,27 @@ func mockLunResponseNumRecordsNil(w http.ResponseWriter, r *http.Request) {
10911091
}
10921092
}
10931093

1094+
func mockLunResponseNumRecordsZero(w http.ResponseWriter, r *http.Request) {
1095+
numRecords := int64(0)
1096+
lunResponse := models.LunResponse{
1097+
LunResponseInlineRecords: []*models.Lun{},
1098+
NumRecords: &numRecords,
1099+
}
1100+
if r.Method == "GET" {
1101+
if r.URL.Path == "/api/protocols/san/lun-maps" {
1102+
mockLunMapResponse(w, r)
1103+
} else {
1104+
setHTTPResponseHeader(w, http.StatusOK)
1105+
if r.URL.Path == "/api/storage/luns/fake-lunName" {
1106+
// LunGetByName calls LunList
1107+
json.NewEncoder(w).Encode(lunResponse)
1108+
} else {
1109+
json.NewEncoder(w).Encode(lunResponse)
1110+
}
1111+
}
1112+
}
1113+
}
1114+
10941115
func TestOntapREST_LunGetByName(t *testing.T) {
10951116
tests := []struct {
10961117
name string
@@ -1100,6 +1121,7 @@ func TestOntapREST_LunGetByName(t *testing.T) {
11001121
}{
11011122
{"PositiveTest", mockLunResponse, false, false},
11021123
{"NumRecordsNilInResponse", mockLunResponseNumRecordsNil, true, true},
1124+
{"NumRecordsZeroInResponse", mockLunResponseNumRecordsZero, false, true},
11031125
{"BackendReturnError", mockResourceNotFound, true, true},
11041126
}
11051127
for _, test := range tests {
@@ -4259,6 +4281,17 @@ func mockJobScheduleResponseNumRecordsNil(w http.ResponseWriter, r *http.Request
42594281
json.NewEncoder(w).Encode(scheduleResponse)
42604282
}
42614283

4284+
func mockJobScheduleResponseNumRecordsZero(w http.ResponseWriter, r *http.Request) {
4285+
numRecords := int64(0)
4286+
scheduleResponse := &models.ScheduleResponse{
4287+
ScheduleResponseInlineRecords: []*models.Schedule{{}},
4288+
NumRecords: &numRecords,
4289+
}
4290+
4291+
setHTTPResponseHeader(w, http.StatusOK)
4292+
json.NewEncoder(w).Encode(scheduleResponse)
4293+
}
4294+
42624295
func mockJobScheduleResponseNumRecordsGrt1(w http.ResponseWriter, r *http.Request) {
42634296
numRecords := int64(2)
42644297
scheduleResponse := &models.ScheduleResponse{
@@ -4279,6 +4312,7 @@ func TestOntapRest_JobScheduleExists(t *testing.T) {
42794312
{"PositiveTest", mockJobScheduleResponse, false},
42804313
{"JobIsNilInResponse", mockJobScheduleResponseRecordNil, true},
42814314
{"NumRecordFieldNil", mockJobScheduleResponseNumRecordsNil, true},
4315+
{"NumRecordFieldZero", mockJobScheduleResponseNumRecordsZero, true},
42824316
{"NumRecordMoreThanOne", mockJobScheduleResponseNumRecordsGrt1, true},
42834317
{"BackendReturnError", mockResourceNotFound, true},
42844318
}

0 commit comments

Comments
 (0)