From 2d36a9cecd4f9d80bbce7f6edc9a0dd842b821f5 Mon Sep 17 00:00:00 2001 From: Serguei Bezverkhi Date: Sat, 3 Mar 2018 00:08:20 -0500 Subject: [PATCH] Mock refactor --- mock/service/controller.go | 47 ++++++++++++++++++++++++++++++++++---- mock/service/node.go | 7 ++++++ mock/service/service.go | 13 +++++++++++ pkg/sanity/controller.go | 18 +++++++++------ pkg/sanity/node.go | 8 +++---- 5 files changed, 76 insertions(+), 17 deletions(-) diff --git a/mock/service/controller.go b/mock/service/controller.go index 2c30881d..545b2514 100644 --- a/mock/service/controller.go +++ b/mock/service/controller.go @@ -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") @@ -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 } @@ -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 { @@ -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() @@ -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) } diff --git a/mock/service/node.go b/mock/service/node.go index e6432f26..e4c7d874 100644 --- a/mock/service/node.go +++ b/mock/service/node.go @@ -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() diff --git a/mock/service/service.go b/mock/service/service.go index 94681449..dccad79c 100644 --- a/mock/service/service.go +++ b/mock/service/service.go @@ -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} @@ -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 } diff --git a/pkg/sanity/controller.go b/pkg/sanity/controller.go index 7fcd41a5..7a0fb964 100644 --- a/pkg/sanity/controller.go +++ b/pkg/sanity/controller.go @@ -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() { @@ -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() { @@ -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() { @@ -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() { @@ -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() { @@ -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() { diff --git a/pkg/sanity/node.go b/pkg/sanity/node.go index 9ed48cf6..e3f39022 100644 --- a/pkg/sanity/node.go +++ b/pkg/sanity/node.go @@ -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") @@ -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{ @@ -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() { @@ -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() { @@ -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{},