Skip to content

Commit

Permalink
Increased the code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
AnikaAgiwal2711 committed Jan 18, 2024
1 parent 2489256 commit c302c03
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 64 deletions.
42 changes: 28 additions & 14 deletions inttests/snapshot_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,11 @@ func TestCreateModifyDeleteSnapshotPolicy(t *testing.T) {
spName := fmt.Sprintf("%s-%s", testPrefix, "SnapPolicy")

snap := &types.SnapshotPolicyCreateParam{
Name: spName,
Name: spName,
AutoSnapshotCreationCadenceInMin: "5",
NumOfRetainedSnapshotsPerLevel: []string{"1"},
SnapshotAccessMode: "ReadOnly",
Paused: "true",

NumOfRetainedSnapshotsPerLevel: []string{"1"},
SnapshotAccessMode: "ReadOnly",
Paused: "true",
}

// create the snapshot policy
Expand All @@ -45,45 +44,60 @@ func TestCreateModifyDeleteSnapshotPolicy(t *testing.T) {
_, err2 := system.CreateSnapshotPolicy(snap)
assert.NotNil(t, err2)


// modify snapshot policy name
err = system.RenameSnapshotPolicy(snapID, "SnapshotPolicyRenamed")
assert.Nil(t, err)

// modify other parameters of snapshot policy
snapModify := &types.SnapshotPolicyModifyParam{
AutoSnapshotCreationCadenceInMin: "6",
NumOfRetainedSnapshotsPerLevel: []string{"2","6"},

NumOfRetainedSnapshotsPerLevel: []string{"2", "6"},
}
err = system.ModifySnapshotPolicy(snapModify,snapID)
err = system.ModifySnapshotPolicy(snapModify, snapID)
assert.Nil(t, err)

volID, err := createVolume(t, "")
assignVolume := &types.AssignVolumeToSnapshotPolicyParam{
SourceVolumeId: volID,

}

// Assign and unassign volume to Snapshot Policy
err = system.AssignVolumeToSnapshotPolicy(assignVolume,snapID)
err = system.AssignVolumeToSnapshotPolicy(assignVolume, snapID)
assert.Nil(t, err)

assignVolume = &types.AssignVolumeToSnapshotPolicyParam{
SourceVolumeId: "Invalid",
}
err = system.AssignVolumeToSnapshotPolicy(assignVolume, snapID)
assert.NotNil(t, err)

unassignVolume := &types.AssignVolumeToSnapshotPolicyParam{
SourceVolumeId: volID,
SourceVolumeId: volID,
AutoSnapshotRemovalAction: "Remove",

}
err = system.UnassignVolumeFromSnapshotPolicy(unassignVolume,snapID)
err = system.UnassignVolumeFromSnapshotPolicy(unassignVolume, snapID)
assert.Nil(t, err)

unassignVolume = &types.AssignVolumeToSnapshotPolicyParam{
SourceVolumeId: volID,
AutoSnapshotRemovalAction: "Invalid",
}
err = system.UnassignVolumeFromSnapshotPolicy(unassignVolume, snapID)
assert.NotNil(t, err)

// Resume and Pause the SnapshotPolicy
err = system.ResumeSnapshotPolicy(snapID)
assert.Nil(t, err)

err = system.ResumeSnapshotPolicy("Invalid")
assert.NotNil(t, err)

err = system.PauseSnapshotPolicy(snapID)
assert.Nil(t, err)

err = system.PauseSnapshotPolicy("Invalid")
assert.NotNil(t, err)

// delete the snapshot policy
err = system.RemoveSnapshotPolicy(snapID)
assert.Nil(t, err)
Expand Down
20 changes: 10 additions & 10 deletions snapshot_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (system *System) CreateSnapshotPolicy(snapPolicy *types.SnapshotPolicyCreat

path := fmt.Sprintf("/api/types/SnapshotPolicy/instances")
snapResp := types.SnapShotPolicyCreateResp{}
err := system.client.getJSONWithRetry(
err := system.client.getJSONWithRetry(
http.MethodPost, path, snapPolicy, &snapResp)
if err != nil {
return "", err
Expand All @@ -38,21 +38,21 @@ func (system *System) CreateSnapshotPolicy(snapPolicy *types.SnapshotPolicyCreat
func (system *System) RemoveSnapshotPolicy(id string) error {
path := fmt.Sprintf("/api/instances/SnapshotPolicy::%v/action/removeSnapshotPolicy", id)
removeParam := &types.EmptyPayload{}
err := system.client.getJSONWithRetry(
err := system.client.getJSONWithRetry(
http.MethodPost, path, removeParam, nil)
if err != nil {
return err
}
return nil
}

// RenameSnapshotPolicy renames a snapshot policy
// RenameSnapshotPolicy renames a snapshot policy
func (system *System) RenameSnapshotPolicy(id, name string) error {
path := fmt.Sprintf("/api/instances/SnapshotPolicy::%v/action/renameSnapshotPolicy", id)
renameSnap := &types.SnapshotPolicyRenameParam{
NewName: name,
}
err := system.client.getJSONWithRetry(
err := system.client.getJSONWithRetry(
http.MethodPost, path, renameSnap, nil)
if err != nil {
return err
Expand All @@ -63,7 +63,7 @@ func (system *System) RenameSnapshotPolicy(id, name string) error {
// ModifySnapshotPolicy modifies a snapshot policy
func (system *System) ModifySnapshotPolicy(modifysnapPolicy *types.SnapshotPolicyModifyParam, id string) error {
path := fmt.Sprintf("/api/instances/SnapshotPolicy::%v/action/modifySnapshotPolicy", id)
err := system.client.getJSONWithRetry(
err := system.client.getJSONWithRetry(
http.MethodPost, path, modifysnapPolicy, nil)
if err != nil {
return err
Expand All @@ -74,7 +74,7 @@ func (system *System) ModifySnapshotPolicy(modifysnapPolicy *types.SnapshotPolic
// AssignVolumeToSnapshotPolicy assigns volume to a snapshot policy
func (system *System) AssignVolumeToSnapshotPolicy(assignVoltoSnap *types.AssignVolumeToSnapshotPolicyParam, id string) error {
path := fmt.Sprintf("/api/instances/SnapshotPolicy::%v/action/addSourceVolumeToSnapshotPolicy", id)
err := system.client.getJSONWithRetry(
err := system.client.getJSONWithRetry(
http.MethodPost, path, assignVoltoSnap, nil)
if err != nil {
return err
Expand All @@ -85,7 +85,7 @@ func (system *System) AssignVolumeToSnapshotPolicy(assignVoltoSnap *types.Assign
// UnassignVolumeFromSnapshotPolicy unassigns volume from a snapshot policy
func (system *System) UnassignVolumeFromSnapshotPolicy(UnassignVolFromSnap *types.AssignVolumeToSnapshotPolicyParam, id string) error {
path := fmt.Sprintf("/api/instances/SnapshotPolicy::%v/action/removeSourceVolumeFromSnapshotPolicy", id)
err := system.client.getJSONWithRetry(
err := system.client.getJSONWithRetry(
http.MethodPost, path, UnassignVolFromSnap, nil)
if err != nil {
return err
Expand All @@ -97,7 +97,7 @@ func (system *System) UnassignVolumeFromSnapshotPolicy(UnassignVolFromSnap *type
func (system *System) PauseSnapshotPolicy(id string) error {
path := fmt.Sprintf("/api/instances/SnapshotPolicy::%v/action/pauseSnapshotPolicy", id)
pauseParam := &types.EmptyPayload{}
err := system.client.getJSONWithRetry(
err := system.client.getJSONWithRetry(
http.MethodPost, path, pauseParam, nil)
if err != nil {
return err
Expand All @@ -109,10 +109,10 @@ func (system *System) PauseSnapshotPolicy(id string) error {
func (system *System) ResumeSnapshotPolicy(id string) error {
path := fmt.Sprintf("/api/instances/SnapshotPolicy::%v/action/resumeSnapshotPolicy", id)
resumeParam := &types.EmptyPayload{}
err := system.client.getJSONWithRetry(
err := system.client.getJSONWithRetry(
http.MethodPost, path, resumeParam, nil)
if err != nil {
return err
}
return nil
}
}
61 changes: 30 additions & 31 deletions snapshot_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
)

var (
ID2 string
ID2 string
errSnap error
)

Expand All @@ -35,18 +35,18 @@ func TestCreateSnapshotPolicy(t *testing.T) {
cases := []testCase{
{
fs: &types.SnapshotPolicyCreateParam{
Name: "testSnapshotPolicy",
Name: "testSnapshotPolicy",
AutoSnapshotCreationCadenceInMin: "5",
NumOfRetainedSnapshotsPerLevel: []string{"1"},
NumOfRetainedSnapshotsPerLevel: []string{"1"},
},
expected: nil,
},
{
fs: &types.SnapshotPolicyCreateParam{
Name: "testSnapshotPolicy2",
Name: "testSnapshotPolicy2",
AutoSnapshotCreationCadenceInMin: "5",
NumOfRetainedSnapshotsPerLevel: []string{"1"},
SnapshotAccessMode: "Invalid",
NumOfRetainedSnapshotsPerLevel: []string{"1"},
SnapshotAccessMode: "Invalid",
},
expected: errors.New("accessMode should get one of the following values: ReadWrite, ReadOnly, but its value is Invalid."),
},
Expand Down Expand Up @@ -131,24 +131,24 @@ func TestModifySnapshotPolicyName(t *testing.T) {

func TestModifySnapshotPolicy(t *testing.T) {
type testCase struct {
id string
snap *types.SnapshotPolicyModifyParam
id string
snap *types.SnapshotPolicyModifyParam
expected error
}
cases := []testCase{
{
id: ID2,
id: ID2,
snap: &types.SnapshotPolicyModifyParam{
AutoSnapshotCreationCadenceInMin: "6",
NumOfRetainedSnapshotsPerLevel: []string{"2", "3"},
NumOfRetainedSnapshotsPerLevel: []string{"2", "3"},
},
expected: nil,
},
{
id: "Invalid",
id: "Invalid",
snap: &types.SnapshotPolicyModifyParam{
AutoSnapshotCreationCadenceInMin: "6",
NumOfRetainedSnapshotsPerLevel: []string{"2", "3"},
NumOfRetainedSnapshotsPerLevel: []string{"2", "3"},
},
expected: errors.New("id (Invalid) must be a hexadecimal number (unsigned long)."),
},
Expand Down Expand Up @@ -185,27 +185,27 @@ func TestModifySnapshotPolicy(t *testing.T) {

func TestAssignSnapshotPolicy(t *testing.T) {
type testCase struct {
id string
snap *types.AssignVolumeToSnapshotPolicyParam
id string
snap *types.AssignVolumeToSnapshotPolicyParam
expected error
}
cases := []testCase{
{
id: ID2,
id: ID2,
snap: &types.AssignVolumeToSnapshotPolicyParam{
SourceVolumeId: "edba1bff00000001",
},
expected: nil,
},
{
id: "Invalid",
id: "Invalid",
snap: &types.AssignVolumeToSnapshotPolicyParam{
SourceVolumeId: "edba1bff00000001",
},
expected: errors.New("id (Invalid) must be a hexadecimal number (unsigned long)."),
},
{
id: ID2,
id: ID2,
snap: &types.AssignVolumeToSnapshotPolicyParam{
SourceVolumeId: "edba1bff000000",
},
Expand Down Expand Up @@ -244,39 +244,39 @@ func TestAssignSnapshotPolicy(t *testing.T) {

func TestUnassignSnapshotPolicy(t *testing.T) {
type testCase struct {
id string
snap *types.AssignVolumeToSnapshotPolicyParam
id string
snap *types.AssignVolumeToSnapshotPolicyParam
expected error
}
cases := []testCase{
{
id: ID2,
id: ID2,
snap: &types.AssignVolumeToSnapshotPolicyParam{
SourceVolumeId: "edba1bff00000001",
SourceVolumeId: "edba1bff00000001",
AutoSnapshotRemovalAction: "Remove",
},
expected: nil,
},
{
id: "Invalid",
id: "Invalid",
snap: &types.AssignVolumeToSnapshotPolicyParam{
SourceVolumeId: "edba1bff00000001",
SourceVolumeId: "edba1bff00000001",
AutoSnapshotRemovalAction: "Remove",
},
expected: errors.New("id (Invalid) must be a hexadecimal number (unsigned long)."),
},
{
id: ID2,
id: ID2,
snap: &types.AssignVolumeToSnapshotPolicyParam{
SourceVolumeId: "edba1bff000000",
SourceVolumeId: "edba1bff000000",
AutoSnapshotRemovalAction: "Remove",
},
expected: errors.New("Invalid volume. Please try again with a valid ID or name."),
},
{
id: ID2,
id: ID2,
snap: &types.AssignVolumeToSnapshotPolicyParam{
SourceVolumeId: "edba1bff000000",
SourceVolumeId: "edba1bff000000",
AutoSnapshotRemovalAction: "Invalid",
},
expected: errors.New("autoSnapshotRemovalAction should get one of the following values: Remove, Detach, but its value is Invalid."),
Expand Down Expand Up @@ -314,7 +314,7 @@ func TestUnassignSnapshotPolicy(t *testing.T) {

func TestPauseSnapshotPolicy(t *testing.T) {
type testCase struct {
id string
id string
expected error
}
cases := []testCase{
Expand Down Expand Up @@ -357,10 +357,9 @@ func TestPauseSnapshotPolicy(t *testing.T) {
}
}


func TestResumeSnapshotPolicy(t *testing.T) {
type testCase struct {
id string
id string
expected error
}
cases := []testCase{
Expand Down Expand Up @@ -405,7 +404,7 @@ func TestResumeSnapshotPolicy(t *testing.T) {

func TestDeleteSnapshotPolicy(t *testing.T) {
type testCase struct {
id string
id string
expected error
}
cases := []testCase{
Expand Down
18 changes: 9 additions & 9 deletions types/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -1086,12 +1086,12 @@ type SnapshotPolicy struct {

// SnapshotPolicyCreate defines the struct for creating a Snapshot Policy
type SnapshotPolicyCreateParam struct {
AutoSnapshotCreationCadenceInMin string `json:"autoSnapshotCreationCadenceInMin"`
NumOfRetainedSnapshotsPerLevel []string `json:"numOfRetainedSnapshotsPerLevel"`
SnapshotAccessMode string `json:"snapshotAccessMode,omitempty"`
AutoSnapshotCreationCadenceInMin string `json:"autoSnapshotCreationCadenceInMin"`
NumOfRetainedSnapshotsPerLevel []string `json:"numOfRetainedSnapshotsPerLevel"`
SnapshotAccessMode string `json:"snapshotAccessMode,omitempty"`
SecureSnapshots string `json:"secureSnapshots,omitempty"`
Name string `json:"name"`
Paused string `json:"paused,omitempty"`
Name string `json:"name"`
Paused string `json:"paused,omitempty"`
}

// SnapShotPolicyCreateResp defines struct for the response when snapshot policy is created successfully
Expand All @@ -1101,18 +1101,18 @@ type SnapShotPolicyCreateResp struct {

// SnapshotPolicyRenameParam defines the struct for renaming a Snapshot Policy
type SnapshotPolicyRenameParam struct {
NewName string `json:"newName"`
NewName string `json:"newName"`
}

// SnapshotPolicyModifyParam defines the struct for modifying a Snapshot Policy
type SnapshotPolicyModifyParam struct {
AutoSnapshotCreationCadenceInMin string `json:"autoSnapshotCreationCadenceInMin"`
NumOfRetainedSnapshotsPerLevel []string `json:"numOfRetainedSnapshotsPerLevel"`
AutoSnapshotCreationCadenceInMin string `json:"autoSnapshotCreationCadenceInMin"`
NumOfRetainedSnapshotsPerLevel []string `json:"numOfRetainedSnapshotsPerLevel"`
}

// AssignVolumeToSnapshotPolicyParam defines the struct for assigning volume to a Snapshot Policy
type AssignVolumeToSnapshotPolicyParam struct {
SourceVolumeId string `json:"sourceVolumeId"`
SourceVolumeId string `json:"sourceVolumeId"`
AutoSnapshotRemovalAction string `json:"autoSnapshotRemovalAction,omitempty"`
}

Expand Down

0 comments on commit c302c03

Please sign in to comment.