Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make mock driver support PV healthy monitor #268

Merged
merged 1 commit into from
Jun 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
60 changes: 60 additions & 0 deletions mock/service/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,41 @@ 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{
VolumeCondition: &csi.VolumeCondition{},
},
}
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)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a case where volume is found but condition is Abnormal=true?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the mock test, I think we can divide it into 2 parts to do

  1. Add basic mock test functions for testing if volume-healthy-monitor can work(Due to the initial implementation PR wasn’t merged, it is hard to do further works)
  2. Add more unit, integration, e2e test for testing volume-healthy-monitor can work correctly on positive and negative cases

The reasons I want to do a testing task like above are below

  1. The initial PR blocks many works and it isn’t perfect. We can ensure it works normally and improves it
  2. After initial PR was merged. It is better for us to let more contributors contribute this project.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure


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 +423,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 +528,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
37 changes: 28 additions & 9 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,6 +405,11 @@ 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")
}
Expand All @@ -407,27 +420,33 @@ func (s *service) NodeGetVolumeStats(ctx context.Context,

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