Skip to content

Commit

Permalink
chore: lint fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
hrak committed Nov 22, 2024
1 parent 4e69446 commit 720b0b6
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 47 deletions.
46 changes: 24 additions & 22 deletions pkg/driver/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,28 @@ package driver

import (
"context"
"go.uber.org/mock/gomock"
"testing"

"github.com/container-storage-interface/spec/lib/go/csi"
"github.com/leaseweb/cloudstack-csi-driver/pkg/cloud"
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"

"github.com/leaseweb/cloudstack-csi-driver/pkg/cloud"
)

var FakeCapacityGiB = 1
var FakeVolName = "CSIVolumeName"
var FakeVolID = "CSIVolumeID"
var FakeAvailability = "nova"
var FakeDiskOfferingId = "9743fd77-0f5d-4ef9-b2f8-f194235c769c"
var FakeVol = cloud.Volume{
ID: FakeVolID,
Name: FakeVolName,
Size: int64(FakeCapacityGiB),
ZoneID: FakeAvailability,
}
var (
FakeCapacityGiB = 1
FakeVolName = "CSIVolumeName"
FakeVolID = "CSIVolumeID"
FakeAvailability = "nova"
FakeDiskOfferingID = "9743fd77-0f5d-4ef9-b2f8-f194235c769c"
FakeVol = cloud.Volume{
ID: FakeVolID,
Name: FakeVolName,
Size: int64(FakeCapacityGiB),
ZoneID: FakeAvailability,
}
)

func TestDetermineSize(t *testing.T) {
cases := []struct {
Expand Down Expand Up @@ -57,18 +60,17 @@ func TestDetermineSize(t *testing.T) {
}
}

// Test CreateVolume
func TestCreateVolume(t *testing.T) {
ctx := context.Background()
mockCtl := gomock.NewController(t)
defer mockCtl.Finish()
mockCloud := cloud.NewMockCloud(mockCtl)
mockCloud.EXPECT().CreateVolume(gomock.Eq(ctx), FakeDiskOfferingId, FakeAvailability, FakeVolName, gomock.Any()).Return(FakeVolID, nil)
mockCloud.EXPECT().CreateVolume(gomock.Eq(ctx), FakeDiskOfferingID, FakeAvailability, FakeVolName, gomock.Any()).Return(FakeVolID, nil)
mockCloud.EXPECT().GetVolumeByName(gomock.Eq(ctx), FakeVolName).Return(nil, cloud.ErrNotFound)
fakeCs := NewControllerService(mockCloud)
// mock CloudStack
// CreateVolume(ctx context.Context, diskOfferingID, zoneID, name string, sizeInGB int64) (string, error)
// csmock.On("CreateVolume", FakeCtx, FakeDiskOfferingId, FakeAvailability, FakeVolName, mock.AnythingOfType("int64")).Return(FakeVolID, nil)
// csmock.On("CreateVolume", FakeCtx, FakeDiskOfferingID, FakeAvailability, FakeVolName, mock.AnythingOfType("int64")).Return(FakeVolID, nil)
// csmock.On("GetVolumeByName", FakeCtx, FakeVolName).Return(nil, cloud.ErrNotFound)
// Init assert
assert := assert.New(t)
Expand All @@ -83,7 +85,7 @@ func TestCreateVolume(t *testing.T) {
},
},
Parameters: map[string]string{
DiskOfferingKey: FakeDiskOfferingId,
DiskOfferingKey: FakeDiskOfferingID,
},
AccessibilityRequirements: &csi.TopologyRequirement{
Requisite: []*csi.Topology{
Expand All @@ -99,9 +101,9 @@ func TestCreateVolume(t *testing.T) {
t.Errorf("failed to CreateVolume: %v", err)
}
// Assert
assert.NotNil(actualRes.Volume)
assert.NotNil(actualRes.Volume.CapacityBytes)
assert.NotEqual(0, len(actualRes.Volume.VolumeId), "Volume Id is nil")
assert.NotNil(actualRes.Volume.AccessibleTopology)
assert.Equal(FakeAvailability, actualRes.Volume.AccessibleTopology[0].GetSegments()[ZoneKey])
assert.NotNil(actualRes.GetVolume())
assert.NotNil(actualRes.GetVolume().GetCapacityBytes())
assert.NotEmpty(actualRes.GetVolume().GetVolumeId(), "Volume Id is empty")
assert.NotNil(actualRes.GetVolume().GetAccessibleTopology())
assert.Equal(FakeAvailability, actualRes.GetVolume().GetAccessibleTopology()[0].GetSegments()[ZoneKey])
}
6 changes: 3 additions & 3 deletions pkg/driver/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"k8s.io/klog/v2"
)

func (cs *Driver) GetPluginInfo(ctx context.Context, req *csi.GetPluginInfoRequest) (*csi.GetPluginInfoResponse, error) {
func (d *Driver) GetPluginInfo(ctx context.Context, req *csi.GetPluginInfoRequest) (*csi.GetPluginInfoResponse, error) {
logger := klog.FromContext(ctx)
logger.V(6).Info("GetPluginInfo: called", "args", *req)
resp := &csi.GetPluginInfoResponse{
Expand All @@ -18,14 +18,14 @@ func (cs *Driver) GetPluginInfo(ctx context.Context, req *csi.GetPluginInfoReque
return resp, nil
}

func (cs *Driver) Probe(ctx context.Context, req *csi.ProbeRequest) (*csi.ProbeResponse, error) {
func (d *Driver) Probe(ctx context.Context, req *csi.ProbeRequest) (*csi.ProbeResponse, error) {
logger := klog.FromContext(ctx)
logger.V(6).Info("Probe: called", "args", *req)

return &csi.ProbeResponse{}, nil
}

func (cs *Driver) GetPluginCapabilities(ctx context.Context, req *csi.GetPluginCapabilitiesRequest) (*csi.GetPluginCapabilitiesResponse, error) {
func (d *Driver) GetPluginCapabilities(ctx context.Context, req *csi.GetPluginCapabilitiesRequest) (*csi.GetPluginCapabilitiesResponse, error) {
logger := klog.FromContext(ctx)
logger.V(6).Info("Probe: called", "args", *req)

Expand Down
25 changes: 14 additions & 11 deletions pkg/driver/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ package driver

import (
"context"
"github.com/leaseweb/cloudstack-csi-driver/pkg/util"
"github.com/stretchr/testify/assert"
"os"
"path/filepath"
"testing"

"github.com/container-storage-interface/spec/lib/go/csi"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

cloud "github.com/leaseweb/cloudstack-csi-driver/pkg/cloud/fake"
"github.com/leaseweb/cloudstack-csi-driver/pkg/mount"
"github.com/leaseweb/cloudstack-csi-driver/pkg/util"
)

const (
Expand All @@ -33,23 +34,25 @@ func TestNodePublishVolumeIdempotentMount(t *testing.T) {
_ = driver.mounter.MakeDir(sourceTest)

volumeCap := csi.VolumeCapability_AccessMode{Mode: csi.VolumeCapability_AccessMode_SINGLE_NODE_WRITER}
req := csi.NodePublishVolumeRequest{VolumeCapability: &csi.VolumeCapability{AccessMode: &volumeCap},
req := csi.NodePublishVolumeRequest{
VolumeCapability: &csi.VolumeCapability{AccessMode: &volumeCap},
VolumeId: "vol_1",
TargetPath: targetTest,
StagingTargetPath: sourceTest,
Readonly: true}
Readonly: true,
}

_, err := driver.NodePublishVolume(context.Background(), &req)
assert.NoError(t, err)
require.NoError(t, err)
_, err = driver.NodePublishVolume(context.Background(), &req)
assert.NoError(t, err)
require.NoError(t, err)

// ensure the target not be mounted twice
targetAbs, err := filepath.Abs(targetTest)
assert.NoError(t, err)
require.NoError(t, err)

mountList, err := driver.mounter.List()
assert.NoError(t, err)
require.NoError(t, err)
mountPointNum := 0
for _, mountPoint := range mountList {
if mountPoint.Path == targetAbs {
Expand All @@ -58,10 +61,10 @@ func TestNodePublishVolumeIdempotentMount(t *testing.T) {
}
assert.Equal(t, 1, mountPointNum)
err = driver.mounter.Unmount(targetTest)
assert.NoError(t, err)
require.NoError(t, err)
_ = driver.mounter.Unmount(targetTest)
err = os.RemoveAll(sourceTest)
assert.NoError(t, err)
require.NoError(t, err)
err = os.RemoveAll(targetTest)
assert.NoError(t, err)
require.NoError(t, err)
}
4 changes: 2 additions & 2 deletions pkg/mount/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ func (*fakeMounter) MakeFile(path string) error {
return nil
}

func (m *fakeMounter) GetStatistics(_ string) (volumeStatistics, error) {
return volumeStatistics{
func (m *fakeMounter) GetStatistics(_ string) (VolumeStatistics, error) {
return VolumeStatistics{
AvailableBytes: 3 * giB,
TotalBytes: 10 * giB,
UsedBytes: 7 * giB,
Expand Down
18 changes: 9 additions & 9 deletions pkg/mount/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type Mounter interface { //nolint:interfacebloat
GetBlockSizeBytes(devicePath string) (int64, error)
GetDevicePath(ctx context.Context, volumeID string) (string, error)
GetDeviceNameFromMount(mountPath string) (string, int, error)
GetStatistics(volumePath string) (volumeStatistics, error)
GetStatistics(volumePath string) (VolumeStatistics, error)
IsBlockDevice(devicePath string) (bool, error)
IsCorruptedMnt(err error) bool
MakeDir(path string) error
Expand All @@ -50,7 +50,7 @@ type NodeMounter struct {
*mount.SafeFormatAndMount
}

type volumeStatistics struct {
type VolumeStatistics struct {
AvailableBytes, TotalBytes, UsedBytes int64
AvailableInodes, TotalInodes, UsedInodes int64
}
Expand Down Expand Up @@ -213,25 +213,25 @@ func (m *NodeMounter) NeedResize(devicePath string, deviceMountPath string) (boo
}

// GetStatistics gathers statistics on the volume.
func (m *NodeMounter) GetStatistics(volumePath string) (volumeStatistics, error) {
func (m *NodeMounter) GetStatistics(volumePath string) (VolumeStatistics, error) {
isBlock, err := m.IsBlockDevice(volumePath)
if err != nil {
return volumeStatistics{}, fmt.Errorf("failed to determine if volume %s is block device: %w", volumePath, err)
return VolumeStatistics{}, fmt.Errorf("failed to determine if volume %s is block device: %w", volumePath, err)
}

if isBlock {
// See http://man7.org/linux/man-pages/man8/blockdev.8.html for details
output, err := exec.Command("blockdev", "getsize64", volumePath).CombinedOutput()
if err != nil {
return volumeStatistics{}, fmt.Errorf("error when getting size of block volume at path %s: output: %s, err: %w", volumePath, string(output), err)
return VolumeStatistics{}, fmt.Errorf("error when getting size of block volume at path %s: output: %s, err: %w", volumePath, string(output), err)
}
strOut := strings.TrimSpace(string(output))
gotSizeBytes, err := strconv.ParseInt(strOut, 10, 64)
if err != nil {
return volumeStatistics{}, fmt.Errorf("failed to parse size %s into int", strOut)
return VolumeStatistics{}, fmt.Errorf("failed to parse size %s into int", strOut)
}

return volumeStatistics{
return VolumeStatistics{
TotalBytes: gotSizeBytes,
}, nil
}
Expand All @@ -240,10 +240,10 @@ func (m *NodeMounter) GetStatistics(volumePath string) (volumeStatistics, error)
// See http://man7.org/linux/man-pages/man2/statfs.2.html for details.
err = unix.Statfs(volumePath, &statfs)
if err != nil {
return volumeStatistics{}, err
return VolumeStatistics{}, err
}

volStats := volumeStatistics{
volStats := VolumeStatistics{
AvailableBytes: int64(statfs.Bavail) * int64(statfs.Bsize), //nolint:unconvert
TotalBytes: int64(statfs.Blocks) * int64(statfs.Bsize), //nolint:unconvert
UsedBytes: (int64(statfs.Blocks) - int64(statfs.Bfree)) * int64(statfs.Bsize), //nolint:unconvert
Expand Down

0 comments on commit 720b0b6

Please sign in to comment.