Skip to content

Commit

Permalink
make mock driver support pv healthy monitor
Browse files Browse the repository at this point in the history
  • Loading branch information
fengzixu committed Jun 17, 2020
1 parent e89bc15 commit 4c42647
Show file tree
Hide file tree
Showing 10 changed files with 765 additions and 285 deletions.
7 changes: 7 additions & 0 deletions driver/driver.mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/kubernetes-csi/csi-test/v3
go 1.12

require (
github.com/container-storage-interface/spec v1.2.0
github.com/container-storage-interface/spec v1.3.0
github.com/golang/mock v1.3.1
github.com/golang/protobuf v1.3.2
github.com/google/uuid v1.1.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMT
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/container-storage-interface/spec v1.2.0 h1:bD9KIVgaVKKkQ/UbVUY9kCaH/CJbhNxe0eeB4JeJV2s=
github.com/container-storage-interface/spec v1.2.0/go.mod h1:6URME8mwIBbpVyZV93Ce5St17xBiQJQY67NDsuohiy4=
github.com/container-storage-interface/spec v1.3.0 h1:wMH4UIoWnK/TXYw8mbcIHgZmB6kHOeIsYsiaTJwa6bc=
github.com/container-storage-interface/spec v1.3.0/go.mod h1:6URME8mwIBbpVyZV93Ce5St17xBiQJQY67NDsuohiy4=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
Expand Down
58 changes: 58 additions & 0 deletions mock/service/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,39 @@ func (s *service) ValidateVolumeCapabilities(
}, nil
}

func (s *service) ControllerGetVolume(
ctx context.Context,
req *csi.ControllerGetVolumeRequest) (
*csi.ControllerGetVolumeResponse, error) {

if hookVal, hookMsg := s.execHook("GetVolumeStart"); hookVal != codes.OK {
return nil, status.Errorf(hookVal, hookMsg)
}

resp := &csi.ControllerGetVolumeResponse{
Status: &csi.ControllerGetVolumeResponse_VolumeStatus{},
}
i, v := s.findVolByID(ctx, req.VolumeId)
if i < 0 {
resp.Status.VolumeCondition.Abnormal = true
resp.Status.VolumeCondition.Message = "volume not found"
return resp, status.Error(codes.NotFound, req.VolumeId)
}

resp.Volume = &v
if !s.config.DisableAttach {
resp.Status.PublishedNodeIds = []string{
s.nodeID,
}
}

if hookVal, hookMsg := s.execHook("GetVolumeEnd"); hookVal != codes.OK {
return nil, status.Errorf(hookVal, hookMsg)
}

return resp, nil
}

func (s *service) ListVolumes(
ctx context.Context,
req *csi.ListVolumesRequest) (
Expand Down Expand Up @@ -388,8 +421,19 @@ func (s *service) ListVolumes(
)

for i = 0; i < len(entries); i++ {
volumeStatus := &csi.ListVolumesResponse_VolumeStatus{
VolumeCondition: &csi.VolumeCondition{},
}

if !s.config.DisableAttach {
volumeStatus.PublishedNodeIds = []string{
s.nodeID,
}
}

entries[i] = &csi.ListVolumesResponse_Entry{
Volume: &vols[j],
Status: volumeStatus,
}
j++
}
Expand Down Expand Up @@ -482,6 +526,20 @@ func (s *service) ControllerGetCapabilities(
},
},
},
{
Type: &csi.ControllerServiceCapability_Rpc{
Rpc: &csi.ControllerServiceCapability_RPC{
Type: csi.ControllerServiceCapability_RPC_GET_VOLUME,
},
},
},
{
Type: &csi.ControllerServiceCapability_Rpc{
Rpc: &csi.ControllerServiceCapability_RPC{
Type: csi.ControllerServiceCapability_RPC_VOLUME_CONDITION,
},
},
},
}

if !s.config.DisableAttach {
Expand Down
45 changes: 34 additions & 11 deletions mock/service/node.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package service

import (
"fmt"
"os"
"path"
"strconv"
Expand Down Expand Up @@ -354,6 +355,13 @@ func (s *service) NodeGetCapabilities(
},
},
},
{
Type: &csi.NodeServiceCapability_Rpc{
Rpc: &csi.NodeServiceCapability_RPC{
Type: csi.NodeServiceCapability_RPC_VOLUME_CONDITION,
},
},
},
}
if s.config.NodeExpansionRequired {
capabilities = append(capabilities, &csi.NodeServiceCapability{
Expand Down Expand Up @@ -397,37 +405,52 @@ func (s *service) NodeGetVolumeStats(ctx context.Context,
if hookVal, hookMsg := s.execHook("NodeGetVolumeStatsStart"); hookVal != codes.OK {
return nil, status.Errorf(hookVal, hookMsg)
}

resp := &csi.NodeGetVolumeStatsResponse{
VolumeCondition: &csi.VolumeCondition{},
}

if len(req.GetVolumeId()) == 0 {
return nil, status.Error(codes.InvalidArgument, "Volume ID cannot be empty")
resp.VolumeCondition.Abnormal = true
resp.VolumeCondition.Message = "Volume ID cannot be empty"
return resp, status.Error(codes.InvalidArgument, "Volume ID cannot be empty")
}

if len(req.GetVolumePath()) == 0 {
return nil, status.Error(codes.InvalidArgument, "Volume Path cannot be empty")
resp.VolumeCondition.Abnormal = true
resp.VolumeCondition.Message = "Volume path cannot be empty"
return resp, status.Error(codes.InvalidArgument, "Volume Path cannot be empty")
}

i, v := s.findVolNoLock("id", req.VolumeId)
if i < 0 {
return nil, status.Error(codes.NotFound, req.VolumeId)
resp.VolumeCondition.Abnormal = true
resp.VolumeCondition.Message = "Volume not found"
return resp, status.Error(codes.NotFound, req.VolumeId)
}

nodeMntPathKey := path.Join(s.nodeID, req.VolumePath)

_, exists := v.VolumeContext[nodeMntPathKey]
if !exists {
return nil, status.Errorf(codes.NotFound, "volume %q doest not exist on the specified path %q", req.VolumeId, req.VolumeId)
msg := fmt.Sprintf("volume %q doest not exist on the specified path %q", req.VolumeId, req.VolumePath)
resp.VolumeCondition.Abnormal = true
resp.VolumeCondition.Message = msg
return resp, status.Errorf(codes.NotFound, msg)
}

if hookVal, hookMsg := s.execHook("NodeGetVolumeStatsEnd"); hookVal != codes.OK {
return nil, status.Errorf(hookVal, hookMsg)
}

return &csi.NodeGetVolumeStatsResponse{
Usage: []*csi.VolumeUsage{
{
Total: v.GetCapacityBytes(),
Unit: csi.VolumeUsage_BYTES,
},
resp.Usage = []*csi.VolumeUsage{
{
Total: v.GetCapacityBytes(),
Unit: csi.VolumeUsage_BYTES,
},
}, nil
}

return resp, nil
}

// checkTargetExists checks if a given path exists.
Expand Down
6 changes: 6 additions & 0 deletions mock/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,12 @@ func (s *service) findVolByName(
return s.findVol("name", name)
}

func (s *service) findVolByID(
ctx context.Context, id string) (int, csi.Volume) {

return s.findVol("id", id)
}

func (s *service) newSnapshot(name, sourceVolumeId string, parameters map[string]string) cache.Snapshot {

ptime := ptypes.TimestampNow()
Expand Down
2 changes: 2 additions & 0 deletions pkg/sanity/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ var _ = DescribeSanity("Controller Service [Controller Server]", func(sc *TestCo
case csi.ControllerServiceCapability_RPC_CLONE_VOLUME:
case csi.ControllerServiceCapability_RPC_EXPAND_VOLUME:
case csi.ControllerServiceCapability_RPC_LIST_VOLUMES_PUBLISHED_NODES:
case csi.ControllerServiceCapability_RPC_GET_VOLUME:
case csi.ControllerServiceCapability_RPC_VOLUME_CONDITION:
default:
Fail(fmt.Sprintf("Unknown capability: %v\n", cap.GetRpc().GetType()))
}
Expand Down
1 change: 1 addition & 0 deletions pkg/sanity/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ var _ = DescribeSanity("Node Service", func(sc *TestContext) {
case csi.NodeServiceCapability_RPC_STAGE_UNSTAGE_VOLUME:
case csi.NodeServiceCapability_RPC_GET_VOLUME_STATS:
case csi.NodeServiceCapability_RPC_EXPAND_VOLUME:
case csi.NodeServiceCapability_RPC_VOLUME_CONDITION:
default:
Fail(fmt.Sprintf("Unknown capability: %v\n", cap.GetRpc().GetType()))
}
Expand Down
Loading

0 comments on commit 4c42647

Please sign in to comment.