Skip to content

Commit

Permalink
Increased code coverage for pkg/controller
Browse files Browse the repository at this point in the history
  • Loading branch information
santhoshatdell committed Nov 15, 2024
1 parent fc5bf9b commit fc72b2e
Show file tree
Hide file tree
Showing 3 changed files with 281 additions and 78 deletions.
61 changes: 61 additions & 0 deletions pkg/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2136,6 +2136,21 @@ var _ = ginkgo.Describe("CSIControllerService", func() {
})
})

ginkgo.When("the array ID could not found", func() {
ginkgo.It("should return error", func() {
req := &csi.CreateSnapshotRequest{
SourceVolumeId: "39bb1b5f-5624-490d-9ece-18f7b28a904e/globalvolid123/scsi",
Name: validSnapName,
}

res, err := ctrlSvc.CreateSnapshot(context.Background(), req)

gomega.Expect(res).To(gomega.BeNil())
gomega.Expect(err).ToNot(gomega.BeNil())
gomega.Expect(err.Error()).To(gomega.ContainSubstring("failed to find array with given ID"))
})
})

ginkgo.When("snapshot name already taken", func() {
ginkgo.It("should fail [sourceVolumeId != snap.sourceVolumeId]", func() {
clientMock.On("GetVolume", mock.Anything, validBaseVolID).Return(
Expand Down Expand Up @@ -2328,6 +2343,32 @@ var _ = ginkgo.Describe("CSIControllerService", func() {
gomega.Expect(err).To(gomega.BeNil())
})
})

ginkgo.When("the request is not valid", func() {
ginkgo.It("should return error", func() {
req := &csi.DeleteSnapshotRequest{
SnapshotId: "",
}

_, err := ctrlSvc.DeleteSnapshot(context.Background(), req)

gomega.Expect(err).ToNot(gomega.BeNil())
gomega.Expect(err.Error()).To(gomega.ContainSubstring("snapshot ID to be deleted is required"))
})
})

ginkgo.When("the array ID could not found", func() {
ginkgo.It("should return error", func() {
req := &csi.DeleteSnapshotRequest{
SnapshotId: "39bb1b5f-5624-490d-9ece-18f7b28a904e/globalvolid123/scsi",
}

_, err := ctrlSvc.DeleteSnapshot(context.Background(), req)

gomega.Expect(err).ToNot(gomega.BeNil())
gomega.Expect(err.Error()).To(gomega.ContainSubstring("failed to find array with given ID"))
})
})
})

ginkgo.Describe("calling ControllerExpandVolume()", func() {
Expand Down Expand Up @@ -4369,6 +4410,26 @@ var _ = ginkgo.Describe("CSIControllerService", func() {
gomega.Expect(err.Error()).To(gomega.ContainSubstring("access mode cannot be UNKNOWN"))
})
})

ginkgo.When("resource ID is null", func() {
ginkgo.It("should fail", func() {
mount := new(csi.VolumeCapability_MountVolume)
accessType := new(csi.VolumeCapability_Mount)
accessType.Mount = mount
_, err := ctrlSvc.ValidateVolumeCapabilities(context.Background(), &csi.ValidateVolumeCapabilitiesRequest{
VolumeId: "",
VolumeContext: nil,
VolumeCapabilities: []*csi.VolumeCapability{
{
AccessMode: &csi.VolumeCapability_AccessMode{Mode: csi.VolumeCapability_AccessMode_UNKNOWN},
AccessType: accessType,
},
},
})
gomega.Expect(err).ToNot(gomega.BeNil())
gomega.Expect(err.Error()).To(gomega.ContainSubstring("No such volume"))
})
})
})

ginkgo.Describe("calling ControllerGetCapabilities()", func() {
Expand Down
196 changes: 119 additions & 77 deletions pkg/controller/csi_extension_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,19 +383,17 @@ var _ = ginkgo.Describe("csi-extension-server", func() {
})

ginkgo.Describe("calling CreateVolumeGroupSnapshot()", func() {
ginkgo.When("valid member volumes are present", func() {
ginkgo.It("should create volume group snapshot successfully", func() {
clientMock.On("GetVolumeGroupsByVolumeID", mock.Anything, validBaseVolID).
Return(gopowerstore.VolumeGroups{VolumeGroup: []gopowerstore.VolumeGroup{{ID: validGroupID, ProtectionPolicyID: validPolicyID}}}, nil)
clientMock.On("CreateVolumeGroupSnapshot", mock.Anything, validGroupID, mock.Anything).
Return(gopowerstore.CreateResponse{ID: validGroupID}, nil)
ginkgo.When("should create volume group snapshot successfully", func() {
ginkgo.It("valid member volumes are present", func() {
clientMock.On("GetVolumeGroupByName", mock.Anything, validGroupName).
Return(gopowerstore.VolumeGroup{ID: validGroupID, ProtectionPolicyID: validPolicyID}, nil)
clientMock.On("AddMembersToVolumeGroup",
mock.Anything,
mock.AnythingOfType("*gopowerstore.VolumeGroupMembers"),
validGroupID).
Return(gopowerstore.EmptyResponse(""), nil)
clientMock.On("CreateVolumeGroupSnapshot", mock.Anything, validGroupID, mock.Anything).
Return(gopowerstore.CreateResponse{ID: validGroupID}, nil)
clientMock.On("GetVolumeGroup", mock.Anything, validGroupID).
Return(gopowerstore.VolumeGroup{
ID: validGroupID,
Expand All @@ -414,35 +412,27 @@ var _ = ginkgo.Describe("csi-extension-server", func() {
gomega.Expect(err).To(gomega.BeNil())
gomega.Expect(res.SnapshotGroupID).To(gomega.Equal(validGroupID))
})
})

ginkgo.When("there is no existing volume group created", func() {
ginkgo.It("should create volume group and snapshot successfully", func() {
ginkgo.It("there is no existing volume group", func() {
clientMock.On("GetVolumeGroupByName", mock.Anything, validGroupName).
Return(gopowerstore.VolumeGroup{}, nil)
clientMock.On("GetVolumeGroupsByVolumeID", mock.Anything, validBaseVolID).
Return(gopowerstore.VolumeGroups{}, nil)
clientMock.On("GetVolumeGroupByName", mock.Anything, validGroupName).
Return(gopowerstore.VolumeGroup{ID: validGroupID, ProtectionPolicyID: validPolicyID}, nil)
createGroupRequest := &gopowerstore.VolumeGroupCreate{
Name: validGroupName,
VolumeIDs: []string{validBaseVolID},
}
clientMock.On("CreateVolumeGroup", mock.Anything, createGroupRequest).
Return(gopowerstore.CreateResponse{ID: validGroupID}, nil)
clientMock.On("CreateVolumeGroupSnapshot", mock.Anything, validGroupID, mock.Anything).
Return(gopowerstore.CreateResponse{ID: validGroupID}, nil)
clientMock.On("AddMembersToVolumeGroup",
mock.Anything,
mock.AnythingOfType("*gopowerstore.VolumeGroupMembers"),
validGroupID).
Return(gopowerstore.EmptyResponse(""), nil)
clientMock.On("GetVolumeGroup", mock.Anything, validGroupID).
Return(gopowerstore.VolumeGroup{
ID: validGroupID,
ProtectionPolicyID: validPolicyID,
Volumes: []gopowerstore.Volume{{ID: validBaseVolID, State: stateReady}},
}, nil)

createGroupRequest := &gopowerstore.VolumeGroupCreate{
Name: validGroupName,
VolumeIDs: []string{validBaseVolID},
}
clientMock.On("CreateVolumeGroup", mock.Anything, createGroupRequest).
Return(gopowerstore.CreateResponse{ID: validGroupID}, nil)

var sourceVols []string
sourceVols = append(sourceVols, validBaseVolID+"/"+firstValidID+"/scsi")
req := vgsext.CreateVolumeGroupSnapshotRequest{
Expand All @@ -456,107 +446,158 @@ var _ = ginkgo.Describe("csi-extension-server", func() {
})
})

ginkgo.When("member volumes are not present", func() {
ginkgo.It("should not create volume group snapshot successfully", func() {
clientMock.On("GetVolumeGroupsByVolumeID", mock.Anything, validBaseVolID).
Return(gopowerstore.VolumeGroups{VolumeGroup: []gopowerstore.VolumeGroup{{ID: validGroupID, ProtectionPolicyID: validPolicyID}}}, nil)
clientMock.On("CreateVolumeGroupSnapshot", mock.Anything, validGroupID, mock.Anything).
Return(gopowerstore.CreateResponse{ID: validGroupID}, nil)
ginkgo.When("should not create volume group snapshot with invalid request", func() {
ginkgo.It("volume group name is empty in the request", func() {
res, err := ctrlSvc.CreateVolumeGroupSnapshot(context.Background(), &vgsext.CreateVolumeGroupSnapshotRequest{})

gomega.Expect(err).Error()
gomega.Expect(err.Error()).To(gomega.ContainSubstring("Name to be set"))
gomega.Expect(res).To(gomega.BeNil())
})

ginkgo.It("volume group name length is greater than 27 in the request", func() {
res, err := ctrlSvc.CreateVolumeGroupSnapshot(context.Background(), &vgsext.CreateVolumeGroupSnapshotRequest{
Name: "1234561111111111111111111112",
})

gomega.Expect(err).Error()
gomega.Expect(err.Error()).To(gomega.ContainSubstring("longer than 27 character max"))
gomega.Expect(res).To(gomega.BeNil())
})

ginkgo.It("source volumes are not present in the request", func() {
res, err := ctrlSvc.CreateVolumeGroupSnapshot(context.Background(), &vgsext.CreateVolumeGroupSnapshotRequest{
Name: validGroupName,
})

gomega.Expect(err).Error()
gomega.Expect(err.Error()).To(gomega.ContainSubstring("Source volumes are not present"))
gomega.Expect(res).To(gomega.BeNil())
})
})

ginkgo.When("should not create volume group snapshot", func() {
ginkgo.It("get volume group by name fails", func() {
clientMock.On("GetVolumeGroupByName", mock.Anything, validGroupName).
Return(gopowerstore.VolumeGroup{ID: validGroupID, ProtectionPolicyID: validPolicyID}, nil)
clientMock.On("GetVolumeGroup", mock.Anything, validGroupID).
Return(gopowerstore.VolumeGroup{
ID: validGroupID,
ProtectionPolicyID: validPolicyID,
Volumes: []gopowerstore.Volume{{ID: validBaseVolID, State: stateReady}},
}, nil)
Return(gopowerstore.VolumeGroup{}, gopowerstore.NewAPIError())

var sourceVols []string
sourceVols = append(sourceVols, validBaseVolID+"/"+firstValidID+"/scsi")
req := vgsext.CreateVolumeGroupSnapshotRequest{
Name: validGroupName,
SourceVolumeIDs: sourceVols,
}
res, err := ctrlSvc.CreateVolumeGroupSnapshot(context.Background(), &req)

gomega.Expect(err).Error()
gomega.Expect(err.Error()).To(gomega.ContainSubstring("Error getting volume group by name"))
gomega.Expect(res).To(gomega.BeNil())
})

ginkgo.It("add members to volume group fails", func() {
clientMock.On("GetVolumeGroupByName", mock.Anything, validGroupName).
Return(gopowerstore.VolumeGroup{ID: validGroupID}, nil)
clientMock.On("AddMembersToVolumeGroup",
mock.Anything,
mock.AnythingOfType("*gopowerstore.VolumeGroupMembers"),
validGroupID).
Return(gopowerstore.EmptyResponse(""), nil)
Return(gopowerstore.EmptyResponse(""), gopowerstore.NewNotFoundError())

var sourceVols []string
sourceVols = append(sourceVols, validBaseVolID+"/"+firstValidID+"/scsi")
req := vgsext.CreateVolumeGroupSnapshotRequest{
Name: validGroupName,
Name: validGroupName,
SourceVolumeIDs: sourceVols,
}
res, err := ctrlSvc.CreateVolumeGroupSnapshot(context.Background(), &req)

gomega.Expect(err).Error()
gomega.Expect(err.Error()).To(gomega.ContainSubstring("Error adding volume group members"))
gomega.Expect(res).To(gomega.BeNil())
})
})

ginkgo.When("volume group name is empty", func() {
ginkgo.It("should not create volume group snapshot successfully", func() {
ginkgo.It("get volume group by ID fails", func() {
clientMock.On("GetVolumeGroupByName", mock.Anything, validGroupName).
Return(gopowerstore.VolumeGroup{}, nil)
clientMock.On("GetVolumeGroupsByVolumeID", mock.Anything, validBaseVolID).
Return(gopowerstore.VolumeGroups{VolumeGroup: []gopowerstore.VolumeGroup{{ID: validGroupID, ProtectionPolicyID: validPolicyID}}}, nil)
clientMock.On("CreateVolumeGroupSnapshot", mock.Anything, validGroupID, mock.Anything).
Return(gopowerstore.CreateResponse{ID: validGroupID}, nil)
clientMock.On("AddMembersToVolumeGroup",
mock.Anything,
mock.AnythingOfType("*gopowerstore.VolumeGroupMembers"),
validGroupID).
Return(gopowerstore.EmptyResponse(""), nil)
clientMock.On("GetVolumeGroup", mock.Anything, validGroupID).
Return(gopowerstore.VolumeGroup{
ID: validGroupID,
ProtectionPolicyID: validPolicyID,
Volumes: []gopowerstore.Volume{{ID: validBaseVolID, State: stateReady}},
}, nil)
Return(gopowerstore.VolumeGroups{}, gopowerstore.NewAPIError())

var sourceVols []string
sourceVols = append(sourceVols, validBaseVolID+"/"+firstValidID+"/scsi")
res, err := ctrlSvc.CreateVolumeGroupSnapshot(context.Background(), &vgsext.CreateVolumeGroupSnapshotRequest{})
req := vgsext.CreateVolumeGroupSnapshotRequest{
Name: validGroupName,
SourceVolumeIDs: sourceVols,
}
res, err := ctrlSvc.CreateVolumeGroupSnapshot(context.Background(), &req)

gomega.Expect(err).Error()
gomega.Expect(err.Error()).To(gomega.ContainSubstring("Error getting volume group by volume ID"))
gomega.Expect(res).To(gomega.BeNil())
})
})

ginkgo.When("volume group name length is greater than 27", func() {
ginkgo.It("should not create volume group snapshot successfully", func() {
ginkgo.It("create volume group fails", func() {
clientMock.On("GetVolumeGroupByName", mock.Anything, validGroupName).
Return(gopowerstore.VolumeGroup{}, nil)
clientMock.On("GetVolumeGroupsByVolumeID", mock.Anything, validBaseVolID).
Return(gopowerstore.VolumeGroups{}, nil)
createGroupRequest := &gopowerstore.VolumeGroupCreate{
Name: validGroupName,
VolumeIDs: []string{validBaseVolID},
}
clientMock.On("CreateVolumeGroup", mock.Anything, createGroupRequest).
Return(gopowerstore.CreateResponse{ID: validGroupID}, gopowerstore.NewNotFoundError())

var sourceVols []string
sourceVols = append(sourceVols, validBaseVolID+"/"+firstValidID+"/scsi")
req := vgsext.CreateVolumeGroupSnapshotRequest{
Name: validGroupName,
SourceVolumeIDs: sourceVols,
}
res, err := ctrlSvc.CreateVolumeGroupSnapshot(context.Background(), &req)

gomega.Expect(err).Error()
gomega.Expect(err.Error()).To(gomega.ContainSubstring("Error creating volume group"))
gomega.Expect(res).To(gomega.BeNil())
})

ginkgo.It("create volume group snapshot fails", func() {
clientMock.On("GetVolumeGroupByName", mock.Anything, validGroupName).
Return(gopowerstore.VolumeGroup{}, nil)
clientMock.On("GetVolumeGroupsByVolumeID", mock.Anything, validBaseVolID).
Return(gopowerstore.VolumeGroups{VolumeGroup: []gopowerstore.VolumeGroup{{ID: validGroupID, ProtectionPolicyID: validPolicyID}}}, nil)
clientMock.On("CreateVolumeGroupSnapshot", mock.Anything, validGroupID, mock.Anything).
Return(gopowerstore.CreateResponse{ID: validGroupID}, nil)
clientMock.On("AddMembersToVolumeGroup",
mock.Anything,
mock.AnythingOfType("*gopowerstore.VolumeGroupMembers"),
validGroupID).
Return(gopowerstore.EmptyResponse(""), nil)
clientMock.On("GetVolumeGroup", mock.Anything, validGroupID).
Return(gopowerstore.VolumeGroup{
ID: validGroupID,
ProtectionPolicyID: validPolicyID,
Volumes: []gopowerstore.Volume{{ID: validBaseVolID, State: stateReady}},
}, nil)
clientMock.On("CreateVolumeGroupSnapshot", mock.Anything, validGroupID, mock.Anything).
Return(gopowerstore.CreateResponse{}, gopowerstore.NewNotFoundError())

var sourceVols []string
sourceVols = append(sourceVols, validBaseVolID+"/"+firstValidID+"/scsi")
res, err := ctrlSvc.CreateVolumeGroupSnapshot(context.Background(), &vgsext.CreateVolumeGroupSnapshotRequest{
Name: "1234561111111111111111111112",
})
req := vgsext.CreateVolumeGroupSnapshotRequest{
Name: validGroupName,
SourceVolumeIDs: sourceVols,
}
res, err := ctrlSvc.CreateVolumeGroupSnapshot(context.Background(), &req)

gomega.Expect(err).Error()
gomega.Expect(err.Error()).To(gomega.ContainSubstring("Error creating volume group snapshot"))
gomega.Expect(res).To(gomega.BeNil())
})
})

ginkgo.When("get volume group fails", func() {
ginkgo.It("should not create volume group snapshot successfully", func() {
clientMock.On("GetVolumeGroupsByVolumeID", mock.Anything, validBaseVolID).
Return(gopowerstore.VolumeGroups{VolumeGroup: []gopowerstore.VolumeGroup{{ID: validGroupID, ProtectionPolicyID: validPolicyID}}}, nil)
clientMock.On("CreateVolumeGroupSnapshot", mock.Anything, validGroupID, mock.Anything).
Return(gopowerstore.CreateResponse{ID: validGroupID}, nil)
ginkgo.It("get volume group fails", func() {
clientMock.On("GetVolumeGroupByName", mock.Anything, validGroupName).
Return(gopowerstore.VolumeGroup{}, nil)
clientMock.On("GetVolumeGroupsByVolumeID", mock.Anything, validBaseVolID).
Return(gopowerstore.VolumeGroups{VolumeGroup: []gopowerstore.VolumeGroup{{ID: validGroupID, ProtectionPolicyID: validPolicyID}}}, nil)
clientMock.On("AddMembersToVolumeGroup",
mock.Anything,
mock.AnythingOfType("*gopowerstore.VolumeGroupMembers"),
validGroupID).
Return(gopowerstore.EmptyResponse(""), nil)
clientMock.On("CreateVolumeGroupSnapshot", mock.Anything, validGroupID, mock.Anything).
Return(gopowerstore.CreateResponse{ID: validGroupID}, nil)
clientMock.On("GetVolumeGroup", mock.Anything, validGroupID).
Return(gopowerstore.VolumeGroup{}, gopowerstore.NewNotFoundError())

Expand All @@ -569,6 +610,7 @@ var _ = ginkgo.Describe("csi-extension-server", func() {
res, err := ctrlSvc.CreateVolumeGroupSnapshot(context.Background(), &req)

gomega.Expect(err).Error()
gomega.Expect(err.Error()).To(gomega.ContainSubstring("Error getting volume group snapshot"))
gomega.Expect(res).To(gomega.BeNil())
})
})
Expand Down
Loading

0 comments on commit fc72b2e

Please sign in to comment.