Skip to content

Commit e2cec4e

Browse files
authored
Merge pull request kubernetes-csi#50 from sbezverk/mock_fix_controller
Mock refactor
2 parents 80f2905 + 513e5ae commit e2cec4e

File tree

5 files changed

+76
-17
lines changed

5 files changed

+76
-17
lines changed

mock/service/controller.go

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@ import (
1414
"github.com/container-storage-interface/spec/lib/go/csi/v0"
1515
)
1616

17+
const (
18+
MaxStorageCapacity = tib
19+
)
20+
1721
func (s *service) CreateVolume(
1822
ctx context.Context,
1923
req *csi.CreateVolumeRequest) (
2024
*csi.CreateVolumeResponse, error) {
2125

2226
if len(req.Name) == 0 {
23-
return nil, status.Error(codes.InvalidArgument, "Volume Name canot be empty")
27+
return nil, status.Error(codes.InvalidArgument, "Volume Name cannot be empty")
2428
}
2529
if req.VolumeCapabilities == nil {
2630
return nil, status.Error(codes.InvalidArgument, "Volume Capabilities cannot be empty")
@@ -41,12 +45,23 @@ func (s *service) CreateVolume(
4145
capacity = lb
4246
}
4347
}
44-
48+
// Check for maximum available capacity
49+
if capacity >= MaxStorageCapacity {
50+
return nil, status.Errorf(codes.OutOfRange, "Requested capacity %d exceeds maximum allowed %d", capacity, MaxStorageCapacity)
51+
}
4552
// Create the volume and add it to the service's in-mem volume slice.
4653
v := s.newVolume(req.Name, capacity)
4754
s.volsRWL.Lock()
4855
defer s.volsRWL.Unlock()
4956
s.vols = append(s.vols, v)
57+
MockVolumes[v.Id] = Volume{
58+
VolumeCSI: v,
59+
NodeID: "",
60+
ISStaged: false,
61+
ISPublished: false,
62+
StageTargetPath: "",
63+
TargetPath: "",
64+
}
5065

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

77+
// If the volume is not specified, return error
78+
if len(req.VolumeId) == 0 {
79+
return nil, status.Error(codes.InvalidArgument, "Volume ID cannot be empty")
80+
}
81+
6282
// If the volume does not exist then return an idempotent response.
6383
i, _ := s.findVolNoLock("id", req.VolumeId)
6484
if i < 0 {
@@ -80,6 +100,20 @@ func (s *service) ControllerPublishVolume(
80100
req *csi.ControllerPublishVolumeRequest) (
81101
*csi.ControllerPublishVolumeResponse, error) {
82102

103+
if len(req.VolumeId) == 0 {
104+
return nil, status.Error(codes.InvalidArgument, "Volume ID cannot be empty")
105+
}
106+
if len(req.NodeId) == 0 {
107+
return nil, status.Error(codes.InvalidArgument, "Node ID cannot be empty")
108+
}
109+
if req.VolumeCapability == nil {
110+
return nil, status.Error(codes.InvalidArgument, "Volume Capabilities cannot be empty")
111+
}
112+
113+
if req.NodeId != s.nodeID {
114+
return nil, status.Errorf(codes.NotFound, "Not matching Node ID %s to Mock Node ID %s", req.NodeId, s.nodeID)
115+
}
116+
83117
s.volsRWL.Lock()
84118
defer s.volsRWL.Unlock()
85119

@@ -149,11 +183,14 @@ func (s *service) ValidateVolumeCapabilities(
149183
req *csi.ValidateVolumeCapabilitiesRequest) (
150184
*csi.ValidateVolumeCapabilitiesResponse, error) {
151185

152-
i, _ := s.findVolNoLock("id", req.VolumeId)
153-
if i < 0 {
154-
return nil, status.Error(codes.NotFound, req.VolumeId)
186+
if len(req.GetVolumeId()) == 0 {
187+
return nil, status.Error(codes.InvalidArgument, "Volume ID cannot be empty")
155188
}
156189
if len(req.VolumeCapabilities) == 0 {
190+
return nil, status.Error(codes.InvalidArgument, req.VolumeId)
191+
}
192+
i, _ := s.findVolNoLock("id", req.VolumeId)
193+
if i < 0 {
157194
return nil, status.Error(codes.NotFound, req.VolumeId)
158195
}
159196

mock/service/node.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ func (s *service) NodeUnpublishVolume(
7575
req *csi.NodeUnpublishVolumeRequest) (
7676
*csi.NodeUnpublishVolumeResponse, error) {
7777

78+
if len(req.GetVolumeId()) == 0 {
79+
return nil, status.Error(codes.InvalidArgument, "Volume ID cannot be empty")
80+
}
81+
if len(req.GetTargetPath()) == 0 {
82+
return nil, status.Error(codes.InvalidArgument, "Target Path cannot be empty")
83+
}
84+
7885
s.volsRWL.Lock()
7986
defer s.volsRWL.Unlock()
8087

mock/service/service.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@ type service struct {
3838
volsNID uint64
3939
}
4040

41+
type Volume struct {
42+
sync.Mutex
43+
VolumeCSI csi.Volume
44+
NodeID string
45+
ISStaged bool
46+
ISPublished bool
47+
StageTargetPath string
48+
TargetPath string
49+
}
50+
51+
var MockVolumes map[string]Volume
52+
4153
// New returns a new Service.
4254
func New() Service {
4355
s := &service{nodeID: Name}
@@ -46,6 +58,7 @@ func New() Service {
4658
s.newVolume("Mock Volume 2", gib100),
4759
s.newVolume("Mock Volume 3", gib100),
4860
}
61+
MockVolumes = map[string]Volume{}
4962
return s
5063
}
5164

pkg/sanity/controller.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -269,12 +269,16 @@ var _ = Describe("DeleteVolume [Controller Server]", func() {
269269
}
270270
})
271271

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

274274
_, err := c.DeleteVolume(
275275
context.Background(),
276276
&csi.DeleteVolumeRequest{})
277-
Expect(err).NotTo(HaveOccurred())
277+
Expect(err).To(HaveOccurred())
278+
279+
serverError, ok := status.FromError(err)
280+
Expect(ok).To(BeTrue())
281+
Expect(serverError.Code()).To(Equal(codes.InvalidArgument))
278282
})
279283

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

343347
serverError, ok := status.FromError(err)
344348
Expect(ok).To(BeTrue())
345-
Expect(serverError.Code()).To(Equal(codes.NotFound))
349+
Expect(serverError.Code()).To(Equal(codes.InvalidArgument))
346350
})
347351

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

357361
serverError, ok := status.FromError(err)
358362
Expect(ok).To(BeTrue())
359-
Expect(serverError.Code()).To(Equal(codes.NotFound))
363+
Expect(serverError.Code()).To(Equal(codes.InvalidArgument))
360364
})
361365

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

441445
serverError, ok := status.FromError(err)
442446
Expect(ok).To(BeTrue())
443-
Expect(serverError.Code()).To(Equal(codes.NotFound))
447+
Expect(serverError.Code()).To(Equal(codes.InvalidArgument))
444448
})
445449

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

455459
serverError, ok := status.FromError(err)
456460
Expect(ok).To(BeTrue())
457-
Expect(serverError.Code()).To(Equal(codes.NotFound))
461+
Expect(serverError.Code()).To(Equal(codes.InvalidArgument))
458462
})
459463

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

470474
serverError, ok := status.FromError(err)
471475
Expect(ok).To(BeTrue())
472-
Expect(serverError.Code()).To(Equal(codes.NotFound))
476+
Expect(serverError.Code()).To(Equal(codes.InvalidArgument))
473477
})
474478

475479
It("should return appropriate values (no optional values added)", func() {

pkg/sanity/node.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,6 @@ var _ = Describe("NodePublishVolume [Node Server]", func() {
168168
Expect(err).NotTo(HaveOccurred())
169169
Expect(nid).NotTo(BeNil())
170170
Expect(nid.GetNodeId()).NotTo(BeEmpty())
171-
172171
var conpubvol *csi.ControllerPublishVolumeResponse
173172
if controllerPublishSupported {
174173
By("controller publishing volume")
@@ -190,7 +189,6 @@ var _ = Describe("NodePublishVolume [Node Server]", func() {
190189
Expect(err).NotTo(HaveOccurred())
191190
Expect(conpubvol).NotTo(BeNil())
192191
}
193-
194192
// NodePublishVolume
195193
By("publishing the volume on a node")
196194
nodepubvolRequest := &csi.NodePublishVolumeRequest{
@@ -269,7 +267,7 @@ var _ = Describe("NodeUnpublishVolume [Node Server]", func() {
269267

270268
serverError, ok := status.FromError(err)
271269
Expect(ok).To(BeTrue())
272-
Expect(serverError.Code()).To(Equal(codes.NotFound))
270+
Expect(serverError.Code()).To(Equal(codes.InvalidArgument))
273271
})
274272

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

284282
serverError, ok := status.FromError(err)
285283
Expect(ok).To(BeTrue())
286-
Expect(serverError.Code()).To(Equal(codes.NotFound))
284+
Expect(serverError.Code()).To(Equal(codes.InvalidArgument))
287285
})
288286

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

0 commit comments

Comments
 (0)