Skip to content

Commit

Permalink
Move methods to util
Browse files Browse the repository at this point in the history
  • Loading branch information
xxx0624 committed Jul 6, 2023
1 parent 0983a83 commit 60bf23e
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 48 deletions.
8 changes: 0 additions & 8 deletions ecs-agent/daemonimages/csidriver/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,6 @@ build-csi-driver: bin/csi-driver
./build-csi-driver-image
$(MAKE) clean

.PHONY: test
test:
go test -v -race -tags unit -timeout=60s ./...

.PHONY: get-deps
get-deps:
go install github.com/golang/mock/mockgen@v1.6.0

.PHONY: mockgen
mockgen:
./update-gomock
Expand Down
2 changes: 0 additions & 2 deletions ecs-agent/daemonimages/csidriver/driver/mock_mount.go

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

3 changes: 2 additions & 1 deletion ecs-agent/daemonimages/csidriver/driver/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"google.golang.org/grpc/status"
"k8s.io/klog/v2"

"github.com/aws/amazon-ecs-agent/ecs-agent/daemonimages/csidriver/util"
"github.com/aws/amazon-ecs-agent/ecs-agent/daemonimages/csidriver/volume"
)

Expand Down Expand Up @@ -62,7 +63,7 @@ func (d *nodeService) NodeGetVolumeStats(ctx context.Context, req *csi.NodeGetVo
return nil, status.Errorf(codes.NotFound, "path %s does not exist", req.VolumePath)
}

isBlock, err := d.IsBlockDevice(req.VolumePath)
isBlock, err := util.IsBlockDevice(req.VolumePath)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to determine whether %s is block device: %v", req.VolumePath, err)
}
Expand Down
13 changes: 0 additions & 13 deletions ecs-agent/daemonimages/csidriver/driver/node_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,8 @@ import (
"fmt"
"strconv"
"strings"

"golang.org/x/sys/unix"
)

// IsBlock checks if the given path is a block device
func (d *nodeService) IsBlockDevice(fullPath string) (bool, error) {
var st unix.Stat_t
err := unix.Stat(fullPath, &st)
if err != nil {
return false, err
}

return (st.Mode & unix.S_IFMT) == unix.S_IFBLK, nil
}

func (d *nodeService) getBlockSizeBytes(devicePath string) (int64, error) {
cmd := d.mounter.(*NodeMounter).Exec.Command("blockdev", "--getsize64", devicePath)
output, err := cmd.Output()
Expand Down
43 changes: 29 additions & 14 deletions ecs-agent/daemonimages/csidriver/driver/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package driver
import (
"context"
"errors"
"fmt"
"os"
"testing"

Expand Down Expand Up @@ -85,11 +86,11 @@ func TestNodeGetVolumeStats(t *testing.T) {
VolumePath: VolumePath,
}
_, err := awsDriver.NodeGetVolumeStats(context.TODO(), req)
expectErr(t, err, codes.NotFound)
expectErrorCode(t, err, codes.NotFound)
},
},
{
name: "fail can't determine block device",
name: "fail can't determine block device due to no such file",
testFunc: func(t *testing.T) {
mockCtl := gomock.NewController(t)
defer mockCtl.Finish()
Expand All @@ -108,7 +109,8 @@ func TestNodeGetVolumeStats(t *testing.T) {
VolumePath: VolumePath,
}
_, err := awsDriver.NodeGetVolumeStats(context.TODO(), req)
expectErr(t, err, codes.Internal)
expectErrorCode(t, err, codes.Internal)
expectErrorMessage(t, err, fmt.Sprintf("failed to determine whether %s is block device:", VolumePath))
},
},
{
Expand All @@ -131,7 +133,7 @@ func TestNodeGetVolumeStats(t *testing.T) {
VolumePath: VolumePath,
}
_, err := awsDriver.NodeGetVolumeStats(context.TODO(), req)
expectErr(t, err, codes.Internal)
expectErrorCode(t, err, codes.Internal)
},
},
}
Expand All @@ -142,17 +144,30 @@ func TestNodeGetVolumeStats(t *testing.T) {

}

func expectErr(t *testing.T, actualErr error, expectedCode codes.Code) {
if actualErr == nil {
t.Fatalf("Expect error but got no error")
}
func expectErrorCode(t *testing.T, actualErr error, expectedCode codes.Code) {
require.NotNil(t, actualErr, "Expect error but got no error")

status, ok := status.FromError(actualErr)
if !ok {
t.Fatalf("Failed to get error status code from error: %v", actualErr)
}
require.True(t, ok, fmt.Sprintf("Failed to get error status from error: %v", actualErr))

require.Equal(
t,
expectedCode,
status.Code(),
fmt.Sprintf("Expected error code %d, got %d message %s", codes.InvalidArgument, status.Code(), status.Message()),
)
}

if status.Code() != expectedCode {
t.Fatalf("Expected error code %d, got %d message %s", codes.InvalidArgument, status.Code(), status.Message())
}
func expectErrorMessage(t *testing.T, actualErr error, expectedPartialMsg string) {
require.NotNil(t, actualErr, "Expect error but got no error")

status, ok := status.FromError(actualErr)
require.True(t, ok, fmt.Sprintf("Failed to get error status from error: %v", actualErr))

require.Containsf(
t,
status.Message(),
expectedPartialMsg,
fmt.Sprintf("Expected partial error message %s", expectedPartialMsg),
)
}
6 changes: 0 additions & 6 deletions ecs-agent/daemonimages/csidriver/driver/node_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ package driver

import "errors"

// IsBlockDevice checks if the given path is a block device
func (d *nodeService) IsBlockDevice(fullPath string) (bool, error) {
// TODO
return false, errors.New("not supported")
}

// getBlockSizeBytes gets the size of the disk in bytes
func (d *nodeService) getBlockSizeBytes(devicePath string) (int64, error) {
// TODO
Expand Down
Binary file modified ecs-agent/daemonimages/csidriver/tarfiles/csi-driver-amd64.tar
Binary file not shown.
4 changes: 0 additions & 4 deletions ecs-agent/daemonimages/csidriver/update-gomock
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,3 @@
set -euo pipefail

mockgen -package driver -destination=./driver/mock_mount.go -source ./driver/mount.go

# Fixes "Mounter Type cannot implement 'Mounter' as it has a non-exported method and is defined in a different package"
# See https://github.com/kubernetes/mount-utils/commit/a20fcfb15a701977d086330b47b7efad51eb608e for context.
sed -i '/type MockMounter struct {/a \\tmount_utils.Interface' ./driver/mock_mount.go
30 changes: 30 additions & 0 deletions ecs-agent/daemonimages/csidriver/util/utils_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//go:build linux
// +build linux

// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.

package util

import "golang.org/x/sys/unix"

// IsBlockDevice checks if the given path is a block device
func IsBlockDevice(fullPath string) (bool, error) {
var st unix.Stat_t
err := unix.Stat(fullPath, &st)
if err != nil {
return false, err
}

return (st.Mode & unix.S_IFMT) == unix.S_IFBLK, nil
}
14 changes: 14 additions & 0 deletions ecs-agent/daemonimages/csidriver/util/utils_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,25 @@ package util

import (
"fmt"
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestIsBlockDevice(t *testing.T) {
volumePath := "./test"
err := os.MkdirAll(volumePath, 0644)
require.NoError(t, err, "fail to create dir")
defer os.RemoveAll(volumePath)

isBlockDevice, err := IsBlockDevice(volumePath)

assert.Nil(t, err)
assert.Equal(t, false, isBlockDevice)
}

func TestParseEndpoint(t *testing.T) {
testCases := []struct {
name string
Expand Down
25 changes: 25 additions & 0 deletions ecs-agent/daemonimages/csidriver/util/utils_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//go:build windows
// +build windows

// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.

package util

import "errors"

// IsBlockDevice checks if the given path is a block device
func IsBlockDevice(fullPath string) (bool, error) {
// TODO
return false, errors.New("not supported")
}

0 comments on commit 60bf23e

Please sign in to comment.