Skip to content

Commit

Permalink
Mock refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
sbezverk committed Mar 6, 2018
1 parent 3e4586c commit 2d36a9c
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 17 deletions.
47 changes: 42 additions & 5 deletions mock/service/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@ import (
"github.com/container-storage-interface/spec/lib/go/csi/v0"
)

const (
MaxStorageCapacity = tib
)

func (s *service) CreateVolume(
ctx context.Context,
req *csi.CreateVolumeRequest) (
*csi.CreateVolumeResponse, error) {

if len(req.Name) == 0 {
return nil, status.Error(codes.InvalidArgument, "Volume Name canot be empty")
return nil, status.Error(codes.InvalidArgument, "Volume Name cannot be empty")
}
if req.VolumeCapabilities == nil {
return nil, status.Error(codes.InvalidArgument, "Volume Capabilities cannot be empty")
Expand All @@ -41,12 +45,23 @@ func (s *service) CreateVolume(
capacity = lb
}
}

// Check for maximum available capacity
if capacity >= MaxStorageCapacity {
return nil, status.Errorf(codes.OutOfRange, "Requested capacity %d exceeds maximum allowed %d", capacity, MaxStorageCapacity)
}
// Create the volume and add it to the service's in-mem volume slice.
v := s.newVolume(req.Name, capacity)
s.volsRWL.Lock()
defer s.volsRWL.Unlock()
s.vols = append(s.vols, v)
MockVolumes[v.Id] = Volume{
VolumeCSI: v,
NodeID: "",
ISStaged: false,
ISPublished: false,
StageTargetPath: "",
TargetPath: "",
}

return &csi.CreateVolumeResponse{Volume: &v}, nil
}
Expand All @@ -59,6 +74,11 @@ func (s *service) DeleteVolume(
s.volsRWL.Lock()
defer s.volsRWL.Unlock()

// If the volume is not specified, return error
if len(req.VolumeId) == 0 {
return nil, status.Error(codes.InvalidArgument, "Volume ID cannot be empty")
}

// If the volume does not exist then return an idempotent response.
i, _ := s.findVolNoLock("id", req.VolumeId)
if i < 0 {
Expand All @@ -80,6 +100,20 @@ func (s *service) ControllerPublishVolume(
req *csi.ControllerPublishVolumeRequest) (
*csi.ControllerPublishVolumeResponse, error) {

if len(req.VolumeId) == 0 {
return nil, status.Error(codes.InvalidArgument, "Volume ID cannot be empty")
}
if len(req.NodeId) == 0 {
return nil, status.Error(codes.InvalidArgument, "Node ID cannot be empty")
}
if req.VolumeCapability == nil {
return nil, status.Error(codes.InvalidArgument, "Volume Capabilities cannot be empty")
}

if req.NodeId != s.nodeID {
return nil, status.Errorf(codes.NotFound, "Not matching Node ID %s to Mock Node ID %s", req.NodeId, s.nodeID)
}

s.volsRWL.Lock()
defer s.volsRWL.Unlock()

Expand Down Expand Up @@ -149,11 +183,14 @@ func (s *service) ValidateVolumeCapabilities(
req *csi.ValidateVolumeCapabilitiesRequest) (
*csi.ValidateVolumeCapabilitiesResponse, error) {

i, _ := s.findVolNoLock("id", req.VolumeId)
if i < 0 {
return nil, status.Error(codes.NotFound, req.VolumeId)
if len(req.GetVolumeId()) == 0 {
return nil, status.Error(codes.InvalidArgument, "Volume ID cannot be empty")
}
if len(req.VolumeCapabilities) == 0 {
return nil, status.Error(codes.InvalidArgument, req.VolumeId)
}
i, _ := s.findVolNoLock("id", req.VolumeId)
if i < 0 {
return nil, status.Error(codes.NotFound, req.VolumeId)
}

Expand Down
7 changes: 7 additions & 0 deletions mock/service/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ func (s *service) NodeUnpublishVolume(
req *csi.NodeUnpublishVolumeRequest) (
*csi.NodeUnpublishVolumeResponse, error) {

if len(req.GetVolumeId()) == 0 {
return nil, status.Error(codes.InvalidArgument, "Volume ID cannot be empty")
}
if len(req.GetTargetPath()) == 0 {
return nil, status.Error(codes.InvalidArgument, "Target Path cannot be empty")
}

s.volsRWL.Lock()
defer s.volsRWL.Unlock()

Expand Down
13 changes: 13 additions & 0 deletions mock/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ type service struct {
volsNID uint64
}

type Volume struct {
sync.Mutex
VolumeCSI csi.Volume
NodeID string
ISStaged bool
ISPublished bool
StageTargetPath string
TargetPath string
}

var MockVolumes map[string]Volume

// New returns a new Service.
func New() Service {
s := &service{nodeID: Name}
Expand All @@ -46,6 +58,7 @@ func New() Service {
s.newVolume("Mock Volume 2", gib100),
s.newVolume("Mock Volume 3", gib100),
}
MockVolumes = map[string]Volume{}
return s
}

Expand Down
18 changes: 11 additions & 7 deletions pkg/sanity/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,16 @@ var _ = Describe("DeleteVolume [Controller Server]", func() {
}
})

It("should not fail when no volume id is provided", func() {
It("should fail when no volume id is provided", func() {

_, err := c.DeleteVolume(
context.Background(),
&csi.DeleteVolumeRequest{})
Expect(err).NotTo(HaveOccurred())
Expect(err).To(HaveOccurred())

serverError, ok := status.FromError(err)
Expect(ok).To(BeTrue())
Expect(serverError.Code()).To(Equal(codes.InvalidArgument))
})

It("should succeed when an invalid volume id is used", func() {
Expand Down Expand Up @@ -342,7 +346,7 @@ var _ = Describe("ValidateVolumeCapabilities [Controller Server]", func() {

serverError, ok := status.FromError(err)
Expect(ok).To(BeTrue())
Expect(serverError.Code()).To(Equal(codes.NotFound))
Expect(serverError.Code()).To(Equal(codes.InvalidArgument))
})

It("should fail when no volume capabilities are provided", func() {
Expand All @@ -356,7 +360,7 @@ var _ = Describe("ValidateVolumeCapabilities [Controller Server]", func() {

serverError, ok := status.FromError(err)
Expect(ok).To(BeTrue())
Expect(serverError.Code()).To(Equal(codes.NotFound))
Expect(serverError.Code()).To(Equal(codes.InvalidArgument))
})

It("should return appropriate values (no optional values added)", func() {
Expand Down Expand Up @@ -440,7 +444,7 @@ var _ = Describe("ControllerPublishVolume [Controller Server]", func() {

serverError, ok := status.FromError(err)
Expect(ok).To(BeTrue())
Expect(serverError.Code()).To(Equal(codes.NotFound))
Expect(serverError.Code()).To(Equal(codes.InvalidArgument))
})

It("should fail when no node id is provided", func() {
Expand All @@ -454,7 +458,7 @@ var _ = Describe("ControllerPublishVolume [Controller Server]", func() {

serverError, ok := status.FromError(err)
Expect(ok).To(BeTrue())
Expect(serverError.Code()).To(Equal(codes.NotFound))
Expect(serverError.Code()).To(Equal(codes.InvalidArgument))
})

It("should fail when no volume capability is provided", func() {
Expand All @@ -469,7 +473,7 @@ var _ = Describe("ControllerPublishVolume [Controller Server]", func() {

serverError, ok := status.FromError(err)
Expect(ok).To(BeTrue())
Expect(serverError.Code()).To(Equal(codes.NotFound))
Expect(serverError.Code()).To(Equal(codes.InvalidArgument))
})

It("should return appropriate values (no optional values added)", func() {
Expand Down
8 changes: 3 additions & 5 deletions pkg/sanity/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ var _ = Describe("NodePublishVolume [Node Server]", func() {
Expect(err).NotTo(HaveOccurred())
Expect(nid).NotTo(BeNil())
Expect(nid.GetNodeId()).NotTo(BeEmpty())

var conpubvol *csi.ControllerPublishVolumeResponse
if controllerPublishSupported {
By("controller publishing volume")
Expand All @@ -190,7 +189,6 @@ var _ = Describe("NodePublishVolume [Node Server]", func() {
Expect(err).NotTo(HaveOccurred())
Expect(conpubvol).NotTo(BeNil())
}

// NodePublishVolume
By("publishing the volume on a node")
nodepubvolRequest := &csi.NodePublishVolumeRequest{
Expand Down Expand Up @@ -269,7 +267,7 @@ var _ = Describe("NodeUnpublishVolume [Node Server]", func() {

serverError, ok := status.FromError(err)
Expect(ok).To(BeTrue())
Expect(serverError.Code()).To(Equal(codes.NotFound))
Expect(serverError.Code()).To(Equal(codes.InvalidArgument))
})

It("should fail when no target path is provided", func() {
Expand All @@ -283,7 +281,7 @@ var _ = Describe("NodeUnpublishVolume [Node Server]", func() {

serverError, ok := status.FromError(err)
Expect(ok).To(BeTrue())
Expect(serverError.Code()).To(Equal(codes.NotFound))
Expect(serverError.Code()).To(Equal(codes.InvalidArgument))
})

It("should return appropriate values (no optional values added)", func() {
Expand Down Expand Up @@ -319,7 +317,7 @@ var _ = Describe("NodeUnpublishVolume [Node Server]", func() {
context.Background(),
&csi.ControllerPublishVolumeRequest{
VolumeId: vol.GetVolume().GetId(),
NodeId: "foobar",
NodeId: "io.kubernetes.storage.mock",
VolumeCapability: &csi.VolumeCapability{
AccessType: &csi.VolumeCapability_Mount{
Mount: &csi.VolumeCapability_MountVolume{},
Expand Down

0 comments on commit 2d36a9c

Please sign in to comment.