Skip to content

Commit

Permalink
Merge pull request #1458 from umagnus/unittest
Browse files Browse the repository at this point in the history
test: add nodeserver.go unit test
  • Loading branch information
k8s-ci-robot authored Aug 11, 2022
2 parents 80c8d1e + b241ee1 commit 3023037
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 2 deletions.
122 changes: 120 additions & 2 deletions pkg/azuredisk/nodeserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ func TestEnsureMountPoint(t *testing.T) {
desc string
target string
skipOnWindows bool
skipOnDarwin bool
expectedMnt bool
expectedErr testutil.TestError
}{
Expand All @@ -175,6 +176,7 @@ func TestEnsureMountPoint(t *testing.T) {
desc: "[Error] Not a directory",
target: azuredisk,
skipOnWindows: true, // no error reported in windows
skipOnDarwin: true,
expectedErr: testutil.TestError{
DefaultError: &os.PathError{Op: "mkdir", Path: azuredisk, Err: syscall.ENOTDIR},
},
Expand Down Expand Up @@ -202,7 +204,7 @@ func TestEnsureMountPoint(t *testing.T) {
d.setMounter(fakeMounter)

for _, test := range tests {
if !(runtime.GOOS == "windows" && test.skipOnWindows) {
if !(runtime.GOOS == "windows" && test.skipOnWindows) && !(runtime.GOOS == "darwin" && test.skipOnDarwin) {
mnt, err := d.ensureMountPoint(test.target)
if !testutil.AssertError(&test.expectedErr, err) {
t.Errorf("desc: %s\n actualErr: (%v), expectedErr: (%v)", test.desc, err, test.expectedErr.Error())
Expand Down Expand Up @@ -349,6 +351,16 @@ func TestNodeGetVolumeStats(t *testing.T) {
skipOnWindows: true,
expectedErr: nil,
},
{
desc: "failed to determine block device",
setupFunc: func(t *testing.T, d FakeDriver) {
d.getHostUtil().(*azureutils.FakeHostUtil).SetPathIsDeviceResult(fakePath, true, fmt.Errorf("host util is not device path"))
},
req: csi.NodeGetVolumeStatsRequest{VolumePath: fakePath, VolumeId: "vol_1"},
skipOnDarwin: true,
skipOnWindows: true,
expectedErr: status.Errorf(codes.NotFound, "failed to determine whether %s is block device: %v", fakePath, fmt.Errorf("host util is not device path")),
},
}

// Setup
Expand Down Expand Up @@ -392,6 +404,12 @@ func TestNodeStageVolume(t *testing.T) {
volumeContextWithResize := map[string]string{
consts.ResizeRequired: "true",
}
volumeContextWithMaxShare := map[string]string{
consts.MaxSharesField: "0.1",
}
volumeContextWithPerfProfileField := map[string]string{
consts.PerfProfileField: "wrong",
}

stdVolCapBlock := &csi.VolumeCapability_Block{
Block: &csi.VolumeCapability_BlockVolume{},
Expand Down Expand Up @@ -448,6 +466,12 @@ func TestNodeStageVolume(t *testing.T) {
req: csi.NodeStageVolumeRequest{VolumeId: "vol_1", StagingTargetPath: sourceTest, VolumeCapability: &csi.VolumeCapability{AccessMode: &volumeCapWrong}},
expectedErr: status.Error(codes.InvalidArgument, "Volume capability not supported"),
},
{
desc: "MaxShares value not supported",
req: csi.NodeStageVolumeRequest{VolumeId: "vol_1", StagingTargetPath: sourceTest, VolumeCapability: &csi.VolumeCapability{AccessMode: &volumeCap,
AccessType: stdVolCapBlock}, VolumeContext: volumeContextWithMaxShare},
expectedErr: status.Error(codes.InvalidArgument, "MaxShares value not supported"),
},
{
desc: "Volume operation in progress",
setupFunc: func(t *testing.T, d FakeDriver) {
Expand Down Expand Up @@ -507,6 +531,25 @@ func TestNodeStageVolume(t *testing.T) {
},
expectedErr: nil,
},
{
desc: "failed to get perf attributes",
skipOnDarwin: true,
skipOnWindows: true,
setupFunc: func(t *testing.T, d FakeDriver) {
d.setPerfOptimizationEnabled(true)
d.setNextCommandOutputScripts(blkidAction, fsckAction, blockSizeAction, blockSizeAction)
},
req: csi.NodeStageVolumeRequest{VolumeId: "vol_1", StagingTargetPath: sourceTest,
VolumeCapability: &csi.VolumeCapability{AccessMode: &volumeCap,
AccessType: stdVolCap},
PublishContext: publishContext,
VolumeContext: volumeContextWithPerfProfileField,
},
cleanupFunc: func(t *testing.T, d FakeDriver) {
d.setPerfOptimizationEnabled(false)
},
expectedErr: status.Errorf(codes.Internal, "failed to get perf attributes for /dev/sdd. Error: %v", fmt.Errorf("Perf profile wrong is invalid")),
},
{
desc: "Successfully staged with performance optimizations",
skipOnDarwin: true,
Expand Down Expand Up @@ -535,6 +578,58 @@ func TestNodeStageVolume(t *testing.T) {
},
expectedErr: nil,
},
{
desc: "failed to optimize device performance",
skipOnDarwin: true,
skipOnWindows: true,
setupFunc: func(t *testing.T, d FakeDriver) {
d.setPerfOptimizationEnabled(true)
mockoptimization := d.getDeviceHelper().(*mockoptimization.MockInterface)
diskSupportsPerfOptimizationCall := mockoptimization.EXPECT().
DiskSupportsPerfOptimization(gomock.Any(), gomock.Any()).
Return(true)
mockoptimization.EXPECT().
OptimizeDiskPerformance(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
Return(fmt.Errorf("failed to optimize device performance")).
After(diskSupportsPerfOptimizationCall)

d.setNextCommandOutputScripts(blkidAction, fsckAction, blockSizeAction, blockSizeAction)
},
req: csi.NodeStageVolumeRequest{VolumeId: "vol_1", StagingTargetPath: sourceTest,
VolumeCapability: &csi.VolumeCapability{AccessMode: &volumeCap,
AccessType: stdVolCap},
PublishContext: publishContext,
VolumeContext: volumeContext,
},
cleanupFunc: func(t *testing.T, d FakeDriver) {
d.setPerfOptimizationEnabled(false)
},
expectedErr: status.Errorf(codes.Internal, "failed to optimize device performance for target(/dev/sdd) error(%s)", fmt.Errorf("failed to optimize device performance")),
},
{
desc: "Successfully staged with perf optimization is disabled",
skipOnDarwin: true,
skipOnWindows: true,
setupFunc: func(t *testing.T, d FakeDriver) {
d.setPerfOptimizationEnabled(true)
mockoptimization := d.getDeviceHelper().(*mockoptimization.MockInterface)
mockoptimization.EXPECT().
DiskSupportsPerfOptimization(gomock.Any(), gomock.Any()).
Return(false)

d.setNextCommandOutputScripts(blkidAction, fsckAction, blockSizeAction, blockSizeAction)
},
req: csi.NodeStageVolumeRequest{VolumeId: "vol_1", StagingTargetPath: sourceTest,
VolumeCapability: &csi.VolumeCapability{AccessMode: &volumeCap,
AccessType: stdVolCap},
PublishContext: publishContext,
VolumeContext: volumeContext,
},
cleanupFunc: func(t *testing.T, d FakeDriver) {
d.setPerfOptimizationEnabled(false)
},
expectedErr: nil,
},
}

// Setup
Expand Down Expand Up @@ -659,6 +754,10 @@ func TestNodePublishVolume(t *testing.T) {
d, _ := NewFakeDriver(t)

volumeCap := csi.VolumeCapability_AccessMode{Mode: csi.VolumeCapability_AccessMode_SINGLE_NODE_SINGLE_WRITER}
volumeCapWrong := csi.VolumeCapability_AccessMode{Mode: 10}
volumeContextWithMaxShare := map[string]string{
consts.MaxSharesField: "0.1",
}
publishContext := map[string]string{
consts.LUN: "/dev/01",
}
Expand Down Expand Up @@ -688,6 +787,7 @@ func TestNodePublishVolume(t *testing.T) {
setup func()
req csi.NodePublishVolumeRequest
skipOnWindows bool
skipOnDarwin bool
expectedErr testutil.TestError
cleanup func()
}{
Expand All @@ -705,6 +805,23 @@ func TestNodePublishVolume(t *testing.T) {
DefaultError: status.Error(codes.InvalidArgument, "Volume ID missing in the request"),
},
},
{
desc: "MaxShares value not supported",
req: csi.NodePublishVolumeRequest{VolumeCapability: &csi.VolumeCapability{AccessMode: &volumeCap, AccessType: stdVolCapBlock},
VolumeId: "vol_1",
VolumeContext: volumeContextWithMaxShare},
expectedErr: testutil.TestError{
DefaultError: status.Error(codes.InvalidArgument, "MaxShares value not supported"),
},
},
{
desc: "Volume capability not supported",
req: csi.NodePublishVolumeRequest{VolumeCapability: &csi.VolumeCapability{AccessMode: &volumeCapWrong, AccessType: stdVolCapBlock},
VolumeId: "vol_1"},
expectedErr: testutil.TestError{
DefaultError: status.Error(codes.InvalidArgument, "Volume capability not supported"),
},
},
{
desc: "Staging target path missing",
req: csi.NodePublishVolumeRequest{VolumeCapability: &csi.VolumeCapability{AccessMode: &volumeCap, AccessType: stdVolCapBlock},
Expand All @@ -730,6 +847,7 @@ func TestNodePublishVolume(t *testing.T) {
StagingTargetPath: sourceTest,
Readonly: true},
skipOnWindows: true, // permission issues
skipOnDarwin: true,
expectedErr: testutil.TestError{
DefaultError: status.Errorf(codes.Internal, fmt.Sprintf("could not mount target \"%s\": "+
"mkdir %s: not a directory", azuredisk, azuredisk)),
Expand Down Expand Up @@ -804,7 +922,7 @@ func TestNodePublishVolume(t *testing.T) {
if test.setup != nil {
test.setup()
}
if !(test.skipOnWindows && runtime.GOOS == "windows") {
if !(test.skipOnWindows && runtime.GOOS == "windows") && !(test.skipOnDarwin && runtime.GOOS == "darwin") {
var err error
_, err = d.NodePublishVolume(context.Background(), &test.req)
if !testutil.AssertError(&test.expectedErr, err) {
Expand Down
4 changes: 4 additions & 0 deletions pkg/csi-common/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package csicommon
import (
"sync"
"testing"
"time"

"github.com/stretchr/testify/assert"
"google.golang.org/grpc"
Expand All @@ -31,7 +32,10 @@ func TestNewNonBlockingGRPCServer(t *testing.T) {

func TestStart(t *testing.T) {
s := NewNonBlockingGRPCServer()
// sleep a while to avoid race condition in unit test
time.Sleep(time.Millisecond * 2000)
s.Start("tcp://127.0.0.1:0", nil, nil, nil, true)
time.Sleep(time.Millisecond * 2000)
}

func TestServe(t *testing.T) {
Expand Down

0 comments on commit 3023037

Please sign in to comment.