Skip to content

Commit

Permalink
Add function to check if volume exists already (#342)
Browse files Browse the repository at this point in the history
1. Adds check functionality to check whether a volume already exist
2. Add more test cases and fixed comment for IsVolumeExist func

Signed-off-by: Ashish Ranjan <[email protected]>
  • Loading branch information
ashishranjan738 authored and prateekpandey14 committed Jun 1, 2018
1 parent b2993ac commit 0c17969
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 2 deletions.
23 changes: 21 additions & 2 deletions cmd/mayactl/app/command/volume_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/openebs/maya/pkg/client/mapiserver"
"github.com/openebs/maya/pkg/util"
mayav1 "github.com/openebs/maya/types/v1"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -75,10 +76,13 @@ func (c *CmdVolumeCreateOptions) Validate(cmd *cobra.Command) error {
return nil
}

// Run does tasks related to mayaserver.
// RunVolumeCreate makes create volume request to maya-apiserver after verifying whether the volume already exists or not. In case if the volume already exists it returns the error and come out of execution.
func (c *CmdVolumeCreateOptions) RunVolumeCreate(cmd *cobra.Command) error {
fmt.Println("Executing volume create...")

err := IsVolumeExist(c.volName)
if err != nil {
return err
}
resp := mapiserver.CreateVolume(c.volName, c.size)
if resp != nil {
return fmt.Errorf("Error: %v", resp)
Expand All @@ -88,3 +92,18 @@ func (c *CmdVolumeCreateOptions) RunVolumeCreate(cmd *cobra.Command) error {

return nil
}

// IsVolumeExist checks whether the volume already exists or not
func IsVolumeExist(volname string) error {
var vols mayav1.VolumeList
err := mapiserver.ListVolumes(&vols)
if err != nil {
return err
}
for _, items := range vols.Items {
if volname == items.ObjectMeta.Name {
return fmt.Errorf("Error: Volume %v already exist ", volname)
}
}
return nil
}
86 changes: 86 additions & 0 deletions cmd/mayactl/app/command/volume_create_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package command

import (
"fmt"
"net/http/httptest"
"os"
"testing"

utiltesting "k8s.io/client-go/util/testing"
)

func TestIsVolumeExist(t *testing.T) {
response := `{"items":[{"metadata":{"annotations":{"vsm.openebs.io/replica-status":"Pending,Running,Pending","openebs.io/jiva-replica-status":"Pending,Running,Pending","deployment.kubernetes.io/revision":"1","openebs.io/volume-monitor":"false","openebs.io/volume-type":"jiva","openebs.io/jiva-replica-count":"3","vsm.openebs.io/replica-ips":"nil,172.17.0.7,nil","openebs.io/jiva-replica-ips":"nil,172.17.0.7,nil","vsm.openebs.io/targetportals":"10.106.224.86:3260","vsm.openebs.io/cluster-ips":"10.106.224.86","openebs.io/storage-pool":"default","openebs.io/capacity":"5G","vsm.openebs.io/controller-ips":"172.17.0.3","openebs.io/jiva-controller-status":"Running","openebs.io/replica-container-status":"Running","openebs.io/jiva-iqn":"iqn.2016-09.com.openebs.jiva:test5","vsm.openebs.io/volume-size":"5G","openebs.io/controller-container-status":"Running","openebs.io/jiva-controller-cluster-ip":"10.106.224.86","vsm.openebs.io/iqn":"iqn.2016-09.com.openebs.jiva:test5","vsm.openebs.io/replica-count":"3","openebs.io/jiva-controller-ips":"172.17.0.3","vsm.openebs.io/controller-status":"Running","openebs.io/jiva-target-portal":"10.106.224.86:3260"},"creationTimestamp":null,"labels":{},"name":"test5"},"status":{"Message":"","Phase":"Running","Reason":""}}],"metadata":{}}`
tests := map[string]*struct {
volname string
fakeHandler utiltesting.FakeHandler
addr string
expectedOutput error
}{
"Creating new volume with volume name test1": {
volname: "test1",
fakeHandler: utiltesting.FakeHandler{
StatusCode: 200,
ResponseBody: string(response),
T: t,
},
addr: "MAPI_ADDR",
expectedOutput: nil,
},
"Getting status error 400": {
volname: "test2",
fakeHandler: utiltesting.FakeHandler{
StatusCode: 400,
ResponseBody: string("HTTP Error 400 - Bad Request"),
T: t,
},
addr: "MAPI_ADDR",
expectedOutput: fmt.Errorf("Status Error: Bad Request"),
},
"Getting status error 404": {
volname: "test3",
fakeHandler: utiltesting.FakeHandler{
StatusCode: 404,
ResponseBody: string("HTTP Error 404 - Not Found"),
T: t,
},
addr: "MAPI_ADDR",
expectedOutput: fmt.Errorf("Status Error: Not Found"),
},
"MAPI_ADDR not set": {
volname: "test4",
fakeHandler: utiltesting.FakeHandler{
StatusCode: 200,
ResponseBody: string(""),
T: t,
},
addr: "MAPI",
expectedOutput: fmt.Errorf("MAPI_ADDR environment variable not set"),
},
"Creating volume which already exist": {
volname: "test5",
fakeHandler: utiltesting.FakeHandler{
StatusCode: 200,
ResponseBody: string(response),
T: t,
},
addr: "MAPI_ADDR",
expectedOutput: fmt.Errorf("Error: Volume %v already exist ", "test5"),
},
}

for name, c := range tests {
t.Run(name, func(t *testing.T) {
server := httptest.NewServer(&c.fakeHandler)
defer os.Unsetenv(c.addr)
defer server.Close()
os.Setenv(c.addr, server.URL)
err := IsVolumeExist(c.volname)
if err != nil && err.Error() != c.expectedOutput.Error() {
t.Errorf("\nExpected output was : %v \nbut got : %v", c.expectedOutput, err)
} else if err == nil && c.expectedOutput != nil {
t.Errorf("\nExpected output was : %v \nbut got : %v", c.expectedOutput, nil)
}
})
}
}

0 comments on commit 0c17969

Please sign in to comment.